Skip to content
maarten-bootPublic

About

a simple programming language. (jff)

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

kukel

The Kukel logo: a shield of green and blue lines around a question mark, above the word Kukel

A simple programming language, just for fun: Kukel, with C-like syntax, classes, closures, generators and exceptions, compiled to bytecode for a small stack machine written in Go.

// shapes.kkl
class Shape { name; Shape(name); area(); describe(); }
Shape::Shape(name) {
    this->name = name;
}
Shape::describe() {
    return name + ": " + str(area());
}

class Circle : Shape { r; Circle(r); area(); }
Circle::Circle(r) {
    Shape::Shape("circle");
    this->r = r;
}
Circle::area() {
    return 3.14159 * r * r;
}

class Square : Shape { side; Square(side); area(); }
Square::Square(side) {
    Shape::Shape("square");
    this->side = side;
}
Square::area() {
    return side * side;
}

main(args) {
    local shapes = [new Circle(1), new Square(3), new Circle(2)];
    sort(shapes, fn(a, b) { return a->area() < b->area(); });

    for (local s in shapes) {
        println(s->describe());
    }

    try {
        println(shapes[5]);
    } catch (e) {
        println("caught", e->kind + ":", e->message);
    }
}
$ kukel run shapes.kkl
circle: 3.14159
square: 9
circle: 12.56636
caught RangeError: index 5 out of range for vector of length 3

What is in the language

  • ints (64-bit, wrapping), floats, strings (bytes), booleans, nil
  • vectors and maps (insertion order) with literals, indexing and for-each
  • functions with default and rest parameters, spread arguments (f(v..)) and named arguments (box(10, 20, label: "crate"))
  • function literals (fn (x) { ... }) that capture variables by reference
  • classes with single inheritance, constructors, static members, virtual member functions and bound methods (p->method)
  • try/catch/finally, throw, and runtime errors as Error objects
  • switch with fall-through, labelled break/continue
  • modules: use geometry; loads geometry.kkl, reached as geometry::name; modules written in Go: db (SQL with SQLite, PostgreSQL and MySQL, and objects in tables), encoding (base64 and hexadecimal), events (an event loop with timers), fs (open files read and written a line or a piece at a time, the standard streams, temporary files, locks, directories), json, net (TCP connections, Unix sockets, TLS and UDP packets), p9 (the 9P file protocol: a client, and a server of trees the program builds and of disk directories), re (regular expressions), strings (a string builder), task (tasks that run in turns in one interpreter, with channels between them: a server with a task for each connection), time, tk (desktop windows with widgets, menus, dialogs and a canvas; in a kukel built with the tag tk), term (the terminal a key at a time, and its size), toml (configuration files), utf8 (text by character instead of by byte), uuid, worker (interpreters that run in parallel and exchange messages) and zmq (ZeroMQ sockets, to talk with other programs and machines); and written in Kukel: iter (generators to make, combine and use up sequences: range, map, filter, take, zip, reduce, ...)
  • 56 built-in functions (strings, collections, math, files, ...)

New to Kukel? docs/tutorial.md is a ten-minute tour, and the booklet teaches the language and its modules from the first program on (each release also has it as a PDF). The full description is in docs/reference.md; the grammar is in syntax.ebnf (and as a Lark grammar in syntax.lark). How programs are compiled and run (the virtual machine, every instruction, the .kkc format) is specified in docs/vm.md.

Installing

With Go 1.25 or newer:

go install github.com/maarten-boot/kukel/cmd/kukel@latest

or from a clone:

make            # vet, test and build bin/kukel
make install    # install kukel into $GOBIN, else $(go env GOPATH)/bin

kukel holds everything (about 25 MB; 18 MB stripped, as the release archives are). Build tags leave parts out for a smaller binary: nopostgres, nomysql, nosqlite (all three: no module db), nozmq and no9p. With all five it is about 8 MB; with SQLite as the only database about 17 MB:

go install -tags nopostgres,nomysql github.com/maarten-boot/kukel/cmd/kukel@latest
make small                    # bin/kukel-small, all five tags
make small TAGS=nozmq         # other tags

The module tk (desktop windows, see examples/notes.kkl) is the other way round: it is only in a kukel built with the tag tk, as it adds about 19 MB (stripped, as the release archives are; 25 MB with go install). It needs no C compiler and no Tcl/Tk on the system (Tcl/Tk 9 come with it); its windows need a display:

go install -tags tk github.com/maarten-boot/kukel/cmd/kukel@latest
make tk                       # bin/kukel-tk

go install names the binary kukel either way, so the second install replaces the first in the Go bin directory. To keep both, install the one with tk into a directory of its own with GOBIN (and run it from there, or put that directory on your PATH):

GOBIN=$HOME/go/bin-tk go install -tags tk github.com/maarten-boot/kukel/cmd/kukel@latest
~/go/bin-tk/kukel version     # lists tk among the modules

