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