Variable indexing
This commit is contained in:
+13
-1
@@ -1,4 +1,10 @@
|
|||||||
use std::{collections::HashMap, sync::Arc};
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
sync::{
|
||||||
|
Arc,
|
||||||
|
atomic::{AtomicUsize, Ordering},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
use crate::top_level::{FunctionSymbol, StructSymbol, TopLevelSymbolScope, TypeSymbol, VarSymbol};
|
use crate::top_level::{FunctionSymbol, StructSymbol, TopLevelSymbolScope, TypeSymbol, VarSymbol};
|
||||||
|
|
||||||
@@ -34,12 +40,14 @@ pub struct StructRef {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TopLevelHirScope {
|
pub struct TopLevelHirScope {
|
||||||
pub variables: HashMap<String, Arc<VarRef>>,
|
pub variables: HashMap<String, Arc<VarRef>>,
|
||||||
|
pub var_idx: AtomicUsize,
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
variables: HashMap::new(),
|
variables: HashMap::new(),
|
||||||
|
var_idx: AtomicUsize::new(0),
|
||||||
};
|
};
|
||||||
|
|
||||||
for (_, symbol) in &tlss.functions {
|
for (_, symbol) in &tlss.functions {
|
||||||
@@ -155,4 +163,8 @@ impl TopLevelHirScope {
|
|||||||
pub fn get_reference(&self, name: &String) -> Option<Arc<VarRef>> {
|
pub fn get_reference(&self, name: &String) -> Option<Arc<VarRef>> {
|
||||||
self.variables.get(name).cloned()
|
self.variables.get(name).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn next_var_idx(&self) -> usize {
|
||||||
|
self.var_idx.fetch_add(1, Ordering::Relaxed) + 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,13 @@ impl Scope {
|
|||||||
Scope::Local(l) => l.get_reference(name),
|
Scope::Local(l) => l.get_reference(name),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn next_var_idx(&self) -> usize {
|
||||||
|
match self {
|
||||||
|
Scope::TopLevel(tl) => tl.next_var_idx(),
|
||||||
|
Scope::Local(l) => l.parent.next_var_idx(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|||||||
Reference in New Issue
Block a user