Skip to content

Commit 47621cf

Browse files
Merge pull request #63 from MarcellPerger1/add-typechecking
Flatten multiple declarations in AST
2 parents ec657fa + 9828da4 commit 47621cf

File tree

5 files changed

+171
-223
lines changed

5 files changed

+171
-223
lines changed

parser/astgen/ast_node.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ class AstDeclNode(AstNode):
100100
name = 'var_decl'
101101
scope: VarDeclScope
102102
type: VarType
103-
decls: list[tuple[AstIdent, AstNode | None]]
103+
ident: AstIdent
104+
value: AstNode | None
104105

105106
def _walk_members(self, fn: WalkerFnT):
106-
self.walk_multiple_objects(fn, (self.decls,))
107+
self.walk_multiple_objects(fn, (self.ident, self.value))
107108

108109

109110
@dataclass

parser/astgen/astgen.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .ast_node import *
99
from .eval_literal import eval_number, eval_string
1010
from .errors import LocatedAstError
11-
from ..common import region_union, RegionUnionArgT, HasRegion
11+
from ..common import region_union, RegionUnionArgT, HasRegion, StrRegion
1212
from ..cst.base_node import AnyNode, Node, Leaf
1313
from ..cst.named_node import NamedLeafCls, NamedNodeCls, NamedSizedNodeCls
1414
from ..cst.nodes import *
@@ -134,14 +134,23 @@ def _walk_smt(self, smt: AnyNode) -> list[AstNode]:
134134
f"the root level.", smt.region)
135135

136136
def _walk_var_decl(self, smt: DeclNode):
137-
decls = [(self._walk_ident(d.ident),
138-
None if d.value is None else self._walk_expr(d.value))
139-
for d in smt.decl_list.decls]
140137
scope = (VarDeclScope.LET if isinstance(smt.decl_scope, DeclScope_Let)
141138
else VarDeclScope.GLOBAL)
142139
tp = (VarType.LIST if isinstance(smt.decl_type, DeclType_List)
143140
else VarType.VARIABLE)
144-
return [AstDeclNode(smt.region, scope, tp, decls)]
141+
# Add the region from the keywords to first decl (to make single-var
142+
# decls a more sensible .region that includes the `let` keyword as well)
143+
extra_region_first = region_union(smt.decl_scope, smt.decl_type)
144+
return [self._walk_single_decl(d, scope, tp,
145+
extra_region_first if i == 0 else None)
146+
for i, d in enumerate(smt.decl_list.decls)]
147+
148+
def _walk_single_decl(self, d: DeclItemNode, scope: VarDeclScope,
149+
tp: VarType, extra_region: StrRegion | None):
150+
return AstDeclNode(
151+
region_union(d.ident, d.value, extra_region),
152+
scope, tp, self._walk_ident(d.ident),
153+
None if d.value is None else self._walk_expr(d.value))
145154

146155
def _walk_assign_left(self, lhs: AnyNode) -> AstNode:
147156
if isinstance(lhs, IdentNode):

parser/common/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class HasRegion:
1616
def region_union(*args: RegionUnionArgT):
1717
regs = []
1818
for loc in args:
19+
if loc is None:
20+
continue
1921
if getattr(loc, 'region', None) is not None: # Duck-type HasRegion
2022
loc = loc.region
2123
if isinstance(loc, StrRegion):

test/test_astgen/.snapshots/test_astgen.txt

Lines changed: 81 additions & 97 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)