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:
+13
-7
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user