kukel version lists the modules (and databases) a binary has.

Each release on GitHub also has ready-made archives of kukel, and of kukel-tk (with tk), for Linux, macOS and Windows (amd64 and arm64), with SHA256SUMS.

Docker

Each release is also a Docker image for linux/amd64 and linux/arm64, ghcr.io/maarten-boot/kukel (tagged with the version, and latest). It holds only kukel (on a distroless base, with CA certificates and time zones); programs come in through a volume mounted at /work, the working directory:

docker run --rm -v "$PWD:/work" ghcr.io/maarten-boot/kukel run prog.kkl -- args
docker run --rm -it -v "$PWD:/work" ghcr.io/maarten-boot/kukel repl
docker run --rm -p 5640:5640 -v "$PWD:/work" ghcr.io/maarten-boot/kukel run server.kkl

kukel runs as a user without privileges (uid 65532); to let a program write files into the mounted directory, run it as yourself: --user "$(id -u):$(id -g)". A server inside (9P, ZeroMQ) listens on tcp://*:port and is reached through -p. make docker builds the image for this machine as kukel:dev (IMAGE=... names it otherwise).

Using it

Command What it does
kukel run [-tasks wait|end] a.kkl b.kkl [-- args] compile the files as one program and run it; -tasks wait: when main returns, wait for its tasks
kukel a.kkl [args] the same as kukel run a.kkl -- args: a script that starts with #!/usr/bin/env kukel runs as a command
kukel check a.kkl ... compile only and report the errors
kukel build [-o prog.kkc] a.kkl ... compile to a bytecode file
kukel build -module [-o m.kkc] m.kkl compile a module on its own; use m; finds m.kkc when there is no m.kkl
kukel exec [-tasks wait|end] prog.kkc [--] [args] run a bytecode file
kukel disasm prog.kkc (or a.kkl ...) show the bytecode
kukel fmt [-w | -l] [-strict | -vertical] [-linelength n] a.kkl ... format source files (indentation, spacing, a blank line before if, for, ...); kukel fmt - formats standard input; -strict also adds the braces a statement of if, for, ... lacks, puts every function on several lines, and puts one argument per line in a call with more than 3 arguments, and one item per line in a call, definition or vector or map literal on a line longer than -linelength (120); -vertical does that for every call, definition and literal with 2 or more
kukel debug a.kkl ... [-- args] run a program under the debugger (breakpoints, stepping, values; it also stops where an exception nothing catches is thrown)
kukel profile [-o file] [-pprof file] [-cpu] a.kkl ... [-- args] run a program and report which functions and lines took the time, with call counts (also of workers); -pprof writes it for go tool pprof (flame graphs); Ctrl-C stops the program and writes the report
kukel dap the debugger for editors (the Debug Adapter Protocol): Visual Studio Code (with the extension in editors/vscode, make vscode), Neovim, Helix, Emacs; see docs/debugging.md
kukel repl [a.kkl ...] read declarations and statements and run them as they come
kukel tokens a.kkl ..., kukel ast [-pos] a.kkl ... show the tokens or the syntax tree
kukel version show the version and the modules written in Go
kukel help show the commands

tests/run has example programs: FizzBuzz, a JSON parser, word counting, closures, exceptions and more. Each comes with its expected output.

Embedding in Go

Go programs can run Kukel code, call its functions and give it Go functions of their own:

import "github.com/maarten-boot/kukel"

app := kukel.NewModule("app").Func("notify",
    func(in *kukel.Interp, args []kukel.Value) (kukel.Value, error) {
        log.Println(args[0])
        return kukel.Nil, nil
    })
prog, err := kukel.CompileFiles([]string{"rules.kkl"}, kukel.CompileOptions{
    Modules:      []*kukel.Module{app},   // rules.kkl: use app; ... app::notify(x);
    DenyBuiltins: kukel.SystemBuiltins,   // no files, no environment, no exit
})
in, err := kukel.New(prog, kukel.Options{StepLimit: 1_000_000})
result, err := in.CallContext(ctx, "discount", kukel.Int(250))

See docs/embedding.md and the package documentation.

How it works

source ──lexer──▶ tokens ──parser──▶ syntax tree ──compiler──▶ bytecode ──VM──▶ run
                                                                  │
                                                       .kkc file ◀┘ (build / exec)
