Identifier fields

This commit is contained in:
2026-04-17 04:06:29 +02:00
parent 41f47f3241
commit d0b7898ad7
2 changed files with 20 additions and 7 deletions
+14 -5
View File
@@ -84,8 +84,8 @@ impl LocalScope {
pub fn walk_postfixes( pub fn walk_postfixes(
self: &Arc<Self>, self: &Arc<Self>,
initial: &Box<ast::Expression>, initial: &mut Box<ast::Expression>,
postfixes: &Vec<Postfix>, postfixes: &mut Vec<Postfix>,
) -> Option<Arc<TypeRef>> { ) -> Option<Arc<TypeRef>> {
let mut current_type = self.get_type_from_expr(initial)?; let mut current_type = self.get_type_from_expr(initial)?;
@@ -93,7 +93,9 @@ impl LocalScope {
match postfix { match postfix {
Postfix::FieldAccess(id) => match &*current_type { Postfix::FieldAccess(id) => match &*current_type {
TypeRef::Struct(s) => { TypeRef::Struct(s) => {
current_type = s.fields.get(id)?.var_type.clone(); let field = s.fields.get(id)?;
*id = field.name.clone();
current_type = field.var_type.clone();
} }
_ => unimplemented!(), _ => unimplemented!(),
}, },
@@ -111,7 +113,10 @@ impl LocalScope {
Some(current_type) Some(current_type)
} }
pub fn get_type_from_expr(self: &Arc<Self>, expr: &ast::Expression) -> Option<Arc<TypeRef>> { pub fn get_type_from_expr(
self: &Arc<Self>,
expr: &mut ast::Expression,
) -> Option<Arc<TypeRef>> {
match expr { match expr {
ast::Expression::IntLiteral(_) => self ast::Expression::IntLiteral(_) => self
.parent .parent
@@ -133,7 +138,11 @@ impl LocalScope {
.get_reference(&"string".to_string()) .get_reference(&"string".to_string())
.map(|r| r.var_type.clone()), .map(|r| r.var_type.clone()),
ast::Expression::Identifier(id) => self.get_reference(id).map(|r| r.var_type.clone()), ast::Expression::Identifier(id) => {
let rf = self.get_reference(id)?;
*id = rf.name.clone();
Some(rf.var_type.clone())
}
ast::Expression::Postfix { initial, postfixes } => { ast::Expression::Postfix { initial, postfixes } => {
self.walk_postfixes(initial, postfixes) self.walk_postfixes(initial, postfixes)
+6 -2
View File
@@ -1,5 +1,9 @@
function test(): int { } struct MyType {
x: int,
}
function test(): MyType { }
function add() { function add() {
let output = test(); let output = test().x;
} }