Variable indexing

This commit is contained in:
2026-04-16 10:12:49 +02:00
parent 3e1fa5cd18
commit 2d46afff9a
2 changed files with 20 additions and 1 deletions
+13 -1
View File
@@ -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
}
} }
+7
View File
@@ -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)]