Refrencing and LocalScope
This commit is contained in:
+14
-1
@@ -1,6 +1,9 @@
|
|||||||
use std::{collections::HashMap, sync::Arc};
|
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)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum TypeRef {
|
pub enum TypeRef {
|
||||||
@@ -158,4 +161,14 @@ impl TopLevelHirScope {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_refrence(&self, name: &String) -> Option<Refrence> {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+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)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Scope {
|
pub enum Scope {
|
||||||
TopLevel(TopLevelHirScope),
|
TopLevel(TopLevelHirScope),
|
||||||
|
Local(LocalScope),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Scope {
|
impl Scope {
|
||||||
@@ -12,4 +23,26 @@ impl Scope {
|
|||||||
let tl = TopLevelSymbolScope::from(top_level);
|
let tl = TopLevelSymbolScope::from(top_level);
|
||||||
Arc::new(Self::TopLevel(TopLevelHirScope::from_tlss(&tl)))
|
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(),
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-19
@@ -1,21 +1,6 @@
|
|||||||
function add(a: int, b: int): int {
|
struct AddParams {
|
||||||
let sum = 0;
|
x: int,
|
||||||
|
y: int,
|
||||||
for (let i = 0; i < 10; i = i + 1) {
|
|
||||||
if (i == 5) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
sum = sum + i;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sum > 20) {
|
|
||||||
return sum;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function main() {
|
function add(p: AddParams) {}
|
||||||
add(10, 10);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user