Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request boriel-basic#606 from boriel/bugfix/allow_case_ins…
Browse files Browse the repository at this point in the history
…ensitive_directivez_in_preproc

fix: allow case-insensitive preproc directives
  • Loading branch information
boriel committed Feb 10, 2022
2 parents 7e54381 + a6110ea commit aee8044
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/parsetab/tabs.dbm.bak
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'zxbpp', (0, 67001)
'zxbpp', (0, 66998)
'asmparse', (67072, 220672)
'zxnext_asmparse', (287744, 245099)
'zxbparser', (532992, 641182)
Binary file modified src/parsetab/tabs.dbm.dat
Binary file not shown.
2 changes: 1 addition & 1 deletion src/parsetab/tabs.dbm.dir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'zxbpp', (0, 67001)
'zxbpp', (0, 66998)
'asmparse', (67072, 220672)
'zxnext_asmparse', (287744, 245099)
'zxbparser', (532992, 641182)
2 changes: 1 addition & 1 deletion src/zxbpp/zxbasmpplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def t_if_OR(self, t):

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

if t.type in states_:
Expand Down
43 changes: 24 additions & 19 deletions src/zxbpp/zxbpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class IncludedFileInfo:

precedence = (
("nonassoc", "DUMMY"),
("left", "AND", "OR"),
("left", "OR"),
("left", "AND"),
("left", "EQ", "NE", "LT", "LE", "GT", "GE"),
("right", "LLP"),
("left", "PASTE", "STRINGIZING"),
Expand Down Expand Up @@ -262,6 +263,22 @@ def expand_macros(macros: List[Any], lineno: int) -> Optional[str]:
return tmp


def to_bool(expr: Union[str, bool, int]) -> int:
if isinstance(expr, str) and expr.isdigit():
expr = int(expr)

return int(bool(expr))


def to_int(expr: Union[str, int]) -> int:
if isinstance(expr, str) and expr.isdigit():
expr = int(expr)
else:
expr = 0

return expr


# -------- GRAMMAR RULES for the preprocessor ---------
def p_start(p):
"""start : program"""
Expand Down Expand Up @@ -655,12 +672,12 @@ def p_expr_str(p):

def p_exprand(p):
"""expr : expr AND expr"""
p[0] = "1" if p[1] and p[3] else "0"
p[0] = "1" if to_bool(p[1]) and to_bool(p[3]) else "0"


def p_expror(p):
"""expr : expr OR expr"""
p[0] = "1" if p[1] or p[3] else "0"
p[0] = "1" if to_bool(p[1]) or to_bool(p[3]) else "0"


def p_exprne(p):
Expand All @@ -675,34 +692,22 @@ def p_expreq(p):

def p_exprlt(p):
"""expr : expr LT expr"""
a = int(p[1]) if p[1].isdigit() else 0
b = int(p[3]) if p[3].isdigit() else 0

p[0] = "1" if a < b else "0"
p[0] = "1" if to_int(p[1]) < to_int(p[3]) else "0"


def p_exprle(p):
"""expr : expr LE expr"""
a = int(p[1]) if p[1].isdigit() else 0
b = int(p[3]) if p[3].isdigit() else 0

p[0] = "1" if a <= b else "0"
p[0] = "1" if to_int(p[1]) <= to_int(p[3]) else "0"


def p_exprgt(p):
"""expr : expr GT expr"""
a = int(p[1]) if p[1].isdigit() else 0
b = int(p[3]) if p[3].isdigit() else 0

p[0] = "1" if a > b else "0"
p[0] = "1" if to_int(p[1]) > to_int(p[3]) else "0"


def p_exprge(p):
"""expr : expr GE expr"""
a = int(p[1]) if p[1].isdigit() else 0
b = int(p[3]) if p[3].isdigit() else 0

p[0] = "1" if a >= b else "0"
p[0] = "1" if to_int(p[1]) >= to_int(p[3]) else "0"


def p_expr_par(p):
Expand Down
2 changes: 1 addition & 1 deletion src/zxbpp/zxbpplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def t_if_OR(self, t):

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

if t.type in states_:
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/iflogic.bi
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ PRINT "And works"
#if (A == 1 && B == 1) || B == 2
PRINT "Parenthesis works"
#endif

#if A == 1 && B != 2
PRINT "this should not happen!"
#endif
2 changes: 2 additions & 0 deletions tests/functional/iflogic.out
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ PRINT "And works"

PRINT "Parenthesis works"
#line 16 "iflogic.bi"

#line 20 "iflogic.bi"
2 changes: 1 addition & 1 deletion tests/functional/zx48k/spfill.bas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
' (c)2002(?) by Alvin Albretch
' Ported to ZX Basic by Paul Fisher (Britlion)

#include <SP/Fill.bas>
#Include <SP/Fill.bas>

10 CLS
20 CIRCLE 128, 87, 87
Expand Down

0 comments on commit aee8044

Please sign in to comment.