Refrencing and LocalScope
This commit is contained in:
+35
-2
@@ -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<TypeRef>),
|
||||
Var(Arc<VarRef>),
|
||||
Func(Arc<FunctionRef>),
|
||||
}
|
||||
|
||||
#[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<Refrence> {
|
||||
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<Scope>,
|
||||
pub variables: HashMap<String, Arc<VarRef>>,
|
||||
}
|
||||
|
||||
impl LocalScope {
|
||||
pub fn from_block(parent: Arc<Scope>) -> Arc<Self> {
|
||||
Arc::new(LocalScope {
|
||||
parent,
|
||||
variables: HashMap::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user