Session 3 — Loops, main.py & First Running Program¶
What Was Built¶
Loop constructs, the command-line runner, and the first real Terse program executed from a .trs file.
Each Loop¶
Iterates over all nodes of a given type in the knowledge base.
The interpreter finds all nodes where is == animal — dog, cat, eagle — and runs the loop body for each, with creature bound to each node in turn.
How it works¶
execute_each() scans facts for all nodes matching the collection type, then runs the body statements once per match with the loop variable added to scope.
Design Decision
each x in animals matches by type — it finds all nodes where is == animals. This mirrors how NCI queries concept clusters — find everything related to a concept and process each one.
While Loop¶
Repeat while a condition holds.
The interpreter checks facts["running"]["is"] == "true" before each iteration. If true, runs the body. Repeats until false or the 100 iteration safety limit is reached.
Safety Limit
While loops are capped at 100 iterations during Phase 1 development to prevent infinite loops during testing. This limit will be removed or made configurable in a future phase.
Status
While loop parsing and interpreter execution are both implemented. End-to-end testing with a .trs file is planned for Session 5.
main.py — Running .trs Files¶
File: src/interpreter/main.py
The command-line runner. Reads a .trs file and runs it through the full pipeline.
run_file(filepath) opens the file, passes the source through the Lexer → Parser → Interpreter pipeline, and prints the results.
This is the moment Terse stopped being just code and became a runnable language.
First Real Terse Program — hello.trs¶
File: examples/hello.trs
The first real Terse program executed from a file. A demonstration of facts, relationships, inference rules, functions, and loops all working together.
// Hello World in Terse
// My first real Terse program
// Goatface Tech — 2026
know world is place
know world has life
know life is beautiful
know dog is animal
know dog has fur
know cat is animal
know cat has fur
know bird is animal
when has fur then is mammal
when is place then is real
each creature in animal
infer creature
each thing in place
infer thing
Running this file:
Produces inference results for all animals — dog and cat derive is mammal, bird stays as animal. World derives is real because it is a place.
Phase 1 Progress After Session 3¶
| Component | Status |
|---|---|
| Lexer | ✅ Complete |
| Parser | ✅ Complete |
| Interpreter core | ✅ Complete |
| Facts & relationships | ✅ Complete |
| Inference rules | ✅ Complete |
| Functions with scope | ✅ Complete |
| Markov chain learning | ✅ Complete |
| Each loops | ✅ Complete |
| While loops (parser) | ✅ Complete |
| While loops (e2e test) | 🔵 Next session |
| main.py | ✅ Complete |
| First .trs program | ✅ Complete |
| REPL | ✅ Complete |
| Error handling | ⚪ Planned |
| Tensors | ⚪ Planned |
| Compression keywords | ⚪ Planned |
| Ethics rule syntax | ⚪ Planned |
Interpreter completeness: ~75%