Skip to content

Commit a6110ea

Browse files
committed
fix: allow case-insensitive preproc directives
1 parent 7e54381 commit a6110ea

9 files changed

Lines changed: 35 additions & 24 deletions

File tree

src/parsetab/tabs.dbm.bak

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'zxbpp', (0, 67001)
1+
'zxbpp', (0, 66998)
22
'asmparse', (67072, 220672)
33
'zxnext_asmparse', (287744, 245099)
44
'zxbparser', (532992, 641182)

src/parsetab/tabs.dbm.dat

0 Bytes
Binary file not shown.

src/parsetab/tabs.dbm.dir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'zxbpp', (0, 67001)
1+
'zxbpp', (0, 66998)
22
'asmparse', (67072, 220672)
33
'zxnext_asmparse', (287744, 245099)
44
'zxbparser', (532992, 641182)

src/zxbpp/zxbasmpplex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def t_if_OR(self, t):
183183

184184
def t_prepro_ID(self, t):
185185
r"[._a-zA-Z][._a-zA-Z0-9]*" # preprocessor directives
186-
t.type = self.reserved_directives.get(t.value, "ID")
186+
t.type = self.reserved_directives.get(t.value.lower(), "ID")
187187
states_ = {"DEFINE": "define", "ERROR": "msg", "IF": "if", "LINE": "line", "PRAGMA": "pragma", "WARNING": "msg"}
188188

189189
if t.type in states_:

src/zxbpp/zxbpp.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ class IncludedFileInfo:
102102

103103
precedence = (
104104
("nonassoc", "DUMMY"),
105-
("left", "AND", "OR"),
105+
("left", "OR"),
106+
("left", "AND"),
106107
("left", "EQ", "NE", "LT", "LE", "GT", "GE"),
107108
("right", "LLP"),
108109
("left", "PASTE", "STRINGIZING"),
@@ -262,6 +263,22 @@ def expand_macros(macros: List[Any], lineno: int) -> Optional[str]:
262263
return tmp
263264

264265

266+
def to_bool(expr: Union[str, bool, int]) -> int:
267+
if isinstance(expr, str) and expr.isdigit():
268+
expr = int(expr)
269+
270+
return int(bool(expr))
271+
272+
273+
def to_int(expr: Union[str, int]) -> int:
274+
if isinstance(expr, str) and expr.isdigit():
275+
expr = int(expr)
276+
else:
277+
expr = 0
278+
279+
return expr
280+
281+
265282
# -------- GRAMMAR RULES for the preprocessor ---------
266283
def p_start(p):
267284
"""start : program"""
@@ -655,12 +672,12 @@ def p_expr_str(p):
655672

656673
def p_exprand(p):
657674
"""expr : expr AND expr"""
658-
p[0] = "1" if p[1] and p[3] else "0"
675+
p[0] = "1" if to_bool(p[1]) and to_bool(p[3]) else "0"
659676

660677

661678
def p_expror(p):
662679
"""expr : expr OR expr"""
663-
p[0] = "1" if p[1] or p[3] else "0"
680+
p[0] = "1" if to_bool(p[1]) or to_bool(p[3]) else "0"
664681

665682

666683
def p_exprne(p):
@@ -675,34 +692,22 @@ def p_expreq(p):
675692

676693
def p_exprlt(p):
677694
"""expr : expr LT expr"""
678-
a = int(p[1]) if p[1].isdigit() else 0
679-
b = int(p[3]) if p[3].isdigit() else 0
680-
681-
p[0] = "1" if a < b else "0"
695+
p[0] = "1" if to_int(p[1]) < to_int(p[3]) else "0"
682696

683697

684698
def p_exprle(p):
685699
"""expr : expr LE expr"""
686-
a = int(p[1]) if p[1].isdigit() else 0
687-
b = int(p[3]) if p[3].isdigit() else 0
688-
689-
p[0] = "1" if a <= b else "0"
700+
p[0] = "1" if to_int(p[1]) <= to_int(p[3]) else "0"
690701

691702

692703
def p_exprgt(p):
693704
"""expr : expr GT expr"""
694-
a = int(p[1]) if p[1].isdigit() else 0
695-
b = int(p[3]) if p[3].isdigit() else 0
696-
697-
p[0] = "1" if a > b else "0"
705+
p[0] = "1" if to_int(p[1]) > to_int(p[3]) else "0"
698706

699707

700708
def p_exprge(p):
701709
"""expr : expr GE expr"""
702-
a = int(p[1]) if p[1].isdigit() else 0
703-
b = int(p[3]) if p[3].isdigit() else 0
704-
705-
p[0] = "1" if a >= b else "0"
710+
p[0] = "1" if to_int(p[1]) >= to_int(p[3]) else "0"
706711

707712

708713
def p_expr_par(p):

src/zxbpp/zxbpplex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def t_if_OR(self, t):
236236

237237
def t_prepro_ID(self, t):
238238
r"[._a-zA-Z][._a-zA-Z0-9]*" # preprocessor directives
239-
t.type = self.reserved_directives.get(t.value, "ID")
239+
t.type = self.reserved_directives.get(t.value.lower(), "ID")
240240
states_ = {"DEFINE": "define", "ERROR": "msg", "IF": "if", "LINE": "line", "PRAGMA": "pragma", "WARNING": "msg"}
241241

242242
if t.type in states_:

tests/functional/iflogic.bi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ PRINT "And works"
1313
#if (A == 1 && B == 1) || B == 2
1414
PRINT "Parenthesis works"
1515
#endif
16+
17+
#if A == 1 && B != 2
18+
PRINT "this should not happen!"
19+
#endif

tests/functional/iflogic.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ PRINT "And works"
1414

1515
PRINT "Parenthesis works"
1616
#line 16 "iflogic.bi"
17+
18+
#line 20 "iflogic.bi"

tests/functional/zx48k/spfill.bas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
' (c)2002(?) by Alvin Albretch
33
' Ported to ZX Basic by Paul Fisher (Britlion)
44

5-
#include <SP/Fill.bas>
5+
#Include <SP/Fill.bas>
66

77
10 CLS
88
20 CIRCLE 128, 87, 87

0 commit comments

Comments
 (0)