basic global constants
This commit is contained in:
@@ -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)?;
|
||||
|
||||
Reference in New Issue
Block a user