Examples
Script and host code
Compile a program once, then execute it with a compatible host context.
A script
fn clamp(value, low, high) {
if (value < low) { return low; }
if (value > high) { return high; }
return value;
}
var result = clamp(input, 0, 100);
return result;C++ host setup
thimble::HostContext host;
host.bind_value("input", 42);
auto program =
thimble::compile(source, "rules.thimble", host);
auto result =
program.value().execute(host);Try it locally
From the repository root, run python3 build.py. The test suite covers the lexer, parser, runtime, limits, callbacks and generated single-header distribution.
Passing a result back to C++
A top-level return becomes the value returned by Program::execute. Check the result before reading it.
auto program = thimble::compile(
"return left + right;", host);
if (!program) {
report(program.error());
return;
}
auto result = program.value().execute(host);
if (!result) {
report(result.error());
return;
}
auto total = result.value().as_int();
if (total) {
std::cout << total.value();
}Binding an existing free function
int add(int a, int b) {
return a + b;
}
// The typed convenience overload deduces the arity.
host.bind_function("add", add);Binding a class member
class Meter {
public:
int scale(int value) { return value * factor_; }
int factor_ = 3;
};
Meter meter;
host.bind_method("scale", meter, &Meter::scale);
// Or pass shared_ptr to retain the object.
auto owned = std::make_shared<Meter>();
host.bind_method("owned_scale", owned, &Meter::scale);Exposing an object to member syntax
auto request_type = host.define_object_type<Request>("Request");
request_type.property("amount", &Request::get_amount,
&Request::set_amount);
request_type.property("label", &Request::label);
request_type.method("approve", &Request::approve);
host.bind_object("request",
std::make_shared<Request>(), request_type);
// Script: request.amount and request.approve()Passing values between script functions
Script functions return a Thimble Value to their caller. Arguments are evaluated left to right and passed by value. A host callback also returns a Result<Value>, which either becomes the script expression value or becomes a structured runtime error.
fn calculate(value) {
return value * 2;
}
fn report(value) {
return calculate(value) + 1;
}
return report(input);Complete application examples
The repository contains a geometry program where C++ owns circles and collision calculations. A second policy program combines access checks, configuration validation and UI visibility, then returns the decisions in a map.
Run python3 build.py from the repository root to compile and execute both.