Project: Krapu

fn main() {
    println("Hello, Krapu!");

So if you’re a programmer, surely you must write your own programming language at some point, right? I’m not sure about that, but at least I kind of did one in the spring of 2021. It’s called Krapu, because its syntax is almost copied from Rust and Rusts unofficial mascot is Ferris the crab. Given the name, file extension must be .krap, because that’s all what’s going to be written in the language. Nothing serious. This was just an exercise. Use it in production at your own risk.

Unlike Rust, Krapu is an interpreted language with optional strong static typing. The interpreter is slow, because it’s a tree-walking interpreter and I didn’t take performance into consideration much. I focused more on implementing language features.

So the reasons for me writing a programming language were:

  1. it was a task that intrigued me
  2. I’d get college credits for doing it
  3. I’d get to write it in Haskell

Implementing a small programming language like this removed some magic from what interpreters and compilers are doing. I acquired better understaing of programming language concepts and vocabulary as a result of reading the associated literature. I also got to try out programming concepts that were (and still are) foreign to me, most exotic was recursion schemes.

Haskell was an excellent choice for this task. Parser combinators are super handy way for writing recursive descent parsers. Strong typing prevented some bugs and helped me keep the code and my thoughts coherent. Tree transformations are really natural to express in haskell because of pattern matching and support recursion. Despite these good parts, some of the semantics were pretty tricky to implement in Haskell, for example continue and break. There’s no idiomatic alternative for these in Haskell. I ended up using monad transformers (ExceptT) and an additional state in the evaluation context.

I wrote quite a lot of tests from the very beginning. With hindsight, some of the tests were unnecessary, since it shouldn’t matter much what kind of a syntax tree is parsed from source code, rather than what it evaluates to. I’d write so called golden tests next time.

If you want to take a look at what the language looks like and maybe learn a little, check out learn.krap file in the GitHub repo.

The thumbnail image for this project is a public domain clip art picture (source) and it’s in no way related to the project apart from name.

Back to Projects