From 2d46afff9adaf27fd53306daa7d45c87823c8d40 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Thu, 16 Apr 2026 10:12:49 +0200 Subject: [PATCH] Variable indexing --- semantic/src/hir.rs | 14 +++++++++++++- semantic/src/scope.rs | 7 +++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/semantic/src/hir.rs b/semantic/src/hir.rs index 1905531..fee6355 100644 --- a/semantic/src/hir.rs +++ b/semantic/src/hir.rs @@ -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}; @@ -34,12 +40,14 @@ pub struct StructRef { #[derive(Debug)] pub struct TopLevelHirScope { pub variables: HashMap>, + pub var_idx: AtomicUsize, } impl TopLevelHirScope { pub fn from_tlss(tlss: &TopLevelSymbolScope) -> Self { let mut scope = Self { variables: HashMap::new(), + var_idx: AtomicUsize::new(0), }; for (_, symbol) in &tlss.functions { @@ -155,4 +163,8 @@ impl TopLevelHirScope { pub fn get_reference(&self, name: &String) -> Option> { self.variables.get(name).cloned() } + + pub fn next_var_idx(&self) -> usize { + self.var_idx.fetch_add(1, Ordering::Relaxed) + 1 + } } diff --git a/semantic/src/scope.rs b/semantic/src/scope.rs index a2f4ac6..116bb1d 100644 --- a/semantic/src/scope.rs +++ b/semantic/src/scope.rs @@ -28,6 +28,13 @@ impl Scope { 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)]