Variable decleration

This commit is contained in:
2026-04-15 12:36:42 +02:00
parent 04087abbdf
commit 09c7f07c95
2 changed files with 28 additions and 3 deletions
+27 -2
View File
@@ -3,7 +3,7 @@ use std::{
sync::{Arc, Mutex},
};
use parser::ast::{ParamList, Statement};
use parser::ast::{self, ParamList, Statement};
use crate::{
hir::{FunctionRef, TopLevelHirScope, TypeRef, VarRef},
@@ -61,12 +61,37 @@ impl LocalScope {
for statement in &mut block.0 {
match statement {
Statement::Block(b) => self.clone().with_block(b),
Statement::VarDecl { kind, name, init } => unimplemented!(),
Statement::VarDecl { kind, name, init } => {
if let Some(init) = init {
self.variables.lock().unwrap().insert(
name.clone(),
Arc::new(VarRef {
name: name.to_string(),
var_type: self.get_type_from_expr(init).unwrap(),
}),
);
}
}
_ => {}
}
}
}
pub fn get_type_from_expr(self: &Arc<Self>, expr: &ast::Expression) -> Option<Arc<TypeRef>> {
match expr {
ast::Expression::IntLiteral(_) => {
self.parent.get_refrence(&"int".to_string()).map(|r| {
if let Refrence::Type(tr) = &r {
tr.clone()
} else {
unimplemented!()
}
})
}
_ => unimplemented!(),
}
}
pub fn with_params(self: &Arc<Self>, param_list: &ParamList) {
for (param_name, type_expr) in &param_list.0 {
match type_expr {