Skip to content

Commit b068c4a

Browse files
committed
Refactor Builder and SymbolTable to integrate SymbolType; update UzumakiExpression handling in WatEmitter
1 parent e557599 commit b068c4a

File tree

5 files changed

+278
-75
lines changed

5 files changed

+278
-75
lines changed

ast/src/builder.rs

Lines changed: 134 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<'a> Builder<'a, InitState> {
102102
}
103103
res.push(ast);
104104
}
105-
let symbol_table = SymbolTable::build(self.types.clone(), &res);
105+
let symbol_table = SymbolTable::build(&res, &self.types, &self.arena);
106106
let t_ast = TypedAst::new(res, symbol_table, self.arena.clone());
107107
Ok(Builder {
108108
arena: Arena::default(),
@@ -133,9 +133,11 @@ impl<'a> Builder<'a, InitState> {
133133
.clone(),
134134
);
135135
} else {
136-
let founded_segments = node
137-
.children_by_field_name("segment", &mut cursor)
138-
.map(|segment| self.build_identifier(id, &segment, code));
136+
let founded_segments =
137+
node.children_by_field_name("segment", &mut cursor)
138+
.map(|segment| {
139+
self.build_identifier(id, &segment, code, Some(SymbolType::Untyped))
140+
});
139141
let founded_segments: Vec<Rc<Identifier>> = founded_segments.collect();
140142
if !founded_segments.is_empty() {
141143
segments = Some(founded_segments);
@@ -145,7 +147,11 @@ impl<'a> Builder<'a, InitState> {
145147
cursor = node.walk();
146148
let founded_imported_types = node
147149
.children_by_field_name("imported_type", &mut cursor)
148-
.map(|imported_type| self.build_identifier(id, &imported_type, code));
150+
.map(|imported_type| {
151+
let t = self.build_identifier(id, &imported_type, code, Some(SymbolType::Untyped));
152+
self.types.push(SymbolType::Global(t.name.clone()));
153+
t
154+
});
149155
let founded_imported_types: Vec<Rc<Identifier>> = founded_imported_types.collect();
150156
if !founded_imported_types.is_empty() {
151157
imported_types = Some(founded_imported_types);
@@ -171,7 +177,12 @@ impl<'a> Builder<'a, InitState> {
171177
) -> Rc<SpecDefinition> {
172178
let id = Self::get_node_id();
173179
let location = Self::get_location(node, code);
174-
let name = self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code);
180+
let name = self.build_identifier(
181+
id,
182+
&node.child_by_field_name("name").unwrap(),
183+
code,
184+
Some(SymbolType::Untyped),
185+
);
175186
let mut definitions = Vec::new();
176187

177188
for i in 0..node.child_count() {
@@ -194,13 +205,25 @@ impl<'a> Builder<'a, InitState> {
194205
) -> Rc<EnumDefinition> {
195206
let id = Self::get_node_id();
196207
let location = Self::get_location(node, code);
197-
let name = self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code);
208+
let name = self.build_identifier(
209+
id,
210+
&node.child_by_field_name("name").unwrap(),
211+
code,
212+
Some(SymbolType::Untyped),
213+
);
198214
let mut variants = Vec::new();
199215

200216
let mut cursor = node.walk();
201217
let founded_variants = node
202218
.children_by_field_name("variant", &mut cursor)
203-
.map(|segment| self.build_identifier(id, &segment, code));
219+
.map(|segment| {
220+
self.build_identifier(
221+
id,
222+
&segment,
223+
code,
224+
Some(SymbolType::Global(name.name.clone())), //TODO revisit
225+
)
226+
});
204227
let founded_variants: Vec<Rc<Identifier>> = founded_variants.collect();
205228
if !founded_variants.is_empty() {
206229
variants = founded_variants;
@@ -254,8 +277,12 @@ impl<'a> Builder<'a, InitState> {
254277
) -> Rc<StructDefinition> {
255278
let id = Self::get_node_id();
256279
let location = Self::get_location(node, code);
257-
let name =
258-
self.build_identifier(id, &node.child_by_field_name("struct_name").unwrap(), code);
280+
let name = self.build_identifier(
281+
id,
282+
&node.child_by_field_name("struct_name").unwrap(),
283+
code,
284+
Some(SymbolType::Untyped),
285+
);
259286
let mut fields = Vec::new();
260287
let mut cursor = node.walk();
261288
let founded_fields = node
@@ -280,10 +307,15 @@ impl<'a> Builder<'a, InitState> {
280307
fn build_struct_field(&mut self, parent_id: u32, node: &Node, code: &[u8]) -> Rc<StructField> {
281308
let id = Self::get_node_id();
282309
let location = Self::get_location(node, code);
283-
let name = self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code);
284-
let type_ = self.build_type(id, &node.child_by_field_name("type").unwrap(), code);
310+
let ty = self.build_type(id, &node.child_by_field_name("type").unwrap(), code);
311+
let name = self.build_identifier(
312+
id,
313+
&node.child_by_field_name("name").unwrap(),
314+
code,
315+
Some(Self::symbol_type_from_ast_type(ty.clone())),
316+
);
285317

286-
let node = Rc::new(StructField::new(id, name, type_, location));
318+
let node = Rc::new(StructField::new(id, name, ty, location));
287319
self.arena
288320
.add_node(AstNode::StructField(node.clone()), parent_id);
289321
node
@@ -297,8 +329,13 @@ impl<'a> Builder<'a, InitState> {
297329
) -> Rc<ConstantDefinition> {
298330
let id = Self::get_node_id();
299331
let location = Self::get_location(node, code);
300-
let name = self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code);
301332
let ty = self.build_type(id, &node.child_by_field_name("type").unwrap(), code);
333+
let name = self.build_identifier(
334+
id,
335+
&node.child_by_field_name("name").unwrap(),
336+
code,
337+
Some(Self::symbol_type_from_ast_type(ty.clone())),
338+
);
302339
let value = self.build_literal(
303340
id,
304341
&node.child_by_field_name("value").unwrap(),
@@ -320,7 +357,6 @@ impl<'a> Builder<'a, InitState> {
320357
) -> Rc<FunctionDefinition> {
321358
let id = Self::get_node_id();
322359
let location = Self::get_location(node, code);
323-
let name = self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code);
324360
let mut arguments = None;
325361
let mut returns = None;
326362

@@ -338,6 +374,12 @@ impl<'a> Builder<'a, InitState> {
338374
if let Some(returns_node) = node.child_by_field_name("returns") {
339375
returns = Some(self.build_type(id, &returns_node, code));
340376
}
377+
let name = self.build_identifier(
378+
id,
379+
&node.child_by_field_name("name").unwrap(),
380+
code,
381+
returns.clone().map(Self::symbol_type_from_ast_type),
382+
);
341383
let body_node = node.child_by_field_name("body").unwrap();
342384
let body = self.build_block(id, &body_node, code);
343385
let node = Rc::new(FunctionDefinition::new(
@@ -356,15 +398,16 @@ impl<'a> Builder<'a, InitState> {
356398
) -> Rc<ExternalFunctionDefinition> {
357399
let id = Self::get_node_id();
358400
let location = Self::get_location(node, code);
359-
let name = self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code);
401+
let name =
402+
self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code, None);
360403
let mut arguments = None;
361404
let mut returns = None;
362405

363406
let mut cursor = node.walk();
364407

365408
let founded_arguments = node
366409
.children_by_field_name("argument", &mut cursor)
367-
.map(|segment| self.build_identifier(id, &segment, code));
410+
.map(|segment| self.build_identifier(id, &segment, code, None));
368411
let founded_arguments: Vec<Rc<Identifier>> = founded_arguments.collect();
369412
if !founded_arguments.is_empty() {
370413
arguments = Some(founded_arguments);
@@ -390,10 +433,14 @@ impl<'a> Builder<'a, InitState> {
390433
) -> Rc<TypeDefinition> {
391434
let id = Self::get_node_id();
392435
let location = Self::get_location(node, code);
393-
let name = self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code);
394-
let type_ = self.build_type(id, &node.child_by_field_name("type").unwrap(), code);
395-
396-
let node = Rc::new(TypeDefinition::new(id, name, type_, location));
436+
let ty = self.build_type(id, &node.child_by_field_name("type").unwrap(), code);
437+
let name = self.build_identifier(
438+
id,
439+
&node.child_by_field_name("name").unwrap(),
440+
code,
441+
Some(Self::symbol_type_from_ast_type(ty.clone())),
442+
);
443+
let node = Rc::new(TypeDefinition::new(id, name, ty, location));
397444
self.arena
398445
.add_node(AstNode::TypeDefinition(node.clone()), parent_id);
399446
node
@@ -403,10 +450,15 @@ impl<'a> Builder<'a, InitState> {
403450
let id = Self::get_node_id();
404451
let location = Self::get_location(node, code);
405452
let name_node = node.child_by_field_name("name").unwrap();
406-
let name = self.build_identifier(id, &name_node, code);
407453
let type_node = node.child_by_field_name("type").unwrap();
408-
let type_ = self.build_type(id, &type_node, code);
409-
let node = Rc::new(Parameter::new(id, location, name, type_));
454+
let ty = self.build_type(id, &type_node, code);
455+
let name = self.build_identifier(
456+
id,
457+
&name_node,
458+
code,
459+
Some(Self::symbol_type_from_ast_type(ty.clone())),
460+
);
461+
let node = Rc::new(Parameter::new(id, location, name, ty));
410462
self.arena
411463
.add_node(AstNode::Parameter(node.clone()), parent_id);
412464
node
@@ -585,7 +637,12 @@ impl<'a> Builder<'a, InitState> {
585637
let id = Self::get_node_id();
586638
let location = Self::get_location(node, code);
587639
let ty = self.build_type(id, &node.child_by_field_name("type").unwrap(), code);
588-
let name = self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code);
640+
let name = self.build_identifier(
641+
id,
642+
&node.child_by_field_name("name").unwrap(),
643+
code,
644+
Some(Self::symbol_type_from_ast_type(ty.clone())),
645+
);
589646
let value = node.child_by_field_name("value").map(|n| {
590647
self.build_expression(
591648
id,
@@ -614,10 +671,15 @@ impl<'a> Builder<'a, InitState> {
614671
) -> Rc<TypeDefinitionStatement> {
615672
let id = Self::get_node_id();
616673
let location = Self::get_location(node, code);
617-
let name = self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code);
618-
let type_ = self.build_type(id, &node.child_by_field_name("type").unwrap(), code);
674+
let ty = self.build_type(id, &node.child_by_field_name("type").unwrap(), code);
675+
let name = self.build_identifier(
676+
id,
677+
&node.child_by_field_name("name").unwrap(),
678+
code,
679+
Some(Self::symbol_type_from_ast_type(ty.clone())),
680+
);
619681

620-
let node = Rc::new(TypeDefinitionStatement::new(id, location, name, type_));
682+
let node = Rc::new(TypeDefinitionStatement::new(id, location, name, ty));
621683
self.arena
622684
.add_node(AstNode::TypeDefinitionStatement(node.clone()), parent_id);
623685
node
@@ -662,7 +724,9 @@ impl<'a> Builder<'a, InitState> {
662724
code,
663725
ty.unwrap(),
664726
)),
665-
"identifier" => Expression::Identifier(self.build_identifier(parent_id, node, code)),
727+
"identifier" => {
728+
Expression::Identifier(self.build_identifier(parent_id, node, code, None))
729+
}
666730
_ => panic!("Unexpected expression node kind: {node_kind}"),
667731
}
668732
}
@@ -717,8 +781,8 @@ impl<'a> Builder<'a, InitState> {
717781
code,
718782
None,
719783
);
720-
let name = self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code);
721-
784+
let name =
785+
self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code, None);
722786
let node = Rc::new(MemberAccessExpression::new(id, location, expression, name));
723787
self.arena
724788
.add_node(AstNode::MemberAccessExpression(node.clone()), parent_id);
@@ -1007,7 +1071,11 @@ impl<'a> Builder<'a, InitState> {
10071071
Type::QualifiedName(self.build_qualified_name(parent_id, node, code))
10081072
}
10091073
"type_fn" => Type::Function(self.build_function_type(parent_id, node, code)),
1010-
"identifier" => Type::Custom(self.build_identifier(parent_id, node, code)),
1074+
"identifier" => {
1075+
let name = self.build_identifier(parent_id, node, code, Some(SymbolType::Untyped));
1076+
self.types.push(SymbolType::Global(name.name.clone()));
1077+
Type::Custom(name)
1078+
}
10111079
_ => {
10121080
let location = Self::get_location(node, code);
10131081
panic!("Unexpected type: {node_kind}, {location}")
@@ -1020,15 +1088,15 @@ impl<'a> Builder<'a, InitState> {
10201088
let location = Self::get_location(node, code);
10211089
let element_type = self.build_type(id, &node.child_by_field_name("type").unwrap(), code);
10221090
let size = node.child_by_field_name("length").map(|n| {
1023-
Box::new(self.build_expression(
1091+
self.build_expression(
10241092
id,
10251093
&n,
10261094
code,
10271095
Some(Self::symbol_type_from_ast_type(element_type.clone())),
1028-
))
1096+
)
10291097
});
10301098

1031-
let node = Rc::new(TypeArray::new(id, location, Box::new(element_type), size));
1099+
let node = Rc::new(TypeArray::new(id, location, element_type, size));
10321100
self.arena
10331101
.add_node(AstNode::TypeArray(node.clone()), parent_id);
10341102
node
@@ -1048,7 +1116,12 @@ impl<'a> Builder<'a, InitState> {
10481116
fn build_generic_type(&mut self, parent_id: u32, node: &Node, code: &[u8]) -> Rc<GenericType> {
10491117
let id = Self::get_node_id();
10501118
let location = Self::get_location(node, code);
1051-
let base = self.build_identifier(id, &node.child_by_field_name("base_type").unwrap(), code);
1119+
let base = self.build_identifier(
1120+
id,
1121+
&node.child_by_field_name("base_type").unwrap(),
1122+
code,
1123+
Some(SymbolType::Untyped),
1124+
);
10521125

10531126
let args = node.child(1).unwrap();
10541127

@@ -1083,10 +1156,7 @@ impl<'a> Builder<'a, InitState> {
10831156
if !founded_arguments.is_empty() {
10841157
arguments = Some(founded_arguments);
10851158
}
1086-
1087-
let returns =
1088-
Box::new(self.build_type(id, &node.child_by_field_name("returns").unwrap(), code));
1089-
1159+
let returns = self.build_type(id, &node.child_by_field_name("returns").unwrap(), code);
10901160
let node = Rc::new(FunctionType::new(id, location, arguments, returns));
10911161
self.arena
10921162
.add_node(AstNode::FunctionType(node.clone()), parent_id);
@@ -1101,8 +1171,10 @@ impl<'a> Builder<'a, InitState> {
11011171
) -> Rc<TypeQualifiedName> {
11021172
let id = Self::get_node_id();
11031173
let location = Self::get_location(node, code);
1104-
let alias = self.build_identifier(id, &node.child_by_field_name("alias").unwrap(), code);
1105-
let name = self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code);
1174+
let alias =
1175+
self.build_identifier(id, &node.child_by_field_name("alias").unwrap(), code, None);
1176+
let name =
1177+
self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code, None);
11061178

11071179
let node = Rc::new(TypeQualifiedName::new(id, location, alias, name));
11081180
self.arena
@@ -1118,9 +1190,18 @@ impl<'a> Builder<'a, InitState> {
11181190
) -> Rc<QualifiedName> {
11191191
let id = Self::get_node_id();
11201192
let location = Self::get_location(node, code);
1121-
let qualifier =
1122-
self.build_identifier(id, &node.child_by_field_name("qualifier").unwrap(), code);
1123-
let name = self.build_identifier(id, &node.child_by_field_name("name").unwrap(), code);
1193+
let qualifier = self.build_identifier(
1194+
id,
1195+
&node.child_by_field_name("qualifier").unwrap(),
1196+
code,
1197+
Some(SymbolType::Untyped),
1198+
);
1199+
let name = self.build_identifier(
1200+
id,
1201+
&node.child_by_field_name("name").unwrap(),
1202+
code,
1203+
Some(SymbolType::Untyped),
1204+
);
11241205

11251206
let node = Rc::new(QualifiedName::new(id, location, qualifier, name));
11261207
self.arena
@@ -1143,11 +1224,17 @@ impl<'a> Builder<'a, InitState> {
11431224
node
11441225
}
11451226

1146-
fn build_identifier(&mut self, parent_id: u32, node: &Node, code: &[u8]) -> Rc<Identifier> {
1227+
fn build_identifier(
1228+
&mut self,
1229+
parent_id: u32,
1230+
node: &Node,
1231+
code: &[u8],
1232+
ty: Option<SymbolType>,
1233+
) -> Rc<Identifier> {
11471234
let id = Self::get_node_id();
11481235
let location = Self::get_location(node, code);
11491236
let name = node.utf8_text(code).unwrap().to_string();
1150-
let node = Rc::new(Identifier::new(id, name, location));
1237+
let node = Rc::new(Identifier::new(id, name, location, ty));
11511238
self.arena
11521239
.add_node(AstNode::Identifier(node.clone()), parent_id);
11531240
node

0 commit comments

Comments
 (0)