This repository implements the core components of a small domain-specific language called Flower:
- A Python interpreter for a custom instruction set
- A lexer and language design
- A parser with static semantic analysis and concrete/abstract syntax trees
- A code generator that emits instructions executable by the interpreter
It showcases the full pipeline from source code to execution.
The Flower world is a 2D grid made of:
- Flowers
- Flower bed
- Grass
- The picker
- Entry point
- Exit point
The picker:
- Always enters and exits at fixed locations
- Moves around the grid trying to pick flowers from the flower bed
- When a flower is picked, it is replaced by grass
Programs written in Flower describe this behavior (movement, picking, control flow, functions, etc.).
The toolchain takes Flower source code through lexing, parsing, tree construction, code generation, and interpretation.
The interpreter uses a single global array Memory with 2000 words:
- Data memory: indices
0–999 - Instruction memory: indices
1000–1999
Additional structures:
data_index: start index of dataprogram_index: start index of program (first instruction)input_array: stores runtime inputssymbol_dict: maps symbols (variables) to data addresseslabel_dict: maps labels to instruction addresses
Main responsibilities:
-
Data loader
Reads data from the code file into the data section ofMemoryuntil a sentinel (+999999999) is reached.
Populatessymbol_dictwith addresses of symbols. -
Instruction loader
Reads instructions into the code section ofMemory.
When it encounters a label opcode (-7), it records the label and the address of the next instruction inlabel_dict. -
Decoder
Splits each instruction into:- Opcode
- Operands
-
Executor
Iterates over the instruction memory, decodes each instruction, and executes it according to the opcode semantics.
| File name | Purpose |
|---|---|
testQuiz.txt |
Quiz code including symbols and labels |
testAssignment.txt |
Computes sum, min, and max of n numbers |
test_pos4.txt |
Tests opcode +4 (EQL test) |
Test_pos2_neg2_pos4.txt |
Tests opcodes +2, -2, and +4 |
Test_neg5neg1.txt |
Tests opcodes -5 and -1 |
Expected results
testQuiz.txt→-3(minimum)testAssignment.txt→35,0,12(sum, min, max)test_pos4.txt→20Test_pos2_neg2_pos4.txt→200Test_neg5neg1.txt→4
How to run (interpreter)
-
Put the interpreter script and test files in the same folder.
-
Run, for example:
python interpreter.py testAssignment.txt