Expand description
Evaluator: turns AST nodes into Swan Values.
§Entry point
eval takes a &Node and a &Scope and returns a Value. It is
infallible: every error (syntax errors, invalid names, type mismatches)
is returned as a Value::Undefined(...), and errors in sub-expressions
propagate as ordinary values. It never panics.
§What lives where
This file handles literals, identifier lookup, grouping ([], {}),
function definition and application, pairing dispatch, mapping, assignment,
labelling, subcontexting, promise chaining (~>), logic (&, |),
conditional (?, ;), composition (<<, >>) and syntax-error nodes.
Operations with non-trivial logic are delegated to the sub-modules declared
below (unary operators, pairing, assignment, ~>, arithmetic and comparison).
§Tuple semantics
Tuples are flat and normalized (Nothing items are dropped). Operators are
lifted over tuples by two internal macros:
map_values!for unary operations:NothinggivesNothing, a tuple is mapped item by item, any other value goes straight to the operation.map_pairs!for binary operations: tuples are paired element-wise (the shorter side is padded withNothing) and the result is normalized.
The short-circuit operators &, |, ? and ; are not lifted: they look
at their left operand as a whole.
§Evaluation order
Operands are evaluated eagerly, left then right. The exceptions are the
short-circuit operators, whose right operand B is evaluated only when needed:
A & B and A ? B when A is truthy, A | B when A is falsy, and
A ; B when A is Nothing. &, | and ? cast A to bool as a whole
(a tuple is truthy if any of its items is), so they are not paired over tuples.
For ->, = and : the left operand is not evaluated; it is resolved into
names by eval_names.
§Scopes
{X}evaluatesXin a new child scope and returns it as aNamespace.A -> Bcaptures the current scope as a closure; applying it evaluates the body in a child of that closure scope.ns.XevaluatesXinside the scope of the namespacens.
Functions§
- eval
- Evaluates an AST node within a given
Scopecontext and returns aValue. - eval_
apply func arg- applies a callable or indexable Swan value to an argument.