Wolfie Script

💻 Peruse Some Source 💻

In Principals of Programming languages we were tasked with building a simple “console” parser that would take in a file line by line and execute the instructions given. This was an exciting opportunity to use the PLY python library which emulates the traditional lex and yacc library for C. At first the parser simply tokenized reserved symbols of my language and executed the action immediately. An example of what was supported at this stage would be:

a = [1,2,3]         # Variables
a[[0,1][0]]         # Accessing lists with lists
x = 2
y = 3
print(x+y)          # No output would be shown unless printed
{
  x = 5
  x                 # Shallow Scoping (would print 5 not 2)
}

The next part of the assignment was to add loops and conditional blocks. The issue with how the interpreter worked so far is that the immediate execution of a statement meant it could not be re-evaluated in a loops conditional. In addition, if-else statements are impossible since the grammar doesn’t support the tokenizing for it. The else block is unreachable with out some odd gimmick of manually jumping to the else phrase. The solution was to build an abstract syntax tree. With this data structure statements were stored in a Node such that this Node could be executed later. It was now possible to support:

a = true
b = false
if(a and b){
  print('true')
}else{
  print('false')
}
x = 0
while(x < 5){
  print(x)
  x = x + 1
}
print('I love wolfiescript!')

The assignment taught me a lot about how programming languages work. I look forward to learning more about compilers in my future education.

Avatar
Neal Beeken
Teacher's Assistant of System Fundamentals