Sign in

    moonbit-eval

    MoonBit Eval Package

    moonbit
    eval
    Download zip
    Author
    Version
    0.3.0
    License
    Apache-2.0
    Last updated
    last year
    Downloads
    170

    #MoonBit Eval

    #Introduction

    MoonBit Eval is an interpreter for the MoonBit language.

    Built on top of the @moonbitlang/parser library, it provides comprehensive and accurate MoonBit syntax support, capable of correctly parsing and executing complex MoonBit code structures including functions, structs, lambdas, loops, and more.

    #Progress

    #Examples

    #Basic Operations

    test "basic" {
    let vm = MoonBitVM::new()
    inspect(vm.eval("1 + 1"), content="2")
    inspect(vm.eval("5 * 3"), content="15")
    inspect(vm.eval("10 / 2"), content="5")
    inspect(vm.eval("5 == 5"), content="true")
    inspect(vm.eval("\"hello\" + \"world\""), content="helloworld")
    }

    #Variables and Functions

    test "variables and functions" {
    let vm = MoonBitVM::new()
    inspect(vm.eval("let a = 1"), content="()")
    inspect(vm.eval("a"), content="1")

    inspect(vm.eval("fn add(a: Int, b: Int) -> Int { a + b }"), content="(a: Int, b: Int) -> Int")
    inspect(vm.eval("add(1, 2)"), content="3")
    }

    #Mutable Variables

    test "mutable variables" {
    let vm = MoonBitVM::new()
    inspect(vm.eval("let mut a = 1"), content="()")
    inspect(vm.eval("a = 12"), content="()")
    inspect(vm.eval("a"), content="12")
    }

    #Control Flow

    test "control flow" {
    let vm = MoonBitVM::new()
    inspect(vm.eval("if 1 > 0 { 3 } else { 4 }"), content="3")

    inspect(vm.eval("let mut sum = 0"), content="()")
    inspect(vm.eval("for i = 0; i < 5; i = i + 1 { sum += i }"), content="()")
    inspect(vm.eval("sum"), content="10")
    }

    #Lambda Functions

    test "lambda" {
    let vm = MoonBitVM::new()
    inspect(vm.eval("let f = x => x * 2"), content="()")
    inspect(vm.eval("f(3)"), content="6")

    inspect(vm.eval("let h = z => z * z"), content="()")
    inspect(vm.eval("h(4)"), content="16")
    }

    #Structs and Methods

    test "struct" {
    let vm = MoonBitVM::new()
    inspect(
    vm.eval(
    #|struct S {
    #| a: Int
    #| b: Int
    #|}
    #|fn S::sum(self: S) -> Int {
    #| self.a + self.b
    #|}
    top=true,
    ),
    content="()",
    )
    inspect(vm.eval("let s = { a: 23, b: 32 }"), content="()")
    inspect(vm.eval("s.sum()"), content="55")
    }

    #Fibonacci Recursion

    test "fibonacci" {
    let vm = MoonBitVM::new()
    inspect(
    vm.eval(
    #|fn fib(n : Int) -> Int {
    #| if n <= 1 {
    #| 1
    #| } else {
    #| fib(n - 1) + fib(n - 2)
    #| }
    #|}
    ),
    content="(n: Int) -> Int",
    )
    inspect(vm.eval("fib(10)"), content="89")
    }

    #External Functions

    test "extern" {
    let vm = MoonBitVM::new()
    vm.interpreter.add_extern_fn("println", params => {
    if params is More(Constant(c=String(s), ..), ..) {
    println(s)
    }
    unit()
    })
    inspect(vm.eval("println(\"Hello from external function\")"), content="()")
    }

    #Features

    • ✅ Complete Expression Support: Arithmetic, logical, and comparison operations
    • ✅ Variable Management: Immutable and mutable variable declarations
    • ✅ Control Flow: If-else statements, for loops, while loops
    • ✅ Function Definitions: Named functions with parameters and return types
    • ✅ Lambda Expressions: Anonymous functions with closure support
    • ✅ Struct Definitions: Custom data types with methods
    • ✅ External Functions: Integration with external function calls
    • ✅ Type System: Basic type checking and inference

    #Contributing

    We welcome contributions to the MoonBit Eval project! Whether it's bug fixes, feature additions, or documentation improvements, your contributions are valuable.

    #Community

    Join our community for discussions and support:
    • QQ Group: 949886784

    QQ群

    ClosureInterpreter

    type ClosureInterpreter

    ClosureInterpreter::add_extern_fn

    ClosureInterpreter::bind_function_parameters

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

    ClosureInterpreter::execute_function_call

    处理用户定义函数的调用,包括作用域管理和参数绑定

    ClosureInterpreter::find_user_function

    fn ClosureInterpreter::find_user_function(self : ClosureInterpreter, name : String) ->
    Func
    ?
    查找用户定义的函数

    ClosureInterpreter::new

    ClosureInterpreter::pop_scope

    fn ClosureInterpreter::pop_scope(self : ClosureInterpreter) -> Unit
    销毁当前作用域

    ClosureInterpreter::push_scope

    fn ClosureInterpreter::push_scope(self : ClosureInterpreter) -> Unit
    创建新的作用域

    EvalResult

    pub struct EvalResult {
    value :
    Expr

    }

    impl Show for EvalResult

    MoonBitVM

    type MoonBitVM

    MoonBitVM::eval

    fn MoonBitVM::eval(self : MoonBitVM, code : String, log? : Bool, top? : Bool) -> EvalResult raise Failure

    MoonBitVM::new

    fn MoonBitVM::new(log? : Bool) -> MoonBitVM

    compare

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

    define_function

    fn define_function(user_functions : Map[String,
    Func
    ], name : String, func :
    Func
    ) -> Unit
    将函数存储到用户函数映射中

    define_struct_method

    fn define_struct_method(struct_methods : Map[String, Map[String,
    Func
    ]], type_name : String, method_name : String, func :
    Func
    ) -> Unit

    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

    expr_to_string

    fn expr_to_string(expr :
    Expr
    ) -> String

    find_variable

    fn find_variable(immutable_values : Array[Map[String,
    Expr
    ]], mutable_values : Array[Map[String,
    Expr
    ]], name : String) ->
    Expr
    优先查找不可变变量(性能优化),然后查找可变变量

    is_comparison_op

    fn is_comparison_op(op : String) -> Bool

    set_immutable_variable

    fn set_immutable_variable(immutable_values : Array[Map[String,
    Expr
    ]], level : Int, name : String, value :
    Expr
    ) -> Unit
    设置不可变变量

    set_mutable_variable

    fn set_mutable_variable(mutable_values : Array[Map[String,
    Expr
    ]], level : Int, name : String, value :
    Expr
    ) -> Unit
    设置可变变量

    update_mutable_variable

    fn update_mutable_variable(mutable_values : Array[Map[String,
    Expr
    ]], name : String, new_value :
    Expr
    ) -> Bool
    在可变变量数组中查找并更新指定变量