Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Interpreter_Runtime Class Reference

Structured runtime for executing typed HIR. More...

#include <Interpreter_Runtime.H>

Collaboration diagram for Aleph::Interpreter_Runtime:
[legend]

Classes

struct  Interpreter_Call_Frame
 

Public Member Functions

 Interpreter_Runtime (Diagnostic_Engine *dx=nullptr, const Interpreter_Runtime_Options &opts={}) noexcept
 Builds a fresh interpreter runtime.
 
void set_diagnostics (Diagnostic_Engine *dx) noexcept
 Sets the optional diagnostic sink for runtime failures.
 
const Interpreter_Environment & prelude_environment () const noexcept
 Returns the persistent prelude environment.
 
const Interpreter_Environment & global_environment () const noexcept
 Returns the globals environment for the last evaluated module.
 
size_t call_depth () const noexcept
 Returns the current call depth.
 
void reset () noexcept
 Clears globals and the call stack while preserving host bindings.
 
bool bind_host_function (const std::string &name, const Interpreter_Host_Function callback, void *user_data=nullptr)
 Defines one host function in the persistent prelude.
 
Interpreter_Execution_Result evaluate_module (const Compiler_HIR_Module *module)
 Evaluates one HIR module and leaves its globals installed.
 
Interpreter_Execution_Result call (const std::string &name, const DynArray< Interpreter_Value > &arguments)
 Calls one visible function by name.
 

Private Member Functions

Interpreter_Execution_Result runtime_error (const Source_Span &span, const std::string &code, const std::string &message, const std::string &note="", const std::string &help="") const
 
Interpreter_Execution_Result parse_integer_literal (const Compiler_HIR_Constant_Expr *node) const
 
Interpreter_Execution_Result parse_string_literal (const Compiler_HIR_Constant_Expr *node) const
 
Interpreter_Execution_Result parse_char_literal (const Compiler_HIR_Constant_Expr *node) const
 
Interpreter_Execution_Result require_integer (const Interpreter_Value &value, const Source_Span &span, const std::string &context) const
 
Interpreter_Execution_Result require_bool (const Interpreter_Value &value, const Source_Span &span, const std::string &context) const
 
Interpreter_Execution_Result evaluate_expr (const Compiler_HIR_Expr *expr, Interpreter_Environment &env)
 
Interpreter_Execution_Result evaluate_stmt (const Compiler_HIR_Stmt *stmt, Interpreter_Environment &env)
 
Interpreter_Execution_Result call_hir_function (const Interpreter_Value &callee, const DynArray< Interpreter_Value > &arguments, const Source_Span &span)
 
Interpreter_Execution_Result call_host_function (const Interpreter_Value &callee, const DynArray< Interpreter_Value > &arguments, const Source_Span &span) const
 
Interpreter_Execution_Result call_value (const Interpreter_Value &callee, const DynArray< Interpreter_Value > &arguments, const Source_Span &span)
 
bool install_module_function (const Compiler_HIR_Function *function)
 

Static Private Member Functions

static Interpreter_Execution_Result ok (const Interpreter_Value &value=Interpreter_Value::make_unit())
 
static Interpreter_Execution_Result control (const Interpreter_Control_Flow_Kind flow, const Interpreter_Value &value=Interpreter_Value::make_unit())
 
static bool strip_quotes (std::string_view text, const char quote, std::string_view &body) noexcept
 
static bool decode_escaped_text (const std::string_view body, std::string &out, std::string &error)
 

Private Attributes

Interpreter_Runtime_Options options
 
Diagnostic_Engine * diagnostics = nullptr
 
Interpreter_Environment prelude
 
Interpreter_Environment globals
 
DynArray< Interpreter_Call_Frame > call_stack
 

Detailed Description

Structured runtime for executing typed HIR.

Host functions live in a persistent prelude environment, while user functions and top-level let bindings are installed in the globals environment for the last evaluated module.

Definition at line 734 of file Interpreter_Runtime.H.

Constructor & Destructor Documentation

◆ Interpreter_Runtime()

Aleph::Interpreter_Runtime::Interpreter_Runtime ( Diagnostic_Engine *  dx = nullptr,
const Interpreter_Runtime_Options &  opts = {} 
)
inlineexplicitnoexcept

Builds a fresh interpreter runtime.

Parameters
dxOptional diagnostic sink for mirrored runtime errors.
optsRuntime options such as maximum call depth.

Definition at line 1452 of file Interpreter_Runtime.H.

Member Function Documentation

◆ bind_host_function()

bool Aleph::Interpreter_Runtime::bind_host_function ( const std::string &  name,
const Interpreter_Host_Function  callback,
void *  user_data = nullptr 
)
inline

Defines one host function in the persistent prelude.

Parameters
nameHost-visible callable name.
callbackHost callback.
user_dataOpaque embedding-owned pointer forwarded to callback.
Returns
false if name already exists in the prelude.

Definition at line 1496 of file Interpreter_Runtime.H.

References ah_runtime_error_unless, Aleph::blossom_maximum_cardinality_matching(), Aleph::Interpreter_Environment::define(), Aleph::Interpreter_Value::make_host_function(), and prelude.

Referenced by Aleph::Compiler_Driver::bind_host_function(), and Aleph::Compiler_Generic_Driver::bind_host_function().

◆ call()

