Fix type values
This commit is contained in:
+26
-2
@@ -90,9 +90,17 @@ impl LocalScope {
|
|||||||
pub fn with_statement(self: &Arc<Self>, statement: &mut Statement) {
|
pub fn with_statement(self: &Arc<Self>, statement: &mut Statement) {
|
||||||
match statement {
|
match statement {
|
||||||
Statement::Block(b) => self.clone().with_block(b),
|
Statement::Block(b) => self.clone().with_block(b),
|
||||||
Statement::VarDecl { name, init, .. } => {
|
Statement::VarDecl {
|
||||||
|
name, init, type_, ..
|
||||||
|
} => {
|
||||||
if let Some(init) = init {
|
if let Some(init) = init {
|
||||||
let var_type = self.get_type_from_expr(init).unwrap();
|
let init_type = self.get_type_from_expr(init);
|
||||||
|
let var_type = type_
|
||||||
|
.as_mut()
|
||||||
|
.map(|t| self.get_type_from_type_expr(t))
|
||||||
|
.or_else(|| Some(init_type))
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let var_name = name.clone();
|
let var_name = name.clone();
|
||||||
|
|
||||||
@@ -193,6 +201,22 @@ impl LocalScope {
|
|||||||
Some(current_type)
|
Some(current_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_type_from_type_expr(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
expr: &mut ast::TypeExpr,
|
||||||
|
) -> Option<Arc<TypeRef>> {
|
||||||
|
match expr {
|
||||||
|
ast::TypeExpr::Identifier(id) => {
|
||||||
|
if let Some(v) = self.get_type_reference(id).map(|r| r.var_type.clone()) {
|
||||||
|
*id = v.get_name();
|
||||||
|
Some(v)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_type_from_expr(
|
pub fn get_type_from_expr(
|
||||||
self: &Arc<Self>,
|
self: &Arc<Self>,
|
||||||
expr: &mut ast::Expression,
|
expr: &mut ast::Expression,
|
||||||
|
|||||||
+7
-1
@@ -1,5 +1,11 @@
|
|||||||
package main;
|
package main;
|
||||||
|
|
||||||
|
public struct Idk {
|
||||||
|
int a,
|
||||||
|
int b,
|
||||||
|
}
|
||||||
|
|
||||||
int test(int a, int b) {
|
int test(int a, int b) {
|
||||||
return a + b;
|
Idk bro = 10;
|
||||||
|
return a + b + bro.a;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user