Skip to content

Commit 85f90b8

Browse files
committed
Refactorizes grammar
Now singleid will encapsulate any ID
1 parent 22012c8 commit 85f90b8

4 files changed

Lines changed: 45 additions & 22 deletions

File tree

parsetab/tabs.dbm.bak

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
'zxbppparse', (0, 69164)
22
'asmparse', (69632, 250673)
33
'zxnext_asmparse', (320512, 281631)
4-
'zxbparser', (602624, 708961)
4+
'zxbparser', (602624, 708747)

parsetab/tabs.dbm.dat

-214 Bytes
Binary file not shown.

parsetab/tabs.dbm.dir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
'zxbppparse', (0, 69164)
22
'asmparse', (69632, 250673)
33
'zxnext_asmparse', (320512, 281631)
4-
'zxbparser', (602624, 708961)
4+
'zxbparser', (602624, 708747)

zxb/zxbparser.py

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
from math import pi as PI
1818
import collections
1919

20+
# typings
21+
from typing import NamedTuple
22+
2023
# Compiler API
2124
import api
2225
from api.debug import __DEBUG__ # analysis:ignore
@@ -110,6 +113,18 @@
110113
last_brk_linenum = 0
111114

112115

116+
# ----------------------------------------------------------------------
117+
# Start of parsing
118+
# ----------------------------------------------------------------------
119+
120+
121+
class Id(NamedTuple):
122+
""" Encapsulates an ID name and its line number where it was read
123+
"""
124+
name: str
125+
lineno: int
126+
127+
113128
def init():
114129
""" Initializes parser state
115130
"""
@@ -150,7 +165,7 @@ def init():
150165

151166

152167
# ----------------------------------------------------------------------
153-
# "Macro" functions. Just return more complex expresions
168+
# "Macro" functions. Just return more complex expressions
154169
# ----------------------------------------------------------------------
155170
def _TYPE(type_):
156171
""" returns an internal type converted to a SYMBOL_TABLE
@@ -391,7 +406,7 @@ def make_call(id_, lineno, args):
391406
return make_func_call(id_, lineno, args)
392407

393408

394-
def make_param_decl(id_, lineno, typedef, is_array=False):
409+
def make_param_decl(id_: str, lineno: int, typedef, is_array=False):
395410
""" Wrapper that creates a param declaration
396411
"""
397412
return SYMBOL_TABLE.declare_param(id_, lineno, typedef, is_array)
@@ -714,18 +729,24 @@ def p_var_decl_ini(p):
714729
p[0] = make_sentence('LET', SYMBOL_TABLE.access_var(p[2][0][0], p.lineno(1)), value)
715730

716731

732+
def p_singleid(p):
733+
""" singleid : ID
734+
| ARRAY_ID
735+
"""
736+
p[0] = Id(name=p[1], lineno=p.lineno(1))
737+
738+
717739
def p_idlist_id(p):
718-
""" idlist : ID
719-
| ARRAY_ID
740+
""" idlist : singleid
720741
"""
721-
p[0] = [(p[1], p.lineno(1))]
742+
p[0] = [p[1]]
722743

723744

724745
def p_idlist_idlist_id(p):
725-
""" idlist : idlist COMMA ID
726-
| idlist COMMA ARRAY_ID
746+
""" idlist : idlist COMMA singleid
727747
"""
728-
p[0] = p[1] + [(p[3], p.lineno(3))]
748+
p[1].append(p[3])
749+
p[0] = p[1]
729750

730751

731752
def p_arr_decl(p):
@@ -2593,10 +2614,10 @@ def p_id_expr(p):
25932614

25942615

25952616
def p_addr_of_id(p):
2596-
""" bexpr : ADDRESSOF ID
2597-
| ADDRESSOF ARRAY_ID
2617+
""" bexpr : ADDRESSOF singleid
25982618
"""
2599-
entry = SYMBOL_TABLE.access_id(p[2], p.lineno(2))
2619+
id_: Id = p[2]
2620+
entry = SYMBOL_TABLE.access_id(id_.name, id_.lineno)
26002621
if entry is None:
26012622
p[0] = None
26022623
return
@@ -3030,27 +3051,29 @@ def p_param_definition(p):
30303051

30313052

30323053
def p_param_def_array(p):
3033-
""" param_def : ID LP RP typedef
3034-
| ARRAY_ID LP RP typedef
3054+
""" param_def : singleid LP RP typedef
30353055
"""
30363056
typeref = p[4]
30373057
if typeref is None:
30383058
p[0] = None
30393059
return
30403060

3041-
lineno = p.lineno(1)
3042-
id_ = p[1]
3061+
lineno = p[1].lineno
3062+
id_ = p[1].name
30433063

30443064
api.check.check_type_is_explicit(lineno, id_, typeref)
30453065
p[0] = make_param_decl(id_, lineno, typeref, is_array=True)
30463066

30473067

30483068
def p_param_def_type(p):
3049-
""" param_def : ID typedef
3069+
""" param_def : singleid typedef
30503070
"""
3051-
if p[2] is not None:
3052-
api.check.check_type_is_explicit(p.lineno(1), p[1], p[2])
3053-
p[0] = make_param_decl(p[1], p.lineno(1), p[2])
3071+
id_: Id = p[1]
3072+
typedef = p[2]
3073+
if typedef is not None:
3074+
api.check.check_type_is_explicit(id_.lineno, id_.name, typedef)
3075+
3076+
p[0] = make_param_decl(id_.name, id_.lineno, typedef)
30543077

30553078

30563079
def p_function_body(p):
@@ -3473,7 +3496,7 @@ def p_error(p):
34733496
# ----------------------------------------
34743497
# Initialization
34753498
# ----------------------------------------
3476-
parser = api.utils.get_or_create('zxbparser', lambda: yacc.yacc(debug=OPTIONS.Debug.value > 2))
3499+
parser = api.utils.get_or_create('zxbparser', lambda: yacc.yacc(debug=True))
34773500

34783501
ast = None
34793502
data_ast = None # Global Variables AST

0 commit comments

Comments
 (0)