basic global constants

This commit is contained in:
2025-12-25 15:19:15 +01:00
parent c527aceecd
commit 781c35d484
5 changed files with 57 additions and 12 deletions

View File

@@ -7,12 +7,14 @@ use crate::{
pub struct Analyzer {
pub functions: HashMap<String, i32>,
pub constants: HashMap<String, u64>,
}
impl Analyzer {
pub fn new() -> Analyzer {
Analyzer {
functions: HashMap::new(),
constants: HashMap::new(),
}
}
@@ -44,6 +46,18 @@ impl Analyzer {
} => {
self.analyze_expr(initializer)?;
}
Stmt::Const { name, value } => {
if self.constants.contains_key(&name.lexeme)
|| self.functions.contains_key(&name.lexeme)
{
return error!(
name.loc,
format!("tried to redefine constant '{}'", name.lexeme)
);
}
self.constants
.insert(name.lexeme.clone(), value.lexeme.parse().unwrap());
}
Stmt::Block(statements) => {
for stmt in statements {
self.analyze_stmt(stmt)?;