Skip to content

Latest commit

 

History

History
97 lines (67 loc) · 3.17 KB

File metadata and controls

97 lines (67 loc) · 3.17 KB

Flower Language

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.


1. Language Overview – “Flower”

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.


2. Interpreter (Python)

2.1 Memory Model & Data Structures

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 data
  • program_index: start index of program (first instruction)
  • input_array: stores runtime inputs
  • symbol_dict: maps symbols (variables) to data addresses
  • label_dict: maps labels to instruction addresses

2.2 Core Logic

Main responsibilities:

  • Data loader
    Reads data from the code file into the data section of Memory until a sentinel (+999999999) is reached.
    Populates symbol_dict with addresses of symbols.

  • Instruction loader
    Reads instructions into the code section of Memory.
    When it encounters a label opcode (-7), it records the label and the address of the next instruction in label_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.

2.3 Interpreter Test Files

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.txt35, 0, 12 (sum, min, max)
  • test_pos4.txt20
  • Test_pos2_neg2_pos4.txt200
  • Test_neg5neg1.txt4

How to run (interpreter)

  1. Put the interpreter script and test files in the same folder.

  2. Run, for example:

    python interpreter.py testAssignment.txt