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>, pub methods: HashMap<String, FunctionRef>,
} }
#[derive(Clone, Debug)] #[derive(Debug)]
pub struct TopLevelHirScope { pub struct TopLevelHirScope {
pub types: HashMap<String, Arc<TypeRef>>, pub types: HashMap<String, Arc<TypeRef>>,
pub functions: HashMap<String, Arc<FunctionRef>>, 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::{ use crate::{
hir::{FunctionRef, TopLevelHirScope, TypeRef, VarRef}, hir::{FunctionRef, TopLevelHirScope, TypeRef, VarRef},
@@ -12,7 +17,7 @@ pub enum Refrence {
Func(Arc<FunctionRef>), Func(Arc<FunctionRef>),
} }
#[derive(Clone, Debug)] #[derive(Debug)]
pub enum Scope { pub enum Scope {
TopLevel(TopLevelHirScope), TopLevel(TopLevelHirScope),
Local(LocalScope), Local(LocalScope),
@@ -27,22 +32,66 @@ impl Scope {
pub fn get_refrence(&self, name: &String) -> Option<Refrence> { pub fn get_refrence(&self, name: &String) -> Option<Refrence> {
match self { match self {
Scope::TopLevel(tl) => tl.get_refrence(name), 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 struct LocalScope {
pub parent: Arc<Scope>, pub parent: Arc<Scope>,
pub variables: HashMap<String, Arc<VarRef>>, pub variables: Mutex<HashMap<String, Arc<VarRef>>>,
} }
impl LocalScope { impl LocalScope {
pub fn from_block(parent: Arc<Scope>) -> Arc<Self> { pub fn new(parent: Arc<Scope>) -> Arc<Self> {
Arc::new(LocalScope { Arc::new(LocalScope {
parent, 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,
} => {
}
}
}
} }
+6 -2
View File
@@ -2,6 +2,8 @@ use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use std::process; use std::process;
use semantic::scope::walk_ast;
fn main() { fn main() {
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = std::env::args().collect();
@@ -53,10 +55,12 @@ pub fn cmd_build() {
fn cmd_check(path: &str) { fn cmd_check(path: &str) {
let source = read_ms_file(path); let source = read_ms_file(path);
match parser::parse(&source) { match parser::parse(&source) {
Ok(ast) => { Ok(mut ast) => {
println!("parse: ok"); println!("parse: ok");
println!("{:#?}", semantic::scope::Scope::from_top(&ast)) walk_ast(semantic::scope::Scope::from_top(&ast), &mut ast);
println!("{:#?}", ast)
} }
Err(e) => { Err(e) => {
eprintln!("parse error:\n{}", e); eprintln!("parse error:\n{}", e);