Pep and Nom

home | blog | all blog posts

7 feb 2025

Things that compile themselves

For some reason, compiler writers and computer language developers take considerable pleasure in announcing, as soon as possible, that their language is now, officially, self hosting . There was a time when this idea was considered strange and exciting, so much so that somebody like Brian Kernighan (a c and Unix luminary) wrote a paper about it. These days its considered a fairly humdrum idea.

I think something similar can be said about the idea of “recursion” in programs; that is, a function or procedure which calls itself. For a brief period, programmers and language designers thought that this idea could provide some kind of “transcendental” self-referentiality. They don't anymore.

In light of all this, I don't expect any wild excitement (also because probably no-one reads this blog) when I say that the Nom language is self-hosting. That is to say, that the compiler for nom is written in nom. To generate a new compiler, I can just write:

 pep -f compile.pss compile.pss > asm.new.pp

and then copy asm.new.pp to asm.pp It's probably a good idea to check that asm.new.pp actually works as hoped before you do that.

But Nom is capable of even more wildly confusing self-referentiality. That is because not just the compiler can compile itself, but many other Nom scripts can also compile/transpile/translate themselves (choose whichever word you prefer). So all the Nom scripts in the translation folder can compile themselves, and this actually has a practical purpose.

create a java program that can compile Nom scripts to java

    pep -f translate.java.pss translate.java.pss > Machine.java
    javac Machine.java
  

So, in the code above the java nom “translator” has been used to translate the translator to java! I don't blame you if you feel slightly dizzy trying to think about that. But it works. Effectively we now have a Pep/Nom system which does not interpret but compiles (via the Java Virtual Machine).

now use this java compiler to compile a expression parser script to java

    cat eg/exp.tolisp.pss | java Machine > Machine.java
    javac Machine.java
    echo "(a+2)*3+4" | java Machine
    # will print the expression (a+2)*3+4 as Lisp code
  

The code above maybe particularly confusing because the java program needs to be called Machine (and the source Machine.java) because of java's compiler restrictions. But it still works. This problem doesn’t exist if you create the compiler using the Go translator for example.

The bash script helpers.pars.sh contains a set of functions to let you try out and test all these translators without having to type so much. For example

Translate the nom json parser to Go, compile and run with file input
 pep.goff eg/json.check.pss somedata.json

This blog post was pretty long and technical. I hope that you can see the strange garbled simplicity of the Pep/Nom system and that it inspires your language ideas.

That's all for today.

mjb