Skip to content

SEval

class SEval

@defgroup evaluator Evaluator & Execution Engine @brief S-expression evaluation and runtime state management

The SEval class implements the core evaluation engine:

@mermaid graph LR A["S-Expression"] --> B["Parse"] B --> C["SEval::eval"] C --> D{"Is it a
list?"} D -->|No| E["Return Atom"] D -->|Yes| F["Get Operator"] F --> G{"Builtin or
User?"} G -->|Builtin| H["dispatch_builtin"] G -->|User| I["dispatch_user"] H --> J["Result"] I --> J @endmermaid

Key Responsibilities: - Evaluation dispatch to operators - Environment and scope chain management - Closure and macro handling - Macro expansion - Virtual signal management

Evaluation Model: WAL uses an applicative-order (eager) evaluator: 1. Evaluate all arguments first (except special forms) 2. Dispatch to operator with evaluated arguments 3. Operator returns result 4. Continue with next expression

Special Forms: Some operators (like if, define, lambda) receive unevaluated arguments and handle evaluation themselves for proper semantics.

See
"Operator Registry" for extending evaluator
See
for value types

@{ S-expression evaluator - the core evaluation engine

The SEval class is responsible for: - Tree-walking evaluation of S-expressions - Environment and scope management - Operator dispatch (builtin and user-defined) - Closure and macro evaluation - State tracking for traces and virtual signals

Variables

Name Description
required_modules Modules already run by require, per interpreter.

Functions

Name Description
SEval Create a new evaluator for a trace container Initializes: - Global environment with builtins - Operator registry with all standard operators - Trace container reference traces : Shared TraceContainer for signal access See : for loading traces
reset Reset evaluator state Clears user definitions and returns traces to time 0.
eval @name Evaluation @{ Evaluate an S-expression The core evaluation function.
eval_args Evaluate multiple arguments Convenience function to evaluate a vector of expressions.
eval_closure Evaluate a closure (lambda application) Creates new environment with parameters bound to arguments, then evaluates closure body in that environment.
apply_callable Call a lambda or a builtin passed as a value.
quasiquote Expand a quasiquoted template, honouring unquote and unquote-splicing.
expand_macro Expand a macro call.
dispatch_builtin Dispatch to a built-in operator Looks up operator in registry and calls it with unevaluated arguments.
dispatch_user Dispatch to a user-defined operator Calls operator with already-evaluated arguments.
get_environment @} @name Environment Access @{ Get current evaluation environment (may be nested in closures)
set_environment Switch to a different environment (advanced usage)
get_global_environment Get global (top-level) environment
get_operator_registry @} @name Operator Registry @{ Access registry for operator queries and extensions See : "Operator Registry"
get_operator_registry Const access for introspection
get_traces @} @name Trace Access @{ Get reference to loaded traces for signal queries See : "Trace System"
get_scope @} @name Scope & Group Management @{ Get current scope for signal hierarchies (e.g., "module.submodule")
set_scope Set current scope context
get_group Get current group for signal classification
set_group Set current group context
get_virtual_signals @} @name Virtual Signals @{ Access virtual signals (user-defined derived signals) See : for virtual signal creation
get_aliases @} @name Aliases & Macros @{ Get all registered aliases (operator name mappings)
add_alias Register an operator alias (e.g., rest -> cdr)
remove_alias Remove an alias
has_alias Check if an alias exists
get_macros Get all registered macros
get_required_modules Modules already run by require in THIS interpreter.
add_macro Register a new macro
has_macro Check if a macro exists
gensym @} @name Symbol Generation @{ Generate unique symbol name Useful for macro hygiene and gensym patterns prefix : Default prefix for generated symbols Return : Unique symbol (e.g., "gensym-123")
initialize_builtins @} Initialize all builtin operators

Variable Details

required_modules

std::unordered_set<std::string> required_modules

Modules already run by require, per interpreter.

This was a process-wide static, which was wrong twice over: a second Wal instance silently skipped a module it had never loaded and so never got its definitions, and two threads mutating one unordered_set is undefined behaviour. Module state belongs to the interpreter that loaded it.

Function Details

SEval

explicit SEval(std::shared_ptr<TraceContainer> traces)

Create a new evaluator for a trace container

Initializes: - Global environment with builtins - Operator registry with all standard operators - Trace container reference

traces
Shared TraceContainer for signal access
See
for loading traces

add_alias

void add_alias(const std::string& from, const std::string& to)

Register an operator alias (e.g., rest -> cdr)

add_macro

void add_macro(const std::string& name, const Value& macro)

Register a new macro

apply_callable

Value apply_callable(const Value& func, const std::vector<Value>& args)

Call a lambda or a builtin passed as a value. Arguments are evaluated.

dispatch_builtin

Value dispatch_builtin(const std::string& name, const std::vector<Value>& args)

Dispatch to a built-in operator

Looks up operator in registry and calls it with unevaluated arguments. Operator decides when/if to evaluate args.

Used for special forms (if, define, lambda, etc.) that need control over argument evaluation.

name
Operator name (e.g., "+", "if", "define")
args
Unevaluated arguments (raw expressions)
Return
Result from operator @throws EvalError if operator not found

dispatch_user

Value dispatch_user(const std::string& name, const std::vector<Value>& args)

Dispatch to a user-defined operator

Calls operator with already-evaluated arguments. Used for closures, Python-imported functions, etc.

name
Operator name
args
Evaluated arguments
Return
Result from operator @throws EvalError if operator not found

eval

Value eval(const Value& expr)

@name Evaluation @{ Evaluate an S-expression

The core evaluation function. Handles: - Atoms: returns value as-is - Symbols: looks up in environment - Lists: dispatches to operator

Evaluation Order: 1. If not a list, return atomic value 2. If symbol starts with '~', resolve signal 3. First element must be operator name (symbol) 4. Remaining elements are arguments 5. Dispatch to operator with unevaluated args

expr
The expression to evaluate (atom or list)
Return
The evaluated result @throws EvalError on undefined variables or operators @throws ParseError on syntax errors (if reparsing)

eval_args

std::vector<Value> eval_args(const std::vector<Value>& args)

Evaluate multiple arguments

Convenience function to evaluate a vector of expressions. Each expression evaluated in current environment.

args
Expressions to evaluate
Return
Vector of evaluated results (same order) @throws EvalError if any argument evaluation fails

eval_closure

Value eval_closure(std::shared_ptr<Closure> closure, const std::vector<Value>& args)

Evaluate a closure (lambda application)

Creates new environment with parameters bound to arguments, then evaluates closure body in that environment.

Process: 1. Create new environment with closure's captured env as parent 2. Bind parameters to arguments 3. Evaluate body in new environment 4. Restore previous environment

Supports recursion via named closures.

closure
The closure to call
args
The evaluated arguments to pass
Return
Result of closure body @throws EvalError if parameter count mismatch
See

expand_macro

Value expand_macro(const std::string& name, const std::vector<Value>& args)

Expand a macro call. Arguments are bound UNEVALUATED.

gensym

std::string gensym(const std::string& prefix = "gensym")

@} @name Symbol Generation @{ Generate unique symbol name Useful for macro hygiene and gensym patterns

prefix
Default prefix for generated symbols
Return
Unique symbol (e.g., "gensym-123")

get_aliases

const std::unordered_map<std::string, std::string>& get_aliases() const

@} @name Aliases & Macros @{ Get all registered aliases (operator name mappings)

get_environment

std::shared_ptr<Environment> get_environment() const

@} @name Environment Access @{ Get current evaluation environment (may be nested in closures)

get_global_environment

std::shared_ptr<Environment> get_global_environment() const

Get global (top-level) environment

get_group

const std::string& get_group() const

Get current group for signal classification

get_macros

const std::unordered_map<std::string, Value>& get_macros() const

Get all registered macros

get_operator_registry

OperatorRegistry& get_operator_registry()

@} @name Operator Registry @{ Access registry for operator queries and extensions

See
"Operator Registry"

const OperatorRegistry& get_operator_registry() const

Const access for introspection

get_required_modules

std::unordered_set<std::string>& get_required_modules()

Modules already run by require in THIS interpreter.

get_scope

const std::string& get_scope() const

@} @name Scope & Group Management @{ Get current scope for signal hierarchies (e.g., "module.submodule")

get_traces

std::shared_ptr<TraceContainer> get_traces() const

@} @name Trace Access @{ Get reference to loaded traces for signal queries

See
"Trace System"

get_virtual_signals

std::unordered_map<std::string, Value>& get_virtual_signals()

@} @name Virtual Signals @{ Access virtual signals (user-defined derived signals)

See
for virtual signal creation

has_alias

bool has_alias(const std::string& name) const

Check if an alias exists

has_macro

bool has_macro(const std::string& name) const

Check if a macro exists

initialize_builtins

void initialize_builtins()

@} Initialize all builtin operators

quasiquote

Value quasiquote(const Value& tmpl, int depth)

Expand a quasiquoted template, honouring unquote and unquote-splicing.

remove_alias

void remove_alias(const std::string& from)

Remove an alias

reset

void reset()

Reset evaluator state

Clears user definitions and returns traces to time 0. Preserves loaded traces and global operators.

Useful between independent computations.

set_environment

void set_environment(std::shared_ptr<Environment> env)

Switch to a different environment (advanced usage)

set_group

void set_group(const std::string& group)

Set current group context

set_scope

void set_scope(const std::string& scope)

Set current scope context