Nikita Popov's Capsule

New project

Some LISP for Hare?

So, about my new project.

His name is mice1. For he is small!

I thought, what am I missing as a programmer? That’s right, my own Scheme dialect. Of course, the point is not that I just want to write an interpreter. The fact is that the new wonderful programming language Hare2 lives without the ability to extend application logic with any scripting language. This can’t go on any longer! “How long!” I thought and sat down to work.

In fact, I expected that much would be unclear to me. But having some experience writing plugins for Emacs and a little experience in Hare itself, I had an idea where to approach it. I also found an excellent article3, which explains in a very basic but clear way what and how to do. I slightly reworked Hare lexer, wrote a small READ, tweaked EVAL. And before I could blink, my interpreter began accepting test code for calculating Fibonacci numbers for interpretation!

(define (fib n)
        (if (= n 2) 1
          (if (= n 1) 1
            (+ (fib (- n 1)) (fib (- n 2))))))

After that, I filed REPL itself and now it usable!

$ ./mice
mice-repl v0.0.0
λ => (define x 12)
12
λ => (+ x 25)
37
λ => (define y (+ x 25))
37
λ => (exit)
Bye!

Of course, there is still a lot of work ahead, but I can handle it. Initially I thought I would add one primitive per week. But implementing Scheme on Hare turned out to be so easy that in a short time I was able to add almost all the most basic primitives. I think I will have some difficulties with the garbage collector. Something tells me that no one has done this on Hare before me. All that remains is to read the documentation and code examples in good old C. Fortunately, Hare and C have comparable capabilities.

As an unexpected bonus, I received another piece of the “understanding technology” puzzle that I was putting together. Now I can say with confidence that I not only understand exactly how machines process commands and perform the operations described in them, but I have also implemented such a machine, albeit at a very basic level and not in hardware. But who knows, maybe someday I’ll get to a stack machine on FPGA.

In the meantime, wish me luck, because I have very big plans for this interpreter.

To be continued…