- C 70.6%
- Python 14.1%
- JavaScript 11.3%
- POV-Ray SDL 1.4%
- HTML 1.4%
- Other 1.2%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| .claude | ||
| bench | ||
| include/tklips | ||
| openspec | ||
| py | ||
| src | ||
| stdlib | ||
| tests | ||
| third_party/linenoise | ||
| tools | ||
| web | ||
| .gitattributes | ||
| .gitignore | ||
| AGENTS.md | ||
| CLAUDE.md | ||
| Makefile | ||
| README.md | ||
tklips
A CLIPS-inspired rule language, designed incrementally. The engine is written in C23 and runs as a binary, and, compiled to WebAssembly, as a JavaScript module, a Python module and a web REPL.
The language is specified in openspec/specs/; this page only gives a
taste of it.
Build and run
Building needs a C23 compiler (gcc 15 or a recent clang), GNU make and
bash; the WebAssembly builds need clang with the wasm32 target, wasi-libc
and Node.js, and the Python module Python 3.10+ with wasmtime.
make # builds build/tklips
./build/tklips main.tk # runs a program file
./build/tklips < program # runs a program from stdin
./build/tklips # interactive shell; -i forces it, after a file
make js # build/web/tklips.mjs, the JavaScript module
make web # build/web/tklips.html, the web REPL
make py # build/py/tklips.py, the Python module
The binary also takes --allow-url (sources and files from http(s)
URLs), --stdlib dir and --seed n. Set WASM_CC to pick the clang of
the WebAssembly builds, for example WASM_CC=clang-19.
A taste of the language
Code is lists and atoms. A list calls the function at its head, with
positional arguments and named ones, (.name value); &name reads a
variable or a constant, and follows paths of keys into objects; *x and
*(expr) spread an object's items as arguments. A function's parameters
and body are separated by >>; fn makes a short function whose
parameters are the holes of its body, @ or @0 the first argument,
@1 the second, with paths as &name takes them. Values are numbers,
texts, objects and functions:
(func greet who (opt how Hi) >> (join &how " " &who))
(greet Ann) # → "Hi Ann"
(greet Bob (.how Bye)) # → "Bye Bob"
(set people (obj (obj (.name Ann) (.age 20)) (obj (.name Bob) (.age 12))))
&people.1.name # → Bob
(map &people (fn (if (>= @.age 18) adult minor)))
# → (obj adult minor)
(for (p &people) (print &p.name)) # Ann, then Bob
(max *(map &people (fn @.age))) # → 20, the ages spread as arguments
(try (str-len 42) (catch (e type/) &e.code)) # → type/not-text
(? join) # the help of join
func returns the function it defines, and with the name _ makes an
anonymous one. An fn takes one body, a call in parentheses or after
:, and keeps the values of the variables its body mentions:
(reduce (obj 1 2 3) (fn : + @ @1) 0) # → 6
(func add-all xs n >> (map &xs (fn (+ &n @))))
(add-all (obj 1 2) 10) # → (obj 11 12)
(set n 3)
(fn (+ &n @)) # → (fn (.n 3) (+ &n @))
(sort (obj 3 1 2) (.cmp (func _ a b >> (- &b &a))))
# → (obj 3 2 1)
Outside parentheses, a line and the lines indented under it make one
list, so most parentheses can go: a line that starts with an atom is a
call of its items, then of the lines under it; a line holding one atom
is that atom; a line that starts with ( reads as above. A blank line
ends the form, a # line doesn't:
func classify age >>
if (>= &age 18)
adult
minor
classify 20 # → adult
for (p &people)
print
join &p.name " is " (classify &p.age) # Ann is adult, then Bob is minor
: reads the rest of its line, or of its list, as one argument, and |
pipes a value through stages, each a call that takes it as its first
argument, or wherever @ stands; a line starting with | adds a stage:
print : str-upper : join a b # AB
&people | map (fn @.age) | max *@ # → 20, the ages spread
10 | - 100 @ # → 90
&people
| filter (fn (>= @.age 18))
| map (fn @.name) # → (obj Ann)
In the shell, Enter runs what you typed, and Alt+Enter or Ctrl+J go on with a new line, indented.
Macros
A macro adds a form to the language. It is declared like a function,
but its calls take their arguments as code, not as values, and are
replaced by its template, where ¶m stands for the code given for
a parameter and *rest for the code given for the rest:
macro unless cond *body >>
if (not &cond) (block *body)
unless (> &n 0)
print "nothing left" # runs when n is not above 0
A variable a template names itself is the call's own, never the caller's variable of that name; a macro reaches a caller's variable by taking its name as an argument:
macro swap a b >>
set tmp (get &a)
set &a (get &b)
set &b &tmp
set tmp 1
set x 2
swap tmp x # tmp is 2, x is 1
macro-for repeats a piece of template once per form, its binding
written like the form, with names where the parts vary; macro-if
picks a piece by whether forms were given; .&name, >&name and
>.&name build a named head or a capture from a parameter holding a
name. macro-expand writes what a call expands to:
macro defaults *fields >>
obj
macro-for ((.name value) &fields) (.&name &value)
.count (macro-if &fields some none)
defaults (.step 1) (.size (+ 1 2)) # → (obj (.step 1) (.size 3) (.count some))
(defaults) # → (obj (.count none))
macro-expand (defaults (.step 1)) # writes (obj (.step 1) (.count some))
Rules
Facts are a relation and an object body. A rule's conditions are patterns
over facts, which capture values with > and match them with &, and
marked conditions such as *none*; run fires the rules whose
conditions hold, once per combination of matching facts:
fact-add person (.name Ann) (.age 20)
fact-add person (.name Bob) (.age 12)
rule adult
person >.id >.name >.age (>= &age 18)
>>
print (join &name " is an adult")
fact-change &id (.adult 1)
rule minor
person >.name
*none* (person &.name (.adult 1))
>>
print (join &name " is a minor")
(run) # Ann is an adult, Bob is a minor → 2 firings
fact-match person (.adult 1) # → (obj 1)
Rules can also order themselves, support the facts they create, collect
aggregates and fire by chance; rule-agenda and its siblings show what
they would do.
Standard library
The standard library is written in tklips, in stdlib/: text, enum,
random and file. (std name) gives a library's source for
require:
(require (std text))
(text-pad-left "7" 3 "0") # → "007"
(text-wrap "the quick brown fox" 9) # → "the quick\nbrown fox"
(require (std enum))
(const COLOR (enum RED GREEN BLUE))
&COLOR.GREEN # → 1
From JavaScript
build/web/tklips.mjs, shipped with each release, is one ES module with
the engine inside, for Node 18+ and browsers:
import { createRuntime } from "./tklips.mjs";
const rt = await createRuntime({ stdout: (text) => console.log(text.trimEnd()) });
await rt.eval("(func greet name >> (join Hello \", \" &name))");
await rt.call("greet", "Ann"); // → "Hello, Ann"
rt.define("upper", (s) => s.toUpperCase());
await rt.eval("(upper (greet Bob))"); // → "HELLO, BOB"
From Python
build/py/tklips.py, shipped with each release, is the same engine in
one Python file, for Python 3.10+ with pip install wasmtime:
from tklips import Runtime
rt = Runtime()
rt.eval('(func greet name >> (join Hello ", " &name))')
rt.call("greet", "Ann") # → "Hello, Ann"
rt.define("upper", str.upper)
rt.eval("(upper (greet Bob))") # → "HELLO, BOB"
Layout
| Path | Holds |
|---|---|
src/lang/, include/tklips/lang/ |
the general-purpose language: reader, printer, evaluation, built-ins |
src/engine/, include/tklips/engine/ |
the rule engine on top of it: the fact store and its built-ins |
src/shell/, include/tklips/shell/ |
the interactive shell and the binary's sources |
src/main.c |
the command line, wiring the engine into the language |
web/ |
the JavaScript module and the web REPL |
py/ |
the Python module and its build script |
stdlib/ |
the standard library, written in tklips |
third_party/linenoise/ |
line editing for the shell, vendored with a patch |
tools/ |
the release compiler and the converters of breaking changes |
bench/ |
programs make bench times |
lang never depends on engine: the engine registers its built-ins with
builtins_register.
Tests
make test checks the doc table of the built-ins
(src/lang/help_docs.inc) against the built-ins, then runs the cases of
tests/cases/; make test-web and make test-py run them against the
JavaScript and Python modules, then the tests of tests/js/ and
tests/py/. Each case is one file, <name>.case, of sections: a line
=== <section>, then the section's content byte for byte, up to the next
such line:
=== input
(print hi)
(nope)
=== stdout
> (print hi)
hi
_
> (nope)
=== stderr
error: 2:1: unknown function: nope
=== exit
1
| Section | Required | Checks |
|---|---|---|
input |
yes | fed to the engine on stdin |
stdout |
no | stdout, byte-exact (defaults to empty) |
stderr |
no | stderr, byte-exact (defaults to empty) |
exit |
no | exit code (defaults to 0) |
args |
no | command-line arguments, split on whitespace, $ROOT standing for the repository root |
env |
no | lines of NAME=value added to the environment, $ROOT and $PATH expanded |
sigint |
no | one time in seconds per line: SIGINT is sent to the engine at each |
hold |
no | with sigint, the seconds stdin stays open after the input |
merged |
no | without sigint: stdout and stderr of a second run sent to the same file |
Content is raw bytes, invalid UTF-8 and carriage returns included; a
header written === <section> noeol says the content has no final
newline, and no content line may start with === .
tests/split_case.awk splits a case into its sections.
Cases run from tests/cases/, where the sources they require are;
tests/std/ holds a stdlib outside that directory, and io/ the files
the resource cases read, those they write going to io/out/, emptied at
each run. Cases that require libraries pass --stdlib $ROOT/stdlib. Set TKLIPS_BIN to
run the suite against another binary.
Design process
The language is specified with OpenSpec
(npx @fission-ai/openspec). Current specs live in openspec/specs/, proposed
changes in openspec/changes/. Each change ships with its tests.
In Claude Code: /opsx:propose to start a change, /opsx:apply to implement
it, /opsx:archive once it's done.