Lesson 8 — Standing on its own
Step 1: compiling
So far, nifla has run every program: it checks the program, then interprets it, line by line, itself. That is the kindest way to learn — but a program you want to hand to someone should stand on its own. That is niflac's work: it checks the program the same way, then compiles it into a program of its own, which runs with no interpreter beside it:
./niflac greeter.nifla -o greeter ./greeter Ada Byron
Compiled programs behave exactly as interpreted ones — same words, same refusals, same output — because the checker and the meaning are shared. (This book's proof holds every example to that: each is run both ways, and the outputs must agree.)
Feel what just happened, because it is the second half of the promise this book made in its opening. The words did not change — not one letter. What changed is what stands underneath them: the interpreter stepped aside, and the program now stands on its own, checked ahead of time and built to run at full strength. Written like JavaScript; running like Swift. Same language, both halves — and you have now held each one.
Step 2: the words the program was run with
A program that stands on its own usually wants to know what it was run with. arguments() gives the list of words that came after the program's name on the command line. You have been using it since Lesson 6; here is its close-up:
The book says it answers:
You brought 2 word(s). First: Ada Ada and Byron
This book's proof runs every example with the arguments Ada and Byron, which is why this program greets them. On your machine, run your own copy with your own name and see it greet you instead.
Your turn. Say the second word you were run with, with "Second: " in front of it.
The worked answer — try yours first, then peek
The book says it answers:
Second: Byron
Step 3: how a program ends
A standalone program also reports how it ended. When a program simply finishes, it hands back the number 0 — the number for "all went well". exitwith(code) ends a program early, handing back a number of your choosing; any number but 0 says something didn't go as hoped, and whoever ran your program can ask for the number and act on it:
The book says it answers:
Starting
The second say is never reached, and the 3 travels back to whoever ran the program. Lesson 9's doorman uses exactly this honesty: feathercheck answers 0 for a true card and 1 for a false one, so a script can ask "was it true?" without reading a word.
Your turn. Say "Opening the gate.", end with exitwith(2), and put a say after it that will never be reached.
The worked answer — try yours first, then peek
The book says it answers:
Opening the gate.
The ledger grows: the book, compiled
The ledger is ready to stand on its own. The whole book so far — gifts, tools, total — compiled once and run like any other program. And because a standalone program can be handed words, the book can greet its keeper:
The book says it answers:
Ada keeps this book. Total: 1120 cents
On your machine you would compile it once —
./niflac ledger.nifla -o ledger ./ledger Tyler
— and it would answer "Tyler keeps this book." The proof runs it with Ada, as everywhere in this book.
Your turn
Write a program that says the words it was run with, joined with " + ", and then — if it was brought fewer than 2 words — ends early with exitwith(1). Otherwise it says "Enough words." (This book's proof brings two words, so you will see the second road.)
The worked answer — try yours first, then peek
The book says it answers:
Ada + Byron Enough words.
You can now…
- compile a program with
niflacso it stands on its
own, behaving exactly as it did interpreted;
- read the words a program was run with, through
arguments();
- end a program early with
exitwith, and say how it
ended in a number — 0 for "all went well".
---