r/programming • u/realguy2300000 • 3h ago
kav: a purely functional systems language with a small & fast compiler implemented in C
https://srcdump.net/shrub/kavi’ve been working on this for a while and i think im ready to show it off a bit :)
i started working on this project after getting excited by functional programming and type systems after learning some haskell, followed by some ATS. kav takes ideas from both but it’s also quite different. the language makes uses of linear and dependent types, compiled with functional purity, which is both safe and fun to program in. however, i won’t go into too much detail about the language design but more about the compiler implementation.
the compiler is fairly compact (about 6000 lines, although liens aren’t everything it gives you a ballpark idea of the scale) and implemented in portable C11, using bidirectional type checking and a small fourier-motzkin solver for proving index constraints. it uses QBE as the backend, which is also fairly compact and implemented in C. this makes the compiler pretty fast, and easy to bootstrap even from one of the more primitive C compilers.
the lexer is generated using re2c, but most of the actual re2c regex is generated using an X-macro of the list of tokens, which makes it very easy to add new keywords to the language.
the languages runtime is also tiny and not dependent on libc or a C compiler, which means the binaries are tiny and linking is quick. it uses a small bump allocator with free-lists for allocation. this is written in C, and ten compiled to qbe using the cproc C compiler. then that generated qbe code is linked into every binary as the runtime allocator.
the compiler is very fast, it takes about 30ms on my machine to compile a fairly substantial amount of kav code (most of that is qbe/assembler/linker which are out of my control, the frontend only takes a few ms)
there is also a standard library, which is not finished, but has enough to write some fun programs (i’m working on a posix make implementation)
i’m pretty excited about this and i’d love more people to get involved or just to try it out.
3
u/BadlyCamouflagedKiwi 3h ago
Do you have some examples of the actual language? Your link goes to the repo which is full of C code - but how does kav work and what does it look like?
1
u/polycratia 3h ago
This is a really interesting project. I'm especially curious how the linear and dependent types feel in actual code. Do you have a few small examples showing what everyday programming in Kav looks like?
1
u/mllv1 2h ago
Nice language. Post in r/ProgrammingLanguages