Walking ast

This commit is contained in:
2026-04-15 11:47:36 +02:00
parent cb03653e88
commit 0d63528d10
3 changed files with 63 additions and 10 deletions
+1 -1
View File
@@ -33,7 +33,7 @@ pub struct StructRef {
pub methods: HashMap<String, FunctionRef>,
}
#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct TopLevelHirScope {
pub types: HashMap<String, Arc<TypeRef>>,
pub functions: HashMap<String, Arc<FunctionRef>>,
+56 -7
View File
@@ -1,4 +1,9 @@
use std::{collections::HashMap, sync::Arc};
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use parser::ast::Statement;
use crate::{
hir::{FunctionRef, TopLevelHirScope, TypeRef, VarRef},
@@ -12,7 +17,7 @@ pub enum Refrence {
Func(Arc<FunctionRef>),
}
#[derive(Clone, Debug)]
#[derive(Debug)]
pub enum Scope {
TopLevel(TopLevelHirScope),
Local(LocalScope),
@@ -27,22 +32,66 @@ impl Scope {
pub fn get_refrence(&self, name: &String) -> Option<Refrence> {
match self {
Scope::TopLevel(tl) => tl.get_refrence(name),
Scope::Local(l) => l.variables.get(name).cloned().map(Refrence::Var),
Scope::Local(l) => l
.variables
.lock()
.unwrap()
.get(name)
.cloned()
.map(Refrence::Var),
}
}
}
#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct LocalScope {
pub parent: Arc<Scope>,
pub variables: HashMap<String, Arc<VarRef>>,
pub variables: Mutex<HashMap<String, Arc<VarRef>>>,
}
impl LocalScope {
pub fn from_block(parent: Arc<Scope>) -> Arc<Self> {
pub fn new(parent: Arc<Scope>) -> Arc<Self> {
Arc::new(LocalScope {
parent,
variables: HashMap::new(),
variables: Mutex::new(HashMap::new()),
})
}
pub fn with_block(self: &Arc<Self>, block: &mut parser::ast::Block) {
for statement in &mut block.0 {
match statement {
Statement::Block(b) => self.clone().with_block(b),
_ => {}
}
}
}
}
pub fn walk_ast(top_scope: Arc<Scope>, tl: &mut Vec<parser::ast::TopLevel>) {
for tl in tl {
match tl {
parser::ast::TopLevel::Import(_) => unimplemented!(),
parser::ast::TopLevel::FunctionDecl {
export,
name,
params,
return_type,
body,
} => {
let scope = LocalScope::new(top_scope.clone());
scope.with_block(body);
println!("{:?}", scope);
}
parser::ast::TopLevel::StructDecl {
export,
name,
fields,
} => {
}
}
}
}