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