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
+5 -1
View File
@@ -122,7 +122,11 @@ impl<'a> Parser<'a> {
while *self.peek() != TokenKind::RightBracket && *self.peek() != TokenKind::EOF {
// Vector elements in Myc are usually just constants in a list
let expr = self.parse_expression()?;
elements.push(expr.eval(&mut crate::ast::nodes::Context {})); // Simple direct eval for literals
// Simple direct eval for literals (Context::new creates fresh scope)
match expr.eval(&mut crate::ast::nodes::Context::new()) {
Ok(val) => elements.push(val),
Err(e) => return Err(format!("Error evaluating vector element: {}", e)),
}
}
self.expect(TokenKind::RightBracket)?;