diff --git a/semantic/src/hir.rs b/semantic/src/hir.rs index b373688..40a5235 100644 --- a/semantic/src/hir.rs +++ b/semantic/src/hir.rs @@ -1,13 +1,11 @@ use std::{collections::HashMap, sync::Arc}; -use crate::{ - scope::Reference, - top_level::{FunctionSymbol, StructSymbol, TopLevelSymbolScope, TypeSymbol, VarSymbol}, -}; +use crate::top_level::{FunctionSymbol, StructSymbol, TopLevelSymbolScope, TypeSymbol, VarSymbol}; #[derive(Clone, Debug)] pub enum TypeRef { Struct(StructRef), + Function(FunctionRef), Int, } @@ -35,15 +33,13 @@ pub struct StructRef { #[derive(Debug)] pub struct TopLevelHirScope { - pub types: HashMap>, - pub functions: HashMap>, + pub variables: HashMap>, } impl TopLevelHirScope { pub fn from_tlss(tlss: &TopLevelSymbolScope) -> Self { let mut scope = Self { - types: HashMap::new(), - functions: HashMap::new(), + variables: HashMap::new(), }; for (_, symbol) in &tlss.functions { @@ -57,16 +53,10 @@ impl TopLevelHirScope { scope } - pub fn function_ref( - &mut self, - tlss: &TopLevelSymbolScope, - symbol: &FunctionSymbol, - ) -> Arc { - if let Some(rf) = self.functions.get(&symbol.name) { - rf.clone() - } else { + pub fn function_ref(&mut self, tlss: &TopLevelSymbolScope, symbol: &FunctionSymbol) { + if self.variables.get(&symbol.name).is_none() { if let Some(_) = tlss.functions.get(&symbol.name) { - let rf = Arc::new(FunctionRef { + let rf = FunctionRef { export: symbol.export, name: symbol.name.clone(), params: symbol @@ -78,11 +68,15 @@ impl TopLevelHirScope { .return_type .clone() .map(|rt| self.type_ref(tlss, &rt)), - }); + }; - self.functions.insert(symbol.name.clone(), rf.clone()); - - rf + self.variables.insert( + symbol.name.clone(), + Arc::new(VarRef { + name: symbol.name.clone(), + var_type: Arc::new(TypeRef::Function(rf)), + }), + ); } else { unimplemented!() } @@ -105,7 +99,13 @@ impl TopLevelHirScope { methods: HashMap::new(), })); - self.types.insert(symbol.name.clone(), rf.clone()); + self.variables.insert( + symbol.name.clone(), + Arc::new(VarRef { + name: symbol.name.clone(), + var_type: rf.clone(), + }), + ); rf } @@ -118,41 +118,37 @@ impl TopLevelHirScope { } pub fn type_ref(&mut self, tlss: &TopLevelSymbolScope, symbol: &TypeSymbol) -> Arc { - if let Some(rf) = self.types.get(&symbol.0) { - rf.clone() + if let Some(var_ref) = self.variables.get(&symbol.0) { + var_ref.var_type.clone() } else { if let Some(tlss_rf) = tlss.structs.get(&symbol.0) { - let rf = Arc::new(TypeRef::Struct(StructRef { - export: tlss_rf.export, - name: tlss_rf.name.clone(), - fields: tlss_rf - .fields - .iter() - .map(|(name, v)| { - ( - name.clone(), - Arc::new(VarRef { - name: name.clone(), - var_type: self.type_ref(tlss, &v.var_type), - }), - ) - }) - .collect(), - methods: HashMap::new(), - })); + let struct_ref = self.struct_ref(tlss, tlss_rf); - self.types.insert(symbol.0.clone(), rf.clone()); - - rf + self.variables + .insert( + symbol.0.clone(), + Arc::new(VarRef { + name: symbol.0.clone(), + var_type: struct_ref, + }), + ) + .unwrap() + .var_type + .clone() } else { match symbol.0.as_str() { - "int" => { - let rf = Arc::new(TypeRef::Int); - - self.types.insert(symbol.0.clone(), rf.clone()); - - rf - } + "int" => self + .variables + .insert( + symbol.0.clone(), + Arc::new(VarRef { + name: symbol.0.clone(), + var_type: Arc::new(TypeRef::Int), + }), + ) + .unwrap() + .var_type + .clone(), _ => { unimplemented!("{:?}", symbol) @@ -162,13 +158,7 @@ impl TopLevelHirScope { } } - pub fn get_reference(&self, name: &String) -> Option { - if let Some(r) = self.functions.get(name) { - Some(Reference::Func(r.clone())) - } else if let Some(r) = self.types.get(name) { - Some(Reference::Type(r.clone())) - } else { - None - } + pub fn get_reference(&self, name: &String) -> Option<&Arc> { + self.variables.get(name) } }