diff --git a/semantic/src/hir.rs b/semantic/src/hir.rs index d1f94fe..bf6da89 100644 --- a/semantic/src/hir.rs +++ b/semantic/src/hir.rs @@ -33,7 +33,7 @@ pub struct StructRef { pub methods: HashMap, } -#[derive(Clone, Debug)] +#[derive(Debug)] pub struct TopLevelHirScope { pub types: HashMap>, pub functions: HashMap>, diff --git a/semantic/src/scope.rs b/semantic/src/scope.rs index 6e00430..7c5e8cb 100644 --- a/semantic/src/scope.rs +++ b/semantic/src/scope.rs @@ -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), } -#[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 { 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, - pub variables: HashMap>, + pub variables: Mutex>>, } impl LocalScope { - pub fn from_block(parent: Arc) -> Arc { + pub fn new(parent: Arc) -> Arc { Arc::new(LocalScope { parent, - variables: HashMap::new(), + variables: Mutex::new(HashMap::new()), }) } + + pub fn with_block(self: &Arc, 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, tl: &mut Vec) { + 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, + } => { + + } + } + } } diff --git a/src/main.rs b/src/main.rs index bff6a1f..fc20c5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ use std::fs; use std::path::PathBuf; use std::process; +use semantic::scope::walk_ast; + fn main() { let args: Vec = std::env::args().collect(); @@ -53,10 +55,12 @@ pub fn cmd_build() { fn cmd_check(path: &str) { let source = read_ms_file(path); match parser::parse(&source) { - Ok(ast) => { + Ok(mut ast) => { println!("parse: ok"); - println!("{:#?}", semantic::scope::Scope::from_top(&ast)) + walk_ast(semantic::scope::Scope::from_top(&ast), &mut ast); + + println!("{:#?}", ast) } Err(e) => { eprintln!("parse error:\n{}", e);