starlark

Embeddable Starlark interpreter written in MoonBit

starlark
interpreter
moon add tonyfettes/starlark@0.1.0
Download zip
Version
0.1.0
License
Apache-2.0
Last updated
4 months ago
Downloads
19
README

#tonyfettes/starlark

An embeddable Starlark interpreter written in MoonBit.

Starlark is a dialect of Python designed for configuration. It is used by Bazel, Buck2, and other build systems.

#Features

  • Core Starlark types: None, bool, int, string, list, tuple, dict
  • Functions with closures, default parameters, *args, and **kwargs
  • List/dict comprehensions
  • 25+ built-in functions (len, range, sorted, enumerate, zip, ...)
  • 50+ methods on strings, lists, and dicts
  • Module loading via host-provided callback
  • Custom built-in functions for host integration
  • 660+ tests including conformance tests against starlark-go

#Usage

Add the dependency to your moon.mod.json:

{ "deps": { "tonyfettes/starlark": "0.1.0" } }

#Evaluate a module

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!")

#Evaluate an expression

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)]})

#Register custom built-in functions

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)

#Load modules

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)

#Starlark Spec Coverage

This interpreter targets the core Starlark spec. The following features are not supported:

FeatureStatus
while loopsNot implemented
lambda expressionsNot implemented
set typeNot implemented
float typeNot implemented
bytes typeNot implemented

#Architecture

Source String -> Lexer -> Tokens -> Parser -> AST -> Evaluator -> Value

Six packages:

PackageDescription
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
RootPublic API: eval(), eval_expr(), new_environment(), set_builtin()

#Development

moon check # Type check moon test # Run all tests moon fmt # Format code moon test --update # Update snapshot expectations

#License

Apache-2.0

#
eval

fn eval(source : String, environment~ :
Environment
, filename? : String) -> Map[String,
Value
] raise

#
eval_expr

fn eval_expr(source : String, environment~ :
Environment
, filename? : String) ->
Value
raise

#
new_environment

fn new_environment(loader? : (String) -> String raise?) ->
Environment

Source Files

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io