Add scope and basic stdlib for evaluation

Introduces a `Scope` struct for dynamic scope management and a `Context`
struct to hold the current scope.
Implements a basic standard library with arithmetic operations and a
greater-than comparison.
Modifies `Node::eval` to handle identifiers by resolving them within the
current scope and `Call` nodes for function execution.
Updates the parser to use `Context::new()` for evaluating vector
elements, ensuring a fresh scope.
Enhances the main loop to handle potential runtime errors during
evaluation.
This commit is contained in:
Michael Schimmel
2026-02-17 00:33:58 +01:00
parent c05c74bb65
commit 647fc79d28
3 changed files with 145 additions and 20 deletions
+13 -7
View File
@@ -53,13 +53,19 @@ impl eframe::App for CompilerApp {
match parser.parse_expression() {
Ok(ast) => {
let mut context = Context {};
let result = ast.eval(&mut context);
self.output_log = format!(
"AST Parsed Successfully.\nResult: {}\n\nFinished at {:?}",
result,
std::time::SystemTime::now(),
);
let mut context = Context::new(); // Use new() which registers stdlib
match ast.eval(&mut context) {
Ok(result) => {
self.output_log = format!(
"AST Parsed & Evaluated Successfully.\nResult: {}\n\nFinished at {:?}",
result,
std::time::SystemTime::now(),
);
}
Err(e) => {
self.output_log = format!("Runtime Error: {}", e);
}
}
}
Err(e) => {
self.output_log = format!("Parser Error: {}", e);