Interpreter_Execution_Result Aleph::Interpreter_Runtime::call ( const std::string &  name,
const DynArray< Interpreter_Value > &  arguments 
)
inline

Calls one visible function by name.

The name is resolved first in globals, then in the persistent prelude.

Parameters
nameFunction or host function name.
argumentsPositional arguments.
Returns
Structured call result.

Definition at line 1551 of file Interpreter_Runtime.H.

References call_value(), globals, Aleph::Interpreter_Environment::lookup(), prelude, runtime_error(), and value.

◆ call_depth()

size_t Aleph::Interpreter_Runtime::call_depth ( ) const
inlinenoexcept

Returns the current call depth.

Definition at line 1476 of file Interpreter_Runtime.H.

References call_stack.

◆ call_hir_function()

◆ call_host_function()

◆ call_value()

◆ control()

◆ decode_escaped_text()

static bool Aleph::Interpreter_Runtime::decode_escaped_text ( const std::string_view  body,
std::string &  out,
std::string &  error 
)
inlinestaticprivate

◆ evaluate_expr()

◆ evaluate_module()

Interpreter_Execution_Result Aleph::Interpreter_Runtime::evaluate_module ( const Compiler_HIR_Module *  module)
inline

Evaluates one HIR module and leaves its globals installed.

Function declarations are installed first to support recursion and mutual recursion. Top-level statements are then evaluated in order.

Parameters
moduleHIR module to evaluate.
Returns
Structured runtime result for the module evaluation.

Definition at line 1513 of file Interpreter_Runtime.H.

References Aleph::blossom_maximum_cardinality_matching(), Aleph::Break, Aleph::Continue, evaluate_stmt(), Aleph::Compiler_HIR_Module::functions, globals, install_module_function(), ok(), reset(), Aleph::Return, Aleph::Runtime_Error, runtime_error(), and Aleph::Compiler_HIR_Module::statements.

Referenced by Aleph::Compiler_Driver::execute_hir_runtime(), and Aleph::Compiler_Generic_Driver::execute_hir_runtime().

◆ evaluate_stmt()

◆ global_environment()

const Interpreter_Environment & Aleph::Interpreter_Runtime::global_environment ( ) const
inlinenoexcept

Returns the globals environment for the last evaluated module.

Definition at line 1470 of file Interpreter_Runtime.H.

References globals.

◆ install_module_function()

bool Aleph::Interpreter_Runtime::install_module_function ( const Compiler_HIR_Function *  function)
inlineprivate

◆ ok()

◆ parse_char_literal()

◆ parse_integer_literal()

◆ parse_string_literal()

◆ prelude_environment()

const Interpreter_Environment & Aleph::Interpreter_Runtime::prelude_environment ( ) const
inlinenoexcept

Returns the persistent prelude environment.

Definition at line 1464 of file Interpreter_Runtime.H.

References prelude.

◆ require_bool()

Interpreter_Execution_Result Aleph::Interpreter_Runtime::require_bool ( const Interpreter_Value &  value,
const Source_Span &  span,
const std::string &  context 
) const
inlineprivate

◆ require_integer()

Interpreter_Execution_Result Aleph::Interpreter_Runtime::require_integer ( const Interpreter_Value &  value,
const Source_Span &  span,
const std::string &  context 
) const
inlineprivate

◆ reset()

void Aleph::Interpreter_Runtime::reset ( )
inlinenoexcept

Clears globals and the call stack while preserving host bindings.

Definition at line 1482 of file Interpreter_Runtime.H.

References call_stack, Aleph::Interpreter_Environment::clear(), globals, prelude, and Aleph::Interpreter_Environment::set_parent().

Referenced by Aleph::Compiler_Driver::clear(), Aleph::Compiler_Generic_Driver::clear(), and evaluate_module().

◆ runtime_error()

◆ set_diagnostics()

void Aleph::Interpreter_Runtime::set_diagnostics ( Diagnostic_Engine *  dx)
inlinenoexcept

Sets the optional diagnostic sink for runtime failures.

Definition at line 1458 of file Interpreter_Runtime.H.

References diagnostics.

Referenced by Aleph::Compiler_Driver::clear(), and Aleph::Compiler_Generic_Driver::clear().

◆ strip_quotes()

static bool Aleph::Interpreter_Runtime::strip_quotes ( std::string_view  text,
const char  quote,
std::string_view &  body 
)
inlinestaticprivatenoexcept

Member Data Documentation

◆ call_stack

DynArray<Interpreter_Call_Frame> Aleph::Interpreter_Runtime::call_stack
private

Definition at line 746 of file Interpreter_Runtime.H.

Referenced by call_depth(), call_hir_function(), and reset().

◆ diagnostics

Diagnostic_Engine* Aleph::Interpreter_Runtime::diagnostics = nullptr
private

Definition at line 743 of file Interpreter_Runtime.H.

Referenced by runtime_error(), and set_diagnostics().

◆ globals

Interpreter_Environment Aleph::Interpreter_Runtime::globals
private

◆ options

Interpreter_Runtime_Options Aleph::Interpreter_Runtime::options
private

Definition at line 742 of file Interpreter_Runtime.H.

Referenced by call_hir_function().

◆ prelude

Interpreter_Environment Aleph::Interpreter_Runtime::prelude
private

Definition at line 744 of file Interpreter_Runtime.H.

Referenced by bind_host_function(), call(), prelude_environment(), and reset().


The documentation for this class was generated from the following file: