Variable decleration
This commit is contained in:
+27
-2
@@ -3,7 +3,7 @@ use std::{
|
|||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
|
|
||||||
use parser::ast::{ParamList, Statement};
|
use parser::ast::{self, ParamList, Statement};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
hir::{FunctionRef, TopLevelHirScope, TypeRef, VarRef},
|
hir::{FunctionRef, TopLevelHirScope, TypeRef, VarRef},
|
||||||
@@ -61,12 +61,37 @@ impl LocalScope {
|
|||||||
for statement in &mut block.0 {
|
for statement in &mut block.0 {
|
||||||
match statement {
|
match statement {
|
||||||
Statement::Block(b) => self.clone().with_block(b),
|
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) {
|
pub fn with_params(self: &Arc<Self>, param_list: &ParamList) {
|
||||||
for (param_name, type_expr) in ¶m_list.0 {
|
for (param_name, type_expr) in ¶m_list.0 {
|
||||||
match type_expr {
|
match type_expr {
|
||||||
|
|||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
function add(a: int, b: int) {
|
function add(a: int, b: int) {
|
||||||
let a = 10;
|
let myVar = 10;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user