Package Role
kukel (the repository root) the Go API for embedding (docs/embedding.md)
internal/token, internal/diag token kinds and source positions; the error type all stages share
internal/lexer, internal/parser, internal/ast source to syntax tree
internal/loader finds and reads the modules a program uses
internal/native, internal/modules/... modules written in Go (db, encoding, events, fs, json, net, os, p9, process, re, strings, task, term, time, tk, toml, utf8, uuid, worker, zmq) and native classes
internal/compiler name resolution, the language's rules, code generation
internal/bytecode instruction set, .kkc files, the verifier that checks loaded files
internal/vm the stack machine: calls, objects, closures, generators, exceptions (specified in docs/vm.md)
internal/debugger the debuggers' core: breakpoints, stepping, expressions (front ends: kukel debug, kukel dap)
internal/value, internal/builtins values with their operators, built-in functions
examples/ demo programs, such as a grep, and a Go program that embeds kukel (examples/README.md)
editors/vscode the Visual Studio Code extension: highlighting and debugging (docs/debugging.md)
editors/geany a filetype for Geany: highlighting, the symbol list, the Build menu (editors/geany/README.md)

TODO.md holds the plan the interpreter was built from, with all design decisions (Q1–Q107). Ideas for later are in docs/future.md. What changed in each release is in CHANGELOG.md, what stays working within v1 in docs/compatibility.md; all the documents are listed in docs/README.md. How to check a change (the subsystems, what else a change touches, and when a full cross-check is due) is in docs/checks.md.

Development

Make target
make gofmt check, go vet, tests, build (each also on its own: make vet, make test, make build). The test of syntax.lark (it parses the examples, the test programs and the programs of the documents) needs Python with lark (pip install lark), else it skips
make install install kukel into $GOBIN, else $(go env GOPATH)/bin
make fmt, make clean gofmt -w the sources; remove bin/ and dist/, and the cached test results
make test-all everything: vet, the tests of the full, the smallest and the tk build, and the tests with the race detector
make small, make test-small build bin/kukel-small, or run the tests, without the optional parts (TAGS=... picks other tags for small; make vet also vets the smallest build)
make tk, make test-tk build bin/kukel-tk, or run the tests, with the module tk (make vet also vets it). Its tests need a display or Xvfb (else they skip), and click, type and drag with xdotool when it is there
make fuzz FUZZTIME=1m fuzz the lexer, parser, compiler, bytecode loader and verifier (what they accept runs in the VM with a step limit), json::decode, toml::decode, base64, time parsing, kukel fmt and the tasks' scheduler (each for 30s by default; FUZZTIME=200000x counts executions instead, and FUZZSLOW sets the three slow ones apart, as CI does)
make bench benchmarks
make golden rewrite the expected outputs in tests/ after an intended change
make vscode the Visual Studio Code extension (editors/vscode) as dist/kukel-vscode-<version>.vsix; needs Node.js (npx fetches vsce)
make docker the Docker image for this machine, kukel:dev (IMAGE=... for another name)
make dist VERSION=v1.2.3 the archives of a release in dist/: kukel and kukel-tk for Linux, macOS and Windows on amd64 and arm64 (PLATFORMS=... picks others), with SHA256SUMS
make booklet the booklet as one PDF, dist/kukel-booklet-<version>.pdf (tools/booklet); needs python3 and Pango (libpango-1.0-0), and installs the Python packages it uses in bin/booklet-venv
make booklet-proof a copy of the booklet to proofread, dist/booklet-proof-<version>.pdf: margins of 0.5 cm and a box around each page (CI keeps it with the booklet as the artifact kukel-booklet)

Releases: pushing a tag v1.2.3 runs .github/workflows/release.yml, which first runs the whole CI (ci.yml); when that passes, the archives of make dist and the booklet's PDF are published as the GitHub release of the tag. Its notes are the tag's section of CHANGELOG.md (## v1.2.3 (date)) and a link to the commits since the previous tag (tools/release-notes.sh TAG prints them; a tag with a - takes its version's section, or "Unreleased"); a tag without a section is not released. A release that already exists (made by hand) gets the archives added, replacing files of the same name, and keeps its notes. At the same time the Docker image is built for linux/amd64 and linux/arm64 and pushed to ghcr.io as :v1.2.3, and as :latest when the tag is the newest release (a tag with a -, such as v1.3.0-rc1, never is). The base images and BuildKit are pinned to digests; Dependabot proposes updates of the base images and of the actions (BuildKit, named in a script of the workflow, is updated by hand).

The tests of db also run on database servers when KUKEL_TEST_POSTGRES and KUKEL_TEST_MYSQL name one (a URL as db::open takes it; the tables item, tag, plain, customer, order, line and doc there are dropped: use a database for tests only), for example with Docker:

docker run -d --rm --name pg -e POSTGRES_PASSWORD=pw -p 5432:5432 postgres:16
docker run -d --rm --name maria -e MARIADB_ROOT_PASSWORD=pw -e MARIADB_DATABASE=test -p 3306:3306 mariadb:11
KUKEL_TEST_POSTGRES="postgres://postgres:pw@127.0.0.1:5432/postgres?sslmode=disable" \
KUKEL_TEST_MYSQL="mysql://root:pw@127.0.0.1:3306/test" go test ./internal/modules/dbmod

License

MIT, see LICENSE.

About

a simple programming language. (jff)

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages