pub fn eval(node: &Node, scope: &Scope) -> ValueExpand description
Evaluates an AST node within a given Scope context and returns a Value.
This function is infallible: it never panics. All error conditions are
encoded as Value::Undefined(...) so that Swan’s “errors are values” semantics
are preserved throughout, and sub-expression errors propagate as Undefined values.
§Arguments
node: the AST node to evaluate.scope: the environment used for identifier lookup. Assignment and labelling write into this scope (not into a parent).
§Error values produced directly here
Undef::TerminationforNode::Unclosed(missing closing bracket).Undef::TokenforNode::UnexpectedToken.Undef::BinaryOperationnamed"function_definition","assignment"or"labelling"when the left side is not a valid name or tuple of names (thelhvholds theUndef::Namedescribing the offending node).Undef::BinaryOperationnamed"subcontexting"when the left side is not a namespace.Undef::BinaryOperationnamed"application"(seeeval_apply).
Errors from the delegated sub-modules are documented there.
§Examples
use swan_core::builtins::root_scope;
use swan_core::evaluator::eval;
use swan_core::lexer::Lexer;
use swan_core::parser::Parser;
use swan_core::value::Value;
let scope = root_scope();
let ast = Parser::new(Lexer::new(r#""a" - 1"#)).parse();
assert!(matches!(eval(&ast, &scope), Value::Undefined(_)));