Swift Micro Interpreter2025

This project was made to learn more about how compilers work. I wrote a small interpreter in C++ that runs a part of SwiftA programming language made by Apple, mostly used to build apps for the iPhone and Mac.. It follows the traditional parts of a compiler frontend: a lexer, a parser and a typechecker. There is no backend that generates machine code, so the interpreter runs the program by traversing the ASTAbstract Syntax Tree. The program stored as a tree, where for example a plus has the two things it adds as its children..

First, FlexA tool that generates the code to cut a text into tokens, such as names, numbers and symbols. splits the code into tokens. Then a parser made with BisonA tool that generates a parser from the grammar rules of a language. builds the AST from them, based on a grammar. I struggled with the grammar, because Bison sometimes gave me shift/reduceA conflict where the parser cannot decide if it should read the next token or first apply a grammar rule to what it already has. or reduce/reduceA conflict where two grammar rules fit the same piece of code, so the parser cannot decide which one to apply. conflicts. The image shows every step for print(factorial(n: 5)), which prints 120.

Every step from the Swift code to the result
Two mistakes that the typechecker finds

After that, a typechecker traverses the AST to look for mistakes, like changing a let (const in other languages) or giving an Int to a Bool, and shows the line it is on. If it finds none, the interpreter traverses the AST again to run the program.

Functions were the hardest part for me. I learned that Swift functions use labels by default when you call them, which I was not used to. In Swift, the labels are part of the function signature, together with the types of the arguments, so f(x: 3) and f(a: 3) can be two different functions. I also had to make sure that a nested function can still use the variables of the function around it. Functions can call themselves too, like factorial here (recursion). In the tree, that call is just another node.

A recursive function and its tree

These are the parts of Swift that work:

  • Values: Int and Bool, with var and let. You can leave out the type, like var a = 5.
  • Math and logic: the usual operators in the right order. && and || skip the right side when it isn't needed.
  • Control flow: if, else if, else, while and do blocks, where an inner block can reuse a name from outside (shadowing).
  • Functions: labels, recursion, nesting and overloading.
  • Errors: syntax and type errors with line numbers, and an error for dividing by zero.

In real Swift, an assignment like a = 5 is also an expression that gives back an empty value. I kept it as a normal statement, because that was simpler.

Strings, arrays and for loops are not in there yet, but they would be cool to add as the next thing.

The labels and types decide which function is calledA nested function that uses a variable of the function around it
A block that reuses a name from outsideThe right side is skipped when it isn't needed
The order of the operators decides the shape of the tree
OpenGL Micro Engine OpenGL Micro Engine
V V

swift-micro-interpreter