Interpreter
A programming language interpreter written in C
I created an interpreter from scratch in C. It was able to run code in a new programming language, and had support for complex features like loops, conditionals, functions, arrays, and more.
Design
The interpreter had three main parts:
- The lexer took in the raw text of the code and converted it into readable tokens.
- Then, the parser took those tokens and turned them into an Abstract Syntax Tree, which held all the statements and expressions to be evaluated and executed. There are various types of statements and expressions including variable definition statements, infix expressions, grouped expressions, and more.
- Finally, the program executes the code by evaluating the expressions and storing the results in objects. It has a map which holds the variables.
Example Code
This is an example of code that the interpreter can execute:
FUNCTION pow(a, b):
INT res = 1
WHILE b > 0
res = res * a
b = b - 1
RETURN res
INT x = 5
INT y = 6
IF y < 10
PRINT pow(x, y)