Home


interpret, How and Why

Introduction

interpret is a stack-based bytecode VM and single-pass compiler for Cambridge O Level Computer Science (2210) pseudocode, written in C#.

Technical jargon aside, interpret runs pseudocode as defined in the above mentioned syllabus, with DECLARE, FOR, CASE OF, arrays, procedures, functions, etc. running in the terminal, as a REPL or on the web. It compiles source to "bytecode", and executes it on a stack-based virtual machine. It is free and open-source (GPL-3.0).

This blog post details the parts of this software I enjoyed writing the most.

Backstory

I sat my O Levels in summer 2025. At the time, my peers and I were left with no choice but to write out and execute pseudocode completely by hand, a tedious and boring process. Our only alternative was this site, which I found also inconvenient and unfun.

interpret has gone through many versions. It started as a naive string reading algorithm which converted pseudocode to C, which would then have to be compiled and executed. I continued changing, tweaking and looking at different systems and algorithms by which to properly and fairly implement this language, so that students after myself may not have to undergo the monotony of writing code by hand.

Engineering process

In the beginning

Crafting Interpreters by Robert Nystrom was my starting point, a wonderful piece of literature, and must read for anyone interested in programming, which helped me see that the wizardry behind programming languages was far more comprehensible than it might seem.

In Crafting Interpreters, Nystrom defines Lox, which he implements in Java and C as jlox (implemented as a tree-walking interpreter) and clox (implemented as a bytecode virtual machine), respectively. Having walked (not entirely) through the two implementations, I obtained the following conclusions:

jlox:

clox:

Having decided what the interpreting process would look like under the hood, for each feature to be implemented, I ended up finding the following workflow best:

  1. Lookup and implement the syntax for that feature, such that it can be tokenised.
  2. If necessary, define opcodes for the feature.
  3. Try and work out the general form which the bytecode will take (using certain placeholders).
  4. Implement handlers within the parser to create the bytecode as such.
  5. For each opcode in the instruction set, write a handler that executes the code, as generally as possible.

Decisions made

Warning: Technical discussions to follow.

My journey with programming during the pandemic, through game development. Specifically through the Unity game engine. In the modern game development space, C# is ubiquitous and hence I developed a comfort with the language, its high level abstractions and simple build systems. I also enjoy that OOP can be treated as a suggestion to a comfortable extent. Garbage collection is a cherry on top of allowing me to create structs (or pseudo-structs), arrays, lists and dictionaries of which I have used extensively. Lastly, my codebase consists of static functions declared within static classes which are called from the program's entry point.

An overview

Simply, the interpreter is these three lines

tokens = tokenise(source)
instructions = parse(tokens)
run(instructions)

The tokens are well defined in the syllabus itself. I defined a custom instruction set and built a VM which runs it (the most fun part).

Each token is a structure consisting of two parts, its type (an enum TokenType) and its value. For example, the line

DECLARE x : INTEGER

would be tokenised as:

(DECLARE, DECLARE)
(IDENTIFIER, x)
(COLON, :)
(INTEGER, INTEGER)

This list of tokens is then fed into the parser, which goes through the code, emitting instructions or bytecode all while keeping track that syntax is in place. So the same line above would then be compiled into bytecode as

DEC (x, INT)

which is then executed by the virtual machine. The bytecode I have defined consists of an opcode, (DEC, in this case) and operand(s) (x and INT).

The above processes, namely tokenising, parsing and execution are all made more convenient by use of dictionaries through which I dispatched functions to handle particular keywords, tokens and opcodes, respectively. In code, I've named them Lookup tables.

Expression parsing

All of the above is simple enough, systems were the input set is consistent and almost always a one-to-one mapping with the output set. My biggest obstacle was expression parsing. In a tree-walk interpreter, expressions are parsed by means of trees that are arranged such that each node is either an operand or an operator, and the nodes which are operators, the branches are operands, and so on and so forth.

I find trees, or graphs, though convenient in certain cases and easy to intuit, difficult to implement and intuit within code. Fortunately, to parse expressions, I have a much more elegant, and simple alternative in the form of the shunting yard algorithm. I found much joy in coming across, learning and researching about the shunting yard algorithm, since, in my travels, I have found no other expression parsing algorithm that is as elegant in its simplicity (Crafting Interpreters made use of Pratt parsing, where recursion made intuition difficult).

My problem statement can be summarised as

Given an expression in infix notation, convert it to postfix

More information on infix and postfix notation may be found here. The significance of the conversion of an expression to postfix is that a VM can calculate an expression if written in postfix much more easily than in infix. For example, consider the line

OUTPUT (3+2) * 15

which compiles to the following bytecode

0 PSH 3
1 PSH 2
2 ADD 
3 PSH 15
4 MUL 
5 OUT 
6 NLN
7 EOF 

In my stack-based VM, I have an operand stack, to which the PSH instruction pushes, so by line 2, my operand stack has 3 and 2. When my VM reaches the instruction ADD, it adds the two topmost operands on the stack, and pushes the result back on top of the stack to be used for further calculations, by line 5, the stack has only one element, 75, which is popped and outputted to the command line by the instruction OUT. (NLN is the instruction to output a newline following some output).

Consider the above expression, in infix: (3+2)*15. The parser needs to turn it into the postfix order shown in the bytecode, and it does so with the mentioned shunting yard algorithm, found by Dijkstra. It reads the tokens once, from left to right, and uses a single extra stack, the OperatorStack, to hold operators which can't be emitted yet.

The rules are simple:

Tracing (3+2)*15:

Token What happens Emitted
( parse the contents as their own expression
3 operand PSH 3
+ nothing to compare against, wait
2 operand PSH 2
) inner expression ends, its waiting + is emitted ADD
* wait
15 operand PSH 15
end flush the waiting * MUL

This is exactly the bytecode listed above.

The reason this works so well is that the output is already in the order a stack machine wants. Every operator finds its operands waiting on top of the stack when it runs, so the VM knows nothing about precedence or brackets, and the parser never has to build a tree. Each instruction is appended to the program as soon as it is known, which is what makes interpret a single-pass compiler.

As a whole, given an expression of length nn, the algorithm runs in O(n)O(n) time.

Virtual machine

Consists of a program counter, which can be thought of as a pointer, pointing at a certain line or address in the instructions set, which it then executes using handler functions specific to each opcode. Above, I've described how an expression would be calculated and outputted as an example.

Open source?

interpret is licensed under GPL-3.0, the most open source of the open source licenses.

interpret is a tool for students. My userbase is kids studying for their O Levels who shouldn't have to pay royalties, make accounts or subscribe to such a service.

The syllabus is public, as should the tool be.

The open source part of the software is important so that issues, bugs, etc., when they pop up, can be looked at and pull requested and subsequently even fixed by teachers/students who come across them. Furthermore, the syllabus will always be updating, with which I may sometimes not be able to catch up with.

Bug reports and pull requests are welcome.