1
0
Fork 0
This repository has been archived on 2025-03-30. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
compiler-course/src/compiler/symtab.rs

44 lines
1.3 KiB
Rust
Raw Normal View History

use crate::compiler::value::Value;
use std::collections::HashMap;
#[derive(Default)]
pub struct SymTab<'source> {
pub locals: HashMap<&'source str, Value>,
pub parent: Option<Box<SymTab<'source>>>,
}
impl<'source> SymTab<'source> {
pub fn get(&mut self, symbol: &str) -> &mut Value {
if let Some(val) = self.locals.get_mut(symbol) {
val
} else if let Some(parent) = &mut self.parent {
parent.get(symbol)
} else {
panic!("No symbol {} found!", symbol);
}
}
pub fn new_global() -> SymTab<'source> {
2025-02-03 23:04:36 +02:00
let locals = HashMap::from([
("+", Value::Func(Value::add)),
("*", Value::Func(Value::mul)),
("-", Value::Func(Value::sub)),
("/", Value::Func(Value::div)),
("%", Value::Func(Value::rem)),
("==", Value::Func(Value::eq)),
("!=", Value::Func(Value::neq)),
("<", Value::Func(Value::lt)),
("<=", Value::Func(Value::le)),
(">", Value::Func(Value::gt)),
(">=", Value::Func(Value::ge)),
("not", Value::Func(Value::not)),
("neg", Value::Func(Value::neg)),
]);
SymTab {
2025-02-03 23:04:36 +02:00
locals,
parent: None,
}
}
}