Sign in

    oboard/moonbit-eval/interpreter does not have a README file

    RuntimeFunction

    typealias (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow as RuntimeFunction

    新的函数类型定义 - 统一的上下文访问

    ControlFlow

    pub suberror ControlFlow {
    Raise(RuntimeValue)
    Continue(Array[RuntimeValue])
    Break(RuntimeValue)
    Return(RuntimeValue)
    Error(String)
    }

    ClosureInterpreter

    pub(all) struct ClosureInterpreter {
    extern_fns : Map[String, (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow]
    embedded_fns : Map[String, (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow]
    embedded_methods : Map[String, Map[String, (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow]]
    main : RuntimePackage
    current_module : RuntimePackage
    call_stack : Array[RuntimeLocation]
    }

    支持引用系统和泛型的解释器

    ClosureInterpreter::add_embedded_fn

    fn ClosureInterpreter::add_embedded_fn(self : ClosureInterpreter, name : String, f : (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow) -> Unit

    ClosureInterpreter::add_embedded_method

    fn ClosureInterpreter::add_embedded_method(self : ClosureInterpreter, type_name : String, method_name : String, f : (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow) -> Unit

    ClosureInterpreter::add_extern_fn

    fn ClosureInterpreter::add_extern_fn(self : ClosureInterpreter, name : String, f : (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow) -> Unit

    ClosureInterpreter::bind_function_parameters

    支持所有参数类型:位置参数、标签参数、可选参数、问号可选参数、丢弃参数

    ClosureInterpreter::call

    调用闭包函数,使用已求值的运行时参数

    ClosureInterpreter::call_closure

    调用闭包函数,恢复捕获的环境

    ClosureInterpreter::constants_equal

    检查两个常量是否相等

    ClosureInterpreter::constants_equal_expr

    检查两个表达式是否相等(用于构造函数匹配)

    ClosureInterpreter::error

    fn[A] ClosureInterpreter::error(self : ClosureInterpreter, msg : String) -> A raise ControlFlow

    ClosureInterpreter::find_pkg

    fn ClosureInterpreter::find_pkg(self : ClosureInterpreter, pkg : String) -> RuntimePackage

    ClosureInterpreter::format_patterns

    格式化模式列表为可读字符串

    ClosureInterpreter::get_function_type_string

    fn ClosureInterpreter::get_function_type_string(self : ClosureInterpreter, value : RuntimeValue) -> String

    将RuntimeValue转换为字符串表示(带解释器支持,用于正确的类型推导)

    ClosureInterpreter::infer_pattern_type

    fn ClosureInterpreter::infer_pattern_type(_ : ClosureInterpreter, pattern :
    Pattern
    ) -> String?

    从模式中推导类型

    ClosureInterpreter::interp_string

    处理字符串插值

    ClosureInterpreter::is_known_function

    fn ClosureInterpreter::is_known_function(self : ClosureInterpreter, name : String) -> Bool

    检查函数是否为已知函数(存在于任何函数映射中)

    ClosureInterpreter::load_module

    fn ClosureInterpreter::load_module(self : ClosureInterpreter, mod : RuntimeModule) -> Unit

    ClosureInterpreter::load_package

    fn ClosureInterpreter::load_package(self : ClosureInterpreter, pkg : RuntimePackage, target? : String) -> Unit

    ClosureInterpreter::lookup_current_function

    fn ClosureInterpreter::lookup_current_function(self : ClosureInterpreter) -> String

    查找当前调用的函数名

    ClosureInterpreter::match_case

    检查模式是否匹配给定的运行时值

    ClosureInterpreter::new

    ClosureInterpreter::parse_error_type

    ClosureInterpreter::parse_type

    ClosureInterpreter::pattern_match

    执行match表达式的模式匹配(返回RuntimeValue)

    ClosureInterpreter::pattern_to_runtime_value

    将模式转换为运行时值(用于范围检查等)

    ClosureInterpreter::pop_scope

    fn ClosureInterpreter::pop_scope(self : ClosureInterpreter) -> Unit

    销毁当前作用域

    ClosureInterpreter::push_scope

    fn ClosureInterpreter::push_scope(self : ClosureInterpreter, loc : RuntimeLocation) -> Unit

    创建新的作用域

    ClosureInterpreter::runtime_value_in_range

    fn ClosureInterpreter::runtime_value_in_range(self : ClosureInterpreter, value : RuntimeValue, lhs :
    Pattern
    , rhs :
    Pattern
    , inclusive : Bool) -> Bool

    检查 RuntimeValue 是否在指定范围内

    ClosureInterpreter::top_eval

    fn ClosureInterpreter::top_eval(self : ClosureInterpreter, code : String) -> Unit

    ClosureInterpreter::top_visit

    ClosureInterpreter::visit

    fn ClosureInterpreter::visit(self : ClosureInterpreter, node :
    Expr
    , expected_type? : String) -> RuntimeValue raise ControlFlow

    带期望类型的访问语法树节点

    ClosureInterpreter::visit_interp

    处理字符串插值表达式 (Interp)

    ClosureInterpreter::visit_multiline_string

    处理多行字符串

    ClosureInterpreter::visit_scoped

    ClosureInterpreter::with_ident

    fn[T] ClosureInterpreter::with_ident(self : ClosureInterpreter, long_ident :
    LongIdent
    , func : (RuntimePackage, String) -> T raise?) -> T raise?

    ConstructorField

    pub(all) struct ConstructorField {
    name : String?
    value : RuntimeValue
    mutable : Bool
    }

    构造器字段 - 支持带标签和可变性

    ConstructorValue

    pub(all) struct ConstructorValue {
    name : String
    fields : Array[ConstructorField]
    }

    构造器值 - 包含构造器名称和字段

    ModuleInfo

    pub(all) struct ModuleInfo {
    name : String
    version : String?
    deps : Map[String, String]?
    readme : String?
    repository : String?
    license : String?
    keywords : Array[String]?
    description : String?
    source : String?
    }

    包元数据
    impl Show for ModuleInfo

    ModuleInfo::get_zip_url

    async fn ModuleInfo::get_zip_url(self : ModuleInfo) -> String noraise

    ModuleInfo::new

    fn ModuleInfo::new(name : String, version? : String, readme? : String, repository? : String, license? : String, keywords? : Array[String], description? : String, source? : String, deps? : Map[String, String]) -> ModuleInfo

    模块元数据构造函数

    RuntimeArgument

    pub(all) struct RuntimeArgument {
    val : RuntimeValue
    kind : RuntimeArgumentKind
    }

    RuntimeArgumentKind

    pub(all) enum RuntimeArgumentKind {
    Positional
    Labelled(String)
    LabelledOption(String)
    }

    RuntimeEnvironment

    pub(all) struct RuntimeEnvironment {
    values : Map[String, RuntimeValue]
    mutable_vars : Map[String, Bool]
    parent : RuntimeEnvironment?
    }

    RuntimeEnvironment::copy

    深拷贝环境 - 完全复制所有内容

    RuntimeEnvironment::create_closure_env

    fn RuntimeEnvironment::create_closure_env(self : RuntimeEnvironment) -> RuntimeEnvironment

    为闭包创建捕获环境 - 实现正确的变量捕获语义

    RuntimeEnvironment::find

    fn RuntimeEnvironment::find(self : RuntimeEnvironment, name : String) -> RuntimeValue?

    在解释器中查找变量

    RuntimeEnvironment::new

    fn RuntimeEnvironment::new(parent? : RuntimeEnvironment, values? : Map[String, RuntimeValue]) -> RuntimeEnvironment

    RuntimeEnvironment::set

    fn RuntimeEnvironment::set(self : RuntimeEnvironment, name : String, value : RuntimeValue) -> Unit

    设置不可变变量

    RuntimeEnvironment::set_mutable_variable

    fn RuntimeEnvironment::set_mutable_variable(self : RuntimeEnvironment, name : String, value : RuntimeValue) -> Unit

    设置可变变量

    RuntimeEnvironment::top_func_def_to_closure

    fn RuntimeEnvironment::top_func_def_to_closure(self : RuntimeEnvironment, func :
    Impl
    ) -> (String?, RuntimeValue)

    处理函数声明体,创建Lambda函数

    RuntimeEnvironment::update

    fn RuntimeEnvironment::update(self : RuntimeEnvironment, name : String, new_value : RuntimeValue) -> Unit

    在可变变量数组中查找并更新指定变量

    RuntimeErrorType

    pub(all) enum RuntimeErrorType {
    ErrorType(RuntimeType)
    DefaultErrorType
    NoErrorType
    Noraise
    MaybeError(RuntimeType)
    }

    RuntimeFunctionContext

    pub(all) struct RuntimeFunctionContext {
    context : ClosureInterpreter
    mod : RuntimePackage
    args : Array[RuntimeArgument]
    }

    Runtime函数执行上下文 - 统一所有函数的执行环境

    RuntimeLocation

    pub enum RuntimeLocation {
    FunctionCall(String)
    ControlFlow(String)
    LetMut(String)
    }

    RuntimeModule

    pub(all) struct RuntimeModule {
    meta : ModuleInfo
    pkgs : Map[String, RuntimePackage]
    }

    RuntimePackage

    pub(all) struct RuntimePackage {
    name : String
    traits : Map[String,
    TraitDecl
    ]
    fn_aliases : Map[String, WithType[RuntimeValue]]
    type_aliases : Map[String, WithType[
    TypeDecl
    ]]
    trait_aliases : Map[String, WithType[
    TraitDecl
    ]]
    stubs : Map[String, String]
    type_definitions : Map[String,
    TypeDecl
    ]
    type_derived_traits : Map[String, Array[String]]
    constructors : Map[String, String]
    struct_methods : Map[String, Map[String, RuntimeValue]]
    trait_methods : Map[String, Map[String, RuntimeValue]]
    values : Map[String, RuntimeValue]
    env : RuntimeEnvironment
    deps : Map[String, RuntimePackage]
    files : Map[String, String]
    loaded : Bool
    }

    RuntimePackage::check_type_constraint

    fn RuntimePackage::check_type_constraint(_self : RuntimePackage, value : RuntimeValue, constraint :
    Type
    ) -> Bool

    检查运行时值是否满足类型约束

    RuntimePackage::cons

    fn RuntimePackage::cons(self : RuntimePackage, name : String, args : Array[RuntimeValue]) -> RuntimeValue

    构造 Constructor

    RuntimePackage::cons_with_labels

    fn RuntimePackage::cons_with_labels(self : RuntimePackage, name : String, labeled_args : Array[(String?, RuntimeValue, Bool)]) -> RuntimeValue

    构造带标签参数的 Constructor

    RuntimePackage::define_struct_method

    fn RuntimePackage::define_struct_method(self : RuntimePackage, type_name : String, method_name : String, func :
    Func
    ) -> Unit

    处理函数定义

    RuntimePackage::define_trait_method

    fn RuntimePackage::define_trait_method(self : RuntimePackage, trait_id : String, method_name : String, func : RuntimeValue) -> Unit

    处理trait方法定义

    RuntimePackage::find

    fn RuntimePackage::find(self : RuntimePackage, name : String) -> RuntimeValue?

    RuntimePackage::find_stub

    fn RuntimePackage::find_stub(self : RuntimePackage, name : String) -> String

    RuntimePackage::is_constructor

    fn RuntimePackage::is_constructor(self : RuntimePackage, name : String) -> Bool

    检查名称是否为构造函数

    RuntimePackage::new

    fn RuntimePackage::new(pkg : String, deps? : Map[String, RuntimePackage], files? : Map[String, String]) -> RuntimePackage

    RuntimePackage::set

    fn RuntimePackage::set(self : RuntimePackage, name : String, value : RuntimeValue) -> Unit

    RuntimeType

    impl Eq for RuntimeType
    impl Show for RuntimeType

    RuntimeType::any

    RuntimeType::array

    fn RuntimeType::array() -> RuntimeType

    RuntimeType::array_view

    fn RuntimeType::array_view() -> RuntimeType

    RuntimeType::bigint

    fn RuntimeType::bigint() -> RuntimeType

    RuntimeType::bool

    fn RuntimeType::bool() -> RuntimeType

    RuntimeType::byte

    fn RuntimeType::byte() -> RuntimeType

    RuntimeType::bytes

    fn RuntimeType::bytes() -> RuntimeType

    RuntimeType::char

    fn RuntimeType::char() -> RuntimeType

    RuntimeType::double

    fn RuntimeType::double() -> RuntimeType

    RuntimeType::fixed_array

    fn RuntimeType::fixed_array() -> RuntimeType

    RuntimeType::float

    fn RuntimeType::float() -> RuntimeType

    RuntimeType::int

    RuntimeType::int64

    fn RuntimeType::int64() -> RuntimeType

    RuntimeType::iter

    fn RuntimeType::iter() -> RuntimeType

    RuntimeType::json

    fn RuntimeType::json() -> RuntimeType

    RuntimeType::list

    fn RuntimeType::list() -> RuntimeType

    RuntimeType::map

    RuntimeType::option

    fn RuntimeType::option() -> RuntimeType

    RuntimeType::result

    fn RuntimeType::result() -> RuntimeType

    RuntimeType::string

    fn RuntimeType::string() -> RuntimeType

    RuntimeType::string_builder

    fn RuntimeType::string_builder() -> RuntimeType

    RuntimeType::tuple

    fn RuntimeType::tuple() -> RuntimeType

    RuntimeType::uint

    fn RuntimeType::uint() -> RuntimeType

    RuntimeType::uint16

    fn RuntimeType::uint16() -> RuntimeType

    RuntimeType::uint64

    fn RuntimeType::uint64() -> RuntimeType

    RuntimeType::uninitialized_array

    fn RuntimeType::uninitialized_array() -> RuntimeType

    RuntimeValue

    pub(all) enum RuntimeValue {
    Unit
    Bool(Bool)
    Int(Int)
    UInt(UInt)
    UInt16(UInt16)
    Int64(Int64)
    UInt64(UInt64)
    Float(Float)
    Double(Double)
    BigInt(
    BigInt
    )
    Char(Char)
    Byte(Byte)
    String(String)
    Bytes(Bytes)
    StringView(
    View
    )
    StringBuilder(StringBuilder)
    Tuple(Array[RuntimeValue])
    Array(Array[RuntimeValue])
    FixedArray(FixedArray[RuntimeValue])
    ArrayView(ArrayView[RuntimeValue])
    UninitializedArray(UninitializedArray[RuntimeValue])
    Map(Map[RuntimeValue, RuntimeValue])
    Struct(WithType[Map[String, RuntimeValue]])
    Closure(
    Func
    , RuntimeEnvironment)
    Constructor(WithType[ConstructorValue])
    Exception(String)
    Iter(Iter[RuntimeValue])
    Iter2(Iter2[RuntimeValue, RuntimeValue])
    Json(Json)
    }

    运行时值 - 解释器实际操作的数据类型
    impl Add for RuntimeValue
    impl Eq for RuntimeValue

    RuntimeValue::from_constant_with_type

    fn RuntimeValue::from_constant_with_type(c :
    Constant
    , expected_type : String?) -> RuntimeValue

    带期望类型的常量创建函数

    RuntimeValue::from_expr

    将@syntax.Expr转换为RuntimeValue

    RuntimeValue::from_option

    fn RuntimeValue::from_option(val : RuntimeValue?) -> RuntimeValue

    RuntimeValue::from_pattern

    将模式转换为值(用于范围匹配)

    RuntimeValue::from_result

    fn RuntimeValue::from_result(val : Result[RuntimeValue, RuntimeValue]) -> RuntimeValue

    RuntimeValue::is_mutable

    fn RuntimeValue::is_mutable(self : RuntimeValue) -> Bool

    检查值是否为可变类型

    RuntimeValue::is_reference

    fn RuntimeValue::is_reference(self : RuntimeValue) -> Bool

    检查值是否为引用类型

    RuntimeValue::iter2

    为 RuntimeValue 实现 iter2() 方法,返回双值迭代器 根据 iter2.mbt 中的 iter2_iter_fn,应该返回 Iter,其中每个元素是元组

    RuntimeValue::overload_array_literal

    fn RuntimeValue::overload_array_literal(self : RuntimeValue, expected_type : String) -> RuntimeValue

    重载数组字面量 - 支持组合重载如[1,2,3] -> Bytes

    RuntimeValue::overload_literal

    fn RuntimeValue::overload_literal(self : RuntimeValue, expected_type : String) -> RuntimeValue

    重载字面量转换 - 将默认类型的RuntimeValue转换为期望的类型 遵循MoonBit重载字面量规则,同时支持模式匹配中的隐式转换

    RuntimeValue::to_expr

    将RuntimeValue转换为表达式(用于输出)

    WithType

    pub struct WithType[T] {
    val : T
    ty : RuntimeType
    }

    带类型信息的值包装器
    impl ToJson for WithType[T]

    array_methods

    let array_methods : Map[String, (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow]

    array_view_methods

    let array_view_methods : Map[String, (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow]

    check_type_constraint2

    fn check_type_constraint2(value : RuntimeType, constraint :
    Type
    ) -> Bool

    compare

    fn[T : Compare + Eq] compare(left : T, right : T, op : String) ->
    Constant

    error

    #callsite(autofill(loc))
    fn[A] error(msg : String, loc~ : SourceLoc) -> A raise ControlFlow

    eval_double_op

    fn eval_double_op(left : Double, right : Double, op : String) ->
    Constant

    eval_float_op

    fn eval_float_op(left : Double, right : Double, op : String) ->
    Constant

    eval_int64_op

    fn eval_int64_op(left : Int64, right : Int64, op : String) ->
    Constant

    eval_int_op

    fn eval_int_op(left : Int, right : Int, op : String) ->
    Constant

    eval_uint64_op

    fn eval_uint64_op(left : UInt64, right : UInt64, op : String) ->
    Constant

    eval_uint_op

    fn eval_uint_op(left : UInt, right : UInt, op : String) ->
    Constant

    extract_type_name

    fn extract_type_name(ty :
    Type
    ) -> String

    从@syntax.Type中提取简单的类型名

    fixedarray_methods

    let fixedarray_methods : Map[String, (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow]

    int_embedded_code

    let int_embedded_code : Map[String, (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow]

    int_methods

    let int_methods : Map[String, (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow]

    is_comparison_op

    fn is_comparison_op(op : String) -> Bool

    iter2_methods

    let iter2_methods : Map[String, (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow]

    Iter2 内置方法映射

    iter_methods

    let iter_methods : Map[String, (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow]

    map_methods

    let map_methods : Map[String, (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow]

    option_methods

    let option_methods : Map[String, (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow]

    parse_code_to_expr

    fn parse_code_to_expr(code : String) -> Result[
    Expr
    , String]

    parse_code_to_impl

    fn parse_code_to_impl(code : String) -> Result[
    Impl
    , String]

    runtime_value_infix

    fn runtime_value_infix(op : String, lhs : RuntimeValue, rhs : RuntimeValue) -> RuntimeValue

    RuntimeValue 之间的中缀运算 - 消除对 constant_infix 的依赖

    string_embedded_code

    let string_embedded_code : Map[String, (RuntimeFunctionContext) -> RuntimeValue raise ControlFlow]

    type_to_string

    fn type_to_string(ty :
    Type
    ) -> String