diff --git a/semantic/src/hir.rs b/semantic/src/hir.rs index f22e58c..d1f94fe 100644 --- a/semantic/src/hir.rs +++ b/semantic/src/hir.rs @@ -1,6 +1,9 @@ use std::{collections::HashMap, sync::Arc}; -use crate::top_level::{FunctionSymbol, StructSymbol, TopLevelSymbolScope, TypeSymbol, VarSymbol}; +use crate::{ + scope::Refrence, + top_level::{FunctionSymbol, StructSymbol, TopLevelSymbolScope, TypeSymbol, VarSymbol}, +}; #[derive(Clone, Debug)] pub enum TypeRef { @@ -158,4 +161,14 @@ impl TopLevelHirScope { } } } + + pub fn get_refrence(&self, name: &String) -> Option { + if let Some(r) = self.functions.get(name) { + Some(Refrence::Func(r.clone())) + } else if let Some(r) = self.types.get(name) { + Some(Refrence::Type(r.clone())) + } else { + None + } + } } diff --git a/semantic/src/scope.rs b/semantic/src/scope.rs index da087e6..6e00430 100644 --- a/semantic/src/scope.rs +++ b/semantic/src/scope.rs @@ -1,10 +1,21 @@ -use std::sync::Arc; +use std::{collections::HashMap, sync::Arc}; -use crate::{hir::TopLevelHirScope, top_level::TopLevelSymbolScope}; +use crate::{ + hir::{FunctionRef, TopLevelHirScope, TypeRef, VarRef}, + top_level::TopLevelSymbolScope, +}; + +#[derive(Clone, Debug)] +pub enum Refrence { + Type(Arc), + Var(Arc), + Func(Arc), +} #[derive(Clone, Debug)] pub enum Scope { TopLevel(TopLevelHirScope), + Local(LocalScope), } impl Scope { @@ -12,4 +23,26 @@ impl Scope { let tl = TopLevelSymbolScope::from(top_level); Arc::new(Self::TopLevel(TopLevelHirScope::from_tlss(&tl))) } + + 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), + } + } +} + +#[derive(Clone, Debug)] +pub struct LocalScope { + pub parent: Arc, + pub variables: HashMap>, +} + +impl LocalScope { + pub fn from_block(parent: Arc) -> Arc { + Arc::new(LocalScope { + parent, + variables: HashMap::new(), + }) + } } diff --git a/test/src/main.ms b/test/src/main.ms index eaed9a5..bede08a 100644 --- a/test/src/main.ms +++ b/test/src/main.ms @@ -1,21 +1,6 @@ -function add(a: int, b: int): int { - let sum = 0; - - for (let i = 0; i < 10; i = i + 1) { - if (i == 5) { - continue; - } - - sum = sum + i; - } - - if (sum > 20) { - return sum; - } else { - return 0; - } +struct AddParams { + x: int, + y: int, } -function main() { - add(10, 10); -} +function add(p: AddParams) {}