Examples and recipes
These assume a Mini-like config: let, fn, if, while, for, ;, { }, "strings", // comments. Adjust keywords to the language you generated.
Embed + run
#include <ReScripted.h>
#include "mylang.hpp" // from embed_language.sh
int main() {
rescripted::Engine engine;
engine.add_language(rescripted::Language::compile(rescripted::embed::mylang));
engine.bind("print", [](const std::vector<rescripted::Value>& args) {
for (const auto& a : args) std::cout << a.to_string() << '\n';
return rescripted::Value();
});
auto id = engine.load_file("scripts/hello.mylang");
engine.run(id);
engine.unload(id);
}
Script: functions and math
let x = 2 + 3 * 4;
fn double(n) { return n * 2; }
fn answer() { return double(x); }
After run, engine.get("x") is 14, engine.call("answer") is 28.
Script: host command + type + decorator
@export
command ping() {
return "pong";
}
entity Hero {
hp = 10;
name = "Ada";
}
Requires custom.function_kinds (command), custom.types (entity), custom.decorators (export).
Script: control flow
let n = 0;
while n < 3 {
n = n + 1;
}
if n == 3 {
print("ok");
} else {
print("bad");
}
for i 5 {
print(i);
}
for item [1, 2, 3] {
print(item);
}
for i 5 iterates 0..4. for item [1,2,3] iterates values.
Imports: C++ package, installed script, dotted file
JSON: imports.enabled, keywords (import or #banana), module_separator ".", bind_as "namespace".
add_executable(game src/main.cpp stdlib/math.c++)
engine.add_library("math", rescripted::library::math);
engine.add_library_file("Workshop", "packages/Workshop/main.mylang");
engine.add_search_path("packages");
import math
import company.app.library
import Workshop
let x = math.sqrt(9)
let n = library.times(3)
company.app.library loads company/app/library.mylang next to the importer (or on a search path). # keywords cannot also be line comments.
Hot reload
engine.unload("scripts/ai.mylang");
auto id = engine.load_file("scripts/ai.mylang");
engine.run(id);
One-shot
auto v = engine.eval("1 + 2;", "MyLang");
Inspect a compiled config
auto lang = rescripted::Language::compile(json);
auto keywords = lang.definition()["keywords"]["reserved"];
Mini demo in this repo
examples/mini/ — mini.json embedded into mini_host, hello.mini loaded at runtime. Copy the pattern, not the files into the user’s language folder unless they asked for Mini itself.