MoonBit Eval Package
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("7 % 3"), content="1")
inspect(vm.eval("5 == 5"), content="true")
inspect(vm.eval("5 != 3"), content="true")
inspect(vm.eval("3 < 5"), content="true")
inspect(vm.eval("5 > 3"), content="true")
inspect(vm.eval("\"hello\" + \"world\""), content="helloworld")
}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")
}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")
}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")
}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")
}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")
}test "array and tuple" {
let vm = MoonBitVM::new()
// Array operations
inspect(vm.eval("let a = [1, 2, 3]"), content="()")
inspect(vm.eval("a[0]"), content="1")
inspect(vm.eval("a[1] = 4"), content="()")
inspect(vm.eval("a"), content="[1, 4, 3]")
// Tuple operations
inspect(vm.eval("let t = (1, 2)"), content="()")
inspect(vm.eval("t.0"), content="1")
inspect(vm.eval("t.1"), content="2")
inspect(vm.eval("let (a, b) = t"), content="()")
}test "mutable fields" {
let vm = MoonBitVM::new()
inspect(
vm.eval(
#|struct Point {
#| mut x : Int
#| mut y : Int
#|}
top=true,
),
content="()",
)
inspect(vm.eval("let p = { x: 1, y: 2 }"), content="()")
inspect(vm.eval("p.x = 3"), content="()")
inspect(vm.eval("p.y = 4"), content="()")
inspect(vm.eval("p.x"), content="3")
}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")
}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="()")
}test "embedded" {
let vm = MoonBitVM::new()
vm.interpreter.add_embedded_fn("%string_length", params =>
if params is More(Constant(c=String(s), loc~), ..) {
Constant(c=Int(s.length().to_string()), loc~)
} else {
unit()
}
)
inspect(
vm.eval("pub fn String::length(self : String) -> Int = \"%string_length\""),
content="()",
)
inspect(vm.eval("\"hello\".length()"), content="5")
}test "match" {
let vm = MoonBitVM::new()
// 基本常量匹配
inspect(vm.eval("let s = 10"), content="()")
inspect(vm.eval("match s { 10 => 100 }"), content="100")
// 变量模式匹配
inspect(vm.eval("match 42 { x => x * 2 }"), content="84")
// 通配符模式匹配
inspect(vm.eval("match 123 { _ => 999 }"), content="999")
// Or模式匹配
inspect(vm.eval("match 5 { 1 | 5 | 10 => 100 }"), content="100")
inspect(vm.eval("match 3 { 1 | 5 | 10 => 100; _ => 200 }"), content="200")
// 测试简单变量绑定
inspect(vm.eval("match 42 { x => x }"), content="42")
// Tuple模式匹配 - 简化测试
inspect(vm.eval("match (1, 2) { (a, b) => a }"), content="1")
inspect(vm.eval("match (1, 2) { (a, b) => b }"), content="2")
inspect(vm.eval("match (1, 2) { (a, b) => a + b }"), content="3")
// Array模式匹配
inspect(vm.eval("match [1, 2, 3] { [a, b, c] => a + b + c }"), content="6")
inspect(vm.eval("match [5, 10] { [x, y] => x * y }"), content="50")
// Record模式匹配
inspect(vm.eval("match { x: 10, y: 20 } { { x, y } => x + y }"), content="30")
inspect(
vm.eval("match { name: \"Alice\", age: 25 } { { name, .. } => name }"),
content="Alice",
)
inspect(
vm.eval("match { name: \"Alice\", age: 25 } { { age, .. } => age }"),
content="25",
)
// 嵌套模式匹配
inspect(
vm.eval("match (1, [2, 3]) { (a, [b, c]) => a + b + c }"),
content="6",
)
// 多个case的匹配
inspect(vm.eval("match 2 { 1 => 10; 2 => 20; _ => 30 }"), content="20")
inspect(vm.eval("match 5 { 1 => 10; 2 => 20; _ => 30 }"), content="30")
}
type ClosureInterpreterfn ClosureInterpreter::add_embedded_fn(self : ClosureInterpreter, name : String, f : (Array[RuntimeArgument]) -> RuntimeValue) -> Unitfn ClosureInterpreter::add_embedded_method(self : ClosureInterpreter, type_name : String, method_name : String, f : (Array[RuntimeArgument]) -> RuntimeValue) -> Unitfn ClosureInterpreter::add_extern_fn(self : ClosureInterpreter, name : String, f : (Array[RuntimeArgument]) -> RuntimeValue) -> Unitfn ClosureInterpreter::bind_function_parameters(self : ClosureInterpreter, params : List[Parameter], args : List[Argument]) -> Unitfn ClosureInterpreter::bind_function_parameters_runtime(self : ClosureInterpreter, params : List[Parameter], args : List[Argument]) -> Unitfn ClosureInterpreter::constants_equal(_self : ClosureInterpreter, c1 : Constant, c2 : Constant) -> Boolfn ClosureInterpreter::constants_equal_expr(self : ClosureInterpreter, expr1 : Expr, expr2 : Expr) -> Boolfn ClosureInterpreter::define_function(self : ClosureInterpreter, name : String, func : Func) -> Unitfn ClosureInterpreter::define_struct_method(self : ClosureInterpreter, type_name : String, method_name : String, func : Func) -> Unitfn ClosureInterpreter::execute_function_call(self : ClosureInterpreter, function : Func, args : List[Argument]) -> Exprfn ClosureInterpreter::execute_function_call_runtime(self : ClosureInterpreter, function : Func, args : List[Argument]) -> RuntimeValuefn ClosureInterpreter::execute_match(self : ClosureInterpreter, value : Expr, cases : List[Case]) -> Exprfn ClosureInterpreter::execute_match_runtime(self : ClosureInterpreter, value : RuntimeValue, cases : List[Case]) -> RuntimeValuefn ClosureInterpreter::pattern_matches(self : ClosureInterpreter, value : Expr, pattern : Pattern) -> Boolfn ClosureInterpreter::set_immutable_variable(self : ClosureInterpreter, name : String, value : RuntimeValue) -> Unitfn ClosureInterpreter::set_mutable_variable(self : ClosureInterpreter, name : String, value : RuntimeValue) -> Unitfn ClosureInterpreter::value_in_range(self : ClosureInterpreter, value : Expr, lhs : Pattern, rhs : Pattern, inclusive : Bool) -> Booltype MoonBitVMpub type RefId Intfn RuntimeStore::update_struct_field(self : RuntimeStore, id : RefId, field : String, value : RuntimeValue) -> Boolpub enum RuntimeValue {
Unit
Bool(Bool)
Int(Int)
Double(Double)
Char(Char)
String(String)
Tuple(Array[RuntimeValue])
Array(Array[RuntimeValue])
StructRef(RefId)
Function(Func)
}impl Show for RuntimeValuefn find_variable(immutable_values : Array[Map[String, RuntimeValue]], mutable_values : Array[Map[String, RuntimeValue]], name : String) -> RuntimeValuefn update_mutable_variable(mutable_values : Array[Map[String, RuntimeValue]], name : String, new_value : RuntimeValue) -> Bool