Walking ast
This commit is contained in:
+1
-1
@@ -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
@@ -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,
|
||||
} => {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-2
@@ -2,6 +2,8 @@ use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
|
||||
use semantic::scope::walk_ast;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = 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);
|
||||
|
||||
Reference in New Issue
Block a user