needmoreeasy EN 한국어
Learn

29 — Bootstrap: NME compiling a tiny language

★★★★☆ (4/5)bootstrap
Prerequisites
28 — Compiler, 23 — Modules
You will end up with
a tiny compiler written in NME

Guide 28 interpreted lines directly. The next step is the seed of a real compiler: a program that translates a tiny language into another language and runs the result. Writing a compiler in NME is called bootstrapping, and it is how every real compiler grows. The example examples/bootstrap.nme does exactly this.

Steps

  1. Define a tiny language with five instructions. BML (beginner mini language) keeps one instruction per line:

    set count 0
    while count 3
      show hello
      add count 1
    end
    show done
    
  2. Read it line by line and translate each instruction into Python. The show instruction becomes a print, set becomes an assignment, and while opens a block. Indentation tracks the block depth:

    # part of examples/bootstrap.nme
    lines = []
    indent = 0
    for raw in program:
        parts = raw.split()
        verb = parts[0]
        if verb == "set":
            lines.append(" " * indent + f"{parts[1]} = {parts[2]}")
        elif verb == "show":
            lines.append(" " * indent + f'print("{parts[1]}")')
        elif verb == "while":
            lines.append(" " * indent + f"while {parts[1]} < {parts[2]}:")
            indent = indent + 4
        elif verb == "end":
            indent = indent - 4
    
    run it →
  3. Join the translated lines and run them. Because NME itself runs on CPython, the compiler can execute what it produced:

    # part of examples/bootstrap.nme
    source = "\n".join(lines)
    exec(source)
    
    run it →
  4. Run the whole program:

    nme r examples/bootstrap
    
    generated Python:
    count = 0
    while count < 3:
        print("hello")
        count += 1
    print("done")
    running it:
    hello
    hello
    hello
    done
    

    A compiler written in NME translated BML to Python, and Python ran it.

Try it yourself

Add a sub <name> <int> instruction to the translator (-= in Python), then a BML program that counts down from 5 to 1.

What you learned

  • A compiler translates source text into another language, then runs it.
  • split() turns a line into words; f"..." builds the output line.
  • Indentation depth tracks nested blocks.
  • Writing a compiler inside NME is bootstrapping — the seed of self-hosting.

This page on GitHub