Project 3: Compiling to Intermediate Code

Description

This project finishes the front-end of your simplified compiler by translating the source code (LOLcode) into intermediate code (LMAOcode). Your compiler must have a function named "generate_LMAOcode_from_LOLcode" that takes a single argument, a multiline string of LOLcode. The multiline string returned by this function must be a legal LMAOcode, as described below.

We recommend that you implement this project in three stages:

  1. Make parser generate an abstract syntax tree (AST) that contains all of the information from the source file.
  2. Generate routines to traverse the AST in the correct order (postfix traversal). Start at the root and recursively call its child sub-trees in order. For most nodes, when it is reached you should process all of its child sub-trees first (assuming it has any) and then process that node itself. For debugging purposes, I recommend that the "processing" just consist of printing the name of each node.
  3. Finish the project by having each node output the appropriate LMAOcode when it is processed, and pass up through the tree any information that the parent node might need in order to make use of its results.

Short-Circuiting Logical Operators

Note that logical operators ('BOTH', 'ALL', 'EITHER', 'ANY') normally short-circuit such that if their result is fixed based on the first term of the operator, the second term does not execute (e.g. "a = (b=3) || (b=4)" would result in a=b=3.) While proper short-circuiting is not required (nor tested) in Project 3, it will be part of Project 4.

Restrictions

Not all of the LOLcode that was parsed in Project 2 will be in Project 3. For instance, none of the parts of LOLcode that deal with types other than NUMBR will be in Project 3. Also absent are the mathematical operators BIGGR and SMALLR.

Error Reporting

Your compiler must still detect errors from previous projects. And in the event of an error, your compiler should raise the an exception.

VISIBLE command

The VISIBLE command (in LOLcode) should output all values on a single line consecutively, and then place a newline at the end of the line (unless there is a exclamation point). For example, this code:

HAI 1.450
I HAS A a AN ITZ A NUMBR AN ITZ 2
VISIBLE 1 a
VISIBLE 13 AN PRODUKT OF a AN 5
KTHXBYE

should produce LMAOcode that that looks something like:

VAL_COPY 2 s6
VAL_COPY s6 s0
VAL_COPY 1 s7
OUT_NUM s7
OUT_NUM s0
OUT_CHAR '\n'
VAL_COPY 13 s8
OUT_NUM s8
VAL_COPY 5 s9
MULT s0 s9 s10
OUT_NUM s10
OUT_CHAR '\n'

The specific intermediate code will not be graded as long as it produces the correct output when executed with "interpreter.py". In the case of the code above, the output should be:

12
1310

LMAOcode (Intermediate Code)

LMAOcode syntax is detailed in the Target Language Specification, but at this point you don't need to pay attention to any of the instructions after NOP.

Testing Intermediate Code Output

The LMAOcode Interpreter (made available as "interpreter.py" in the starter code) will allow you to directly run LMAOcode so that you can test them. "interpreter.py" can be used via the command line and reads from stdin and writes the executed output to stdout. We recommend that you first create an input file with a .lmao extension. For example, if you wanted to compile and test the source program "mytest.lol" in your Project3/ directory, you would first output the resulting LMAOcode your compiler generates to a file, "mytest.lmao", then run:

$ python3 interpreter.py <mytest.lmao >mytest.out

Note: Your generated intermediate code does not have to match the sample code provided (though any large differences are probably a sign of a problem), but your final output when you run the intermediate code MUST match the expected output.

Handing In

When you hand in your program, you must include in a folder called Project3:

  • A project.py with the "generate_LMAOcode_from_LOLcode" function.
  • A README.txt file that includes and a summary of what does and does not work in your code. Your README may also include any notes that you want us to read before grading your program, such as any external resources you used in its development.