|
17 | 17 | from math import pi as PI |
18 | 18 | import collections |
19 | 19 |
|
| 20 | +# typings |
| 21 | +from typing import NamedTuple |
| 22 | + |
20 | 23 | # Compiler API |
21 | 24 | import api |
22 | 25 | from api.debug import __DEBUG__ # analysis:ignore |
|
110 | 113 | last_brk_linenum = 0 |
111 | 114 |
|
112 | 115 |
|
| 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 | + |
113 | 128 | def init(): |
114 | 129 | """ Initializes parser state |
115 | 130 | """ |
@@ -150,7 +165,7 @@ def init(): |
150 | 165 |
|
151 | 166 |
|
152 | 167 | # ---------------------------------------------------------------------- |
153 | | -# "Macro" functions. Just return more complex expresions |
| 168 | +# "Macro" functions. Just return more complex expressions |
154 | 169 | # ---------------------------------------------------------------------- |
155 | 170 | def _TYPE(type_): |
156 | 171 | """ returns an internal type converted to a SYMBOL_TABLE |
@@ -391,7 +406,7 @@ def make_call(id_, lineno, args): |
391 | 406 | return make_func_call(id_, lineno, args) |
392 | 407 |
|
393 | 408 |
|
394 | | -def make_param_decl(id_, lineno, typedef, is_array=False): |
| 409 | +def make_param_decl(id_: str, lineno: int, typedef, is_array=False): |
395 | 410 | """ Wrapper that creates a param declaration |
396 | 411 | """ |
397 | 412 | return SYMBOL_TABLE.declare_param(id_, lineno, typedef, is_array) |
@@ -714,18 +729,24 @@ def p_var_decl_ini(p): |
714 | 729 | p[0] = make_sentence('LET', SYMBOL_TABLE.access_var(p[2][0][0], p.lineno(1)), value) |
715 | 730 |
|
716 | 731 |
|
| 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 | + |
717 | 739 | def p_idlist_id(p): |
718 | | - """ idlist : ID |
719 | | - | ARRAY_ID |
| 740 | + """ idlist : singleid |
720 | 741 | """ |
721 | | - p[0] = [(p[1], p.lineno(1))] |
| 742 | + p[0] = [p[1]] |
722 | 743 |
|
723 | 744 |
|
724 | 745 | def p_idlist_idlist_id(p): |
725 | | - """ idlist : idlist COMMA ID |
726 | | - | idlist COMMA ARRAY_ID |
| 746 | + """ idlist : idlist COMMA singleid |
727 | 747 | """ |
728 | | - p[0] = p[1] + [(p[3], p.lineno(3))] |
| 748 | + p[1].append(p[3]) |
| 749 | + p[0] = p[1] |
729 | 750 |
|
730 | 751 |
|
731 | 752 | def p_arr_decl(p): |
@@ -2593,10 +2614,10 @@ def p_id_expr(p): |
2593 | 2614 |
|
2594 | 2615 |
|
2595 | 2616 | def p_addr_of_id(p): |
2596 | | - """ bexpr : ADDRESSOF ID |
2597 | | - | ADDRESSOF ARRAY_ID |
| 2617 | + """ bexpr : ADDRESSOF singleid |
2598 | 2618 | """ |
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) |
2600 | 2621 | if entry is None: |
2601 | 2622 | p[0] = None |
2602 | 2623 | return |
@@ -3030,27 +3051,29 @@ def p_param_definition(p): |
3030 | 3051 |
|
3031 | 3052 |
|
3032 | 3053 | 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 |
3035 | 3055 | """ |
3036 | 3056 | typeref = p[4] |
3037 | 3057 | if typeref is None: |
3038 | 3058 | p[0] = None |
3039 | 3059 | return |
3040 | 3060 |
|
3041 | | - lineno = p.lineno(1) |
3042 | | - id_ = p[1] |
| 3061 | + lineno = p[1].lineno |
| 3062 | + id_ = p[1].name |
3043 | 3063 |
|
3044 | 3064 | api.check.check_type_is_explicit(lineno, id_, typeref) |
3045 | 3065 | p[0] = make_param_decl(id_, lineno, typeref, is_array=True) |
3046 | 3066 |
|
3047 | 3067 |
|
3048 | 3068 | def p_param_def_type(p): |
3049 | | - """ param_def : ID typedef |
| 3069 | + """ param_def : singleid typedef |
3050 | 3070 | """ |
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) |
3054 | 3077 |
|
3055 | 3078 |
|
3056 | 3079 | def p_function_body(p): |
@@ -3473,7 +3496,7 @@ def p_error(p): |
3473 | 3496 | # ---------------------------------------- |
3474 | 3497 | # Initialization |
3475 | 3498 | # ---------------------------------------- |
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)) |
3477 | 3500 |
|
3478 | 3501 | ast = None |
3479 | 3502 | data_ast = None # Global Variables AST |
|
0 commit comments