Skip to main content

eval

Function eval 

Source
pub fn eval(node: &Node, scope: &Scope) -> Value
Expand 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::Termination for Node::Unclosed (missing closing bracket).
  • Undef::Token for Node::UnexpectedToken.
  • Undef::BinaryOperation named "function_definition", "assignment" or "labelling" when the left side is not a valid name or tuple of names (the lhv holds the Undef::Name describing the offending node).
  • Undef::BinaryOperation named "subcontexting" when the left side is not a namespace.
  • Undef::BinaryOperation named "application" (see eval_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(_)));