Embeddable Starlark interpreter written in MoonBit
{
"deps": {
"tonyfettes/starlark": "0.1.0"
}
}let env = @starlark.new_environment()
let globals = @starlark.eval(
#|def greet(name):
#| return "Hello, " + name + "!"
#|message = greet("MoonBit")
,
environment=env,
)
println(globals["message"]) // String("Hello, MoonBit!")let env = @starlark.new_environment()
let result = @starlark.eval_expr("[x * x for x in range(5)]", environment=env)
println(result) // List({val: [Int(0), Int(1), Int(4), Int(9), Int(16)]})let env = @starlark.new_environment()
@starlark.set_builtin(env, "double", fn(args, _kwargs) {
match args[0] {
@value.Value::Int(n) => @value.Value::Int(n * 2L)
_ => fail!("expected int")
}
})
let globals = @starlark.eval("result = double(21)\n", environment=env)
println(globals["result"]) // Int(42)let loader : (String) -> String raise Error = fn(mod) {
if mod == "math.star" {
"pi = 3 # int-only, no floats\ndef square(x):\n return x * x\n"
} else {
fail!("module not found: " + mod)
}
}
let env = @starlark.new_environment(loader=Some(loader))
let globals = @starlark.eval(
#|load("math.star", "square")
#|result = square(7)
,
environment=env,
)
println(globals["result"]) // Int(49)| Feature | Status |
|---|---|
| while loops | Not implemented |
| lambda expressions | Not implemented |
| set type | Not implemented |
| float type | Not implemented |
| bytes type | Not implemented |
Source String -> Lexer -> Tokens -> Parser -> AST -> Evaluator -> Value| Package | Description |
|---|---|
| ast/ | AST node types (Expr, Stmt) with source spans |
| lexer/ | Python-like tokenizer with INDENT/DEDENT handling |
| parser/ | Recursive descent parser with Pratt precedence climbing |
| value/ | Runtime value types (Value enum) |
| eval/ | Tree-walk evaluator with closures and method tables |
| Root | Public API: eval(), eval_expr(), new_environment(), set_builtin() |
moon check # Type check
moon test # Run all tests
moon fmt # Format code
moon test --update # Update snapshot expectationsfn eval(source : String, environment~ : Environment, filename? : String) -> Map[String, Value] raiseEmbeddable Starlark interpreter written in MoonBit