Pętlik is a small programming language, which syntax is defined by the following grammar with the start symbol Program:

Program → InstructionSequence
InstructionSequence → ε | InstructionSequence Instruction
Instruction → Increment | Repeat
Increment → Variable
Repeat → '(' Variable InstructionSequence ')'
Variable → 'a' | ... | 'z'

Instructions:

  • Increment: equivalent to ++variable in C, incrementing the variable by 1.
  • Repeat: equivalent to a C-style while loop:
    while (variable > 0) {
        --variable;
        ...
    }
    

The project consist of two components:

  1. An optimizing compiler that converts Pętlik programs into bytecode for a virtual machine.
  2. An interpreter for executing the bytecode.

Machine instructions:

  • INC Variable: increment the value of Variable by 1.
  • ADD Variable0 Variable1: add the value of Variable1 to Variable0.
  • CLR Variable: set the value of Variable to 0.
  • JMP Address: jump to the instruction at the given Address.
  • DJZ Variable Address: if Variable is 0, jump to the specified Address; otherwise, decrement Variable by 1.
  • HLT: halt execution.

The program supports printing the values of variables and running Pętlik programs.

Source code