A small, typed expression language you can embed in Go, TypeScript and Rust. It is checked before it runs, and it cannot run away with your CPU or memory.
Risor v3 is a release candidate. Until 3.0.0, only fixes change the language, the specification and the APIs. Looking for Risor v2? It is released and maintained on the
v2branch. Existing imports ofgithub.com/deepnoodle-ai/risor/v2keep working.
Your application has data. Your users, your operators or an LLM want to write a little logic over it. Risor is the language for that logic. Your program gives it data and gets a value back.
// A rule
order.total > 100 && order.country == "US"
// A template
`Hello, ${user.name}!`
// A query
users.filter(u => u.age >= 18).map(u => u.name)If you know JavaScript or TypeScript, you can read Risor already. A longer
program names its values with const and ends with one expression:
const adults = users.filter(u => u.age >= 18);
`${len(adults)} of ${len(users)} users are adults`Native in three languages. Risor has three independent implementations, one each in Go, TypeScript and Rust. None of them is a binding to another language, and none uses WebAssembly or cgo. Each is idiomatic in its own language, and none of the three libraries has a run-time dependency. The same program gives the same result in all three.
Bounded CPU and memory. Each run has a limit on steps, on allocation, on wall-clock time and on call depth. These limits are part of the language. The step and allocation counts are exact, so a successful run reports the same usage in Go, TypeScript and Rust. You can run code that you did not write, and meter it.
Checked before it runs. A static checker is part of the language. A program that passes the checker cannot fail with an unknown name, a missing field, an unexpected null or a value of the wrong kind.
Small. No statements, no loops, no reassignment. if, match and
try are expressions. Values cannot be changed, and host data is read in
place, with no copy.
Defined by a specification. The language is defined by
spec/ and by a conformance suite of data files in
suite/. All three implementations pass the Core suite.
The checker knows the shape of your data. Say user is
{name: string, age: int, email: string | null}:
user.nmae
error[no-field] 1:6-1:10: {age: int, email: string | null, name: string} has no field "nmae"
user.email.toLowerCase()
error[maybe-null] 1:1-1:11: user.email may be null; its type is string | null
Write user.email?.toLowerCase() ?? "none" and the checker accepts it.
You find the mistake when the rule is saved, not when it runs.
A run that passes a limit stops with an error that names the limit:
range(0, 10000000)
error[steps-exceeded] 1:1-1:19: the run needed more than its limit of 1000000 steps
| Limit | Default | Bounds |
|---|---|---|
| Steps | 1,000,000 | CPU: the work a run does |
| Allocation | 1,000,000 units | Memory: the values a run makes |
| Deadline | Off | Wall-clock time |
| Depth | 200 | Recursion and nesting |
Steps, allocation and depth are deterministic: whether a program passes one
of these limits is the same in every implementation, on every run. The
deadline is not, because wall-clock time is not. A host can also cancel a
run. A catch in the program cannot catch a limit.
Declare what the program can see, compile it once, run it as often as you like. The three APIs have the same concepts and the same names.
go get github.com/deepnoodle-ai/risor/go/v3@latesttype User struct {
Name string `json:"name"`
Age int `json:"age"`
}
userType, err := risor.TypeOf[User]() // {age: int, name: string}
env, err := risor.NewEnv(risor.VarType("user", userType))
expr, err := risor.Parse(`user.age >= 18`)
prog, err := env.Compile(expr) // checks the program
out, err := prog.Run(ctx, risor.Map(map[string]any{
"user": User{Name: "Ada", Age: 36},
}))
// trueTypeScript's types do not exist at run time, so a TypeScript host declares the type in Risor's type syntax. It looks like a TypeScript type.
import { createEnv, parse } from "@deepnoodle/risor";
const env = createEnv({ vars: { user: "{name: string, age: int}" } });
const prog = env.compile(parse("user.age >= 18")); // checks the program
prog.run({ user: { name: "Ada", age: 36 } });
// true[dependencies]
risor = { git = "https://github.com/deepnoodle-ai/risor", features = ["derive"] }use std::collections::HashMap;
use risor::{parse, CompileOptions, Env};
#[derive(risor::Host)]
struct User { name: String, age: i64 }
let env = Env::builder().var_host::<User>("user").build()?;
let prog = env.compile(&parse("user.age >= 18")?, CompileOptions::default())?;
let vars = HashMap::from([("user".to_string(), User { name: "Ada".into(), age: 36 })]);
let out = prog.run(&vars)?;
// true#[derive(risor::Host)] lets a program read your structs in place. It needs
the derive feature, which adds one dependency: the project's own
risor-derive crate, which has none of its own.
risor.NewEnv(risor.Limits{Steps: 10_000, Allocation: 10_000, Deadline: time.Second})createEnv({ limits: { steps: 10_000, allocation: 10_000, deadlineMs: 1000 } });Env::builder().limits(Limits { steps: Some(10_000), allocation: Some(10_000),
deadline: Some(Duration::from_secs(1)), ..Limits::default() })| Implementation | Where | Package |
|---|---|---|
| Go | go/ |
github.com/deepnoodle-ai/risor/go/v3 |
| TypeScript | ts/ |
not yet on npm |
| Rust | rust/ |
not yet on crates.io |
# After the first CLI release tag (go/cmd/risor/vX.Y.Z):
go install github.com/deepnoodle-ai/risor/go/cmd/risor@latest
# From a clone of this repository:
cd go/cmd/risor && go install .
risor eval program.risor --vars data.json # run a program
risor check program.risor --vars data.json # check it without running it
risor fmt --write . # format programs
risor test # run case files
risor # the REPLAll three implementations pass the Core specification with the type
checker, and offer the same follow-on packages from
spec/packages/: JSON Schema, config files, templates,
queries and others. The newest additions are type annotations and type
lines, guards on match arms, walk for trees, typed lambdas that call
themselves, and helpers that take a function as a parameter. Recursive named
types and export const entry points are next.
We want to hear what works and what does not before 3.0. Open an issue about the language, the specification or an implementation.
| To read | Start with |
|---|---|
| Language guide (by example) | docs/guide.md |
| Host guide: Go | docs/hosts/go.md |
| Host guide: TypeScript | docs/hosts/typescript.md |
| Host guide: Rust | docs/hosts/rust.md |
| The standard library | docs/library/README.md |
| The language specification | spec/core/01-language.md |
| How a host embeds it | spec/core/03-host.md |
| The design and its decisions | experiments/v3/design/00-overview.md |
| The conformance suite | suite/README.md |
Migrating from expr and Risor v2 |
docs/migrating.md |
| Stability promise and versioning | docs/versioning.md |
| Benchmarks against expr | docs/benchmarks.md |