Project 4: Language Enhancements

Description

Now that you have a fully working front-end for a limited version of LOLcode, you will extend many of its features. These additions include the type "LETTR", "TROOF", "NUMBAR", the flow-control commands "O RLY", "IM IN YOUR LOOP", and "GTFO", and code blocks for the flow control statements, and proper short-circuiting for the other Boolean Logic operators. In addition to modifying your lexer, parser, and intermediate code output, these new features will require you to add in semantic checking for types and scope.

As with Project 3, your program must provide the function "generate_LMAOcode_from_LOLcode", which accepts a multiline string as its argument and returns a multiline string of LMAOcode. This function must be found in project.py, however you are welcome to split your code into multiple other python files, but you are responsible for ensuring they can work together to allow "project.generate_LMAOcode_from_LOLcode" to function as needed.

Error Reporting

Your compiler must still catch errors from previous projects as well as some new errors. For reference, the previous errors are:

  • Lexing Errors
  • Parsing Errors
  • Redeclaration Errors
  • Undeclared Errors

And the new errors associated with this project are:

  • GTFO Outside Of Loop Errors
  • Assignment Operator Type Mismatch Errors
  • Mathematical Operator With Non-Numeric Type Errors
  • Mathematical Operator Type Mismatch Errors
  • Logical Operator With Non-Boolean Type Errors
  • Flow Control Non-Boolean Conditional Errors

Of course, in all error cases you should raise the appropriate exception.

We recommend that you implement this project one piece at a time. Most of the new features are independent of each other, so you can add them in any order that you find easiest. I've broken them down into groups below.

Intermediate Code Generation (LMAOCode)

In addition to the LMAOcode instructions that you used for Project 3, you will also need to use labels and the jump instructions (JUMP, JUMP_IF_0, and JUMP_IF_N0) to successfully complete Project 4. Read through the Target Language Specification to get more information about the proper use of jump instructions.

Group 1: TROOFs and TROOF Literals

Allow variables to be declared as TROOF and ensure that WIN and FAIL tokens are treated as 1 and 0. You should update your symbol table to support type tracking of all variables, including s-values.

Group 2: Short Circuiting

You must also make sure that logical operators (BOTH, EITHER, ALL, ANY) all short-circuit. This means that when the result of a logical expression is known, later (right-ward) expressions are not evaluated.

For example:

I HAS A x ITZ A TROOF AN ITZ WIN
I HAS A y ITZ A TROOF AN ITZ WIN
I HAS A z ITZ A TROOF AN ITZ WIN
EITHER OF WIN AN x R FAIL   BTW EITHER shouldn't evaluate the assignment because it knows its result already.
ALL OF x AN y R WIN AN x R FAIL AN z R FAIL MKAY
BTW only the first three expressions are evaluated, because the third determine the answer
VISIBLE x y z  BTW Prints 011

Group 3: The "LETTR" Type

Up until this project LOLcode has had only one variable type called "NUMBR", but for this project you must implement multiple types, one of which is called "LETTR" (which holds a single character). This means that you now need to pay attention to Type Checking.

Your symbol table should keep track of the type of each variable. The 'LETTR' type is more limited than the 'NUMBR' type, and cannot be used in most expressions. The 'LETTR' type can only be used with assignment operator "R" and then only in the case that both sides of the operator are of the 'LETTR' type. LETTRs can also be used in comparison operators. In all other cases a type-checking error should be thrown.

You must also implement literal characters, represented in the source code by a character inside of single quotes, such as 'P'. Literal characters should behave similarly to literal integers, but are of type LETTR. Note that in addition to basic printable characters, you must support the newline character literal ':)', the tab literal ':>', the single quote literal ':'', the colon literal '::'. No other escape characters need to be supported.

You also need to add support for the GIMMEH expression, which yields a literal (using the LMAOcode command IN_CHAR).

Group 4: Flow Control Commands

You must implement two new flow commands: "O RLY?" and "IM IN YR LOOP" (associated with an optional "GTFO").

The O RLY? command must be followed by a an expression with the type TROOF. If the enclosed expression evaluates to true (WIN), the statements of the YA RLY code block that follows it should be executed. If the expression is false (FAIL), the previously mentioned code block should be skipped.

The YA RLY statements may optionally be followed by an NO WAI command and a second code block. The second statement is executed if and only if the condition expression evaluated to FAIL and the first statement was skipped.

The IM IN YR LOOP command denotes that code block of statements until the closing NOW IM OUTTA YR LOOP will be repeated forever.

Finally, the GTFO command is legal only inside of a loop. When executed, the GTFO command exits the lowest level (most nested) loop unconditionally.

For example:

I HAS A x ITZ A NUMBR AN ITZ 5
IM IN YR LOOP
    I HAS A y ITZ A NUMBR AN ITZ 0
    IM IN YR LOOP
        O RLY? FURSTSMALLR x y
            YA RLY
                GTFO
        OIC
        VISIBLE x y
        UPPIN y
    NOW IM OUTTA YR LOOP
    NERFIN x
    O RLY? FURSTSMALLR x 0
        YA RLY
            GTFO
    OIC
NOW IM OUTTA YR LOOP

The first "GTFO" command in the above code will cause execution to jump to the NERFIN x statement, if x is less than y.

The output from the execution of the above code is the numbers 50-55, 40-44, 30-33, 20-22, 10-11, and 00 (each on their own line).

Group 5: Code Blocks and Scope

You must allow flow control statements to support blocks of code with a more nested scope. Any variable defined inside of a code block will not be available after that code block ends (it will "fall out of scope"). You are not allowed to redeclare a variable name within the same scope, but you may redeclare in a different scope.

When two variables at different scope levels have the same name, the most recent (more nested) one should take precedence.

Testing Intermediate Code Output

As with the previous project, the interpreter.py will allow you to directly run LMAOcode so that you to test them.

See the instructions regarding testing your project on Piazza with the test_case.py file.

Submission

When you submit 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 the your name, a brief 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.
  • You may divide your project code into multiple files to do so you will need to use relative imports (if that doesn't mean anything to you, just keep everything in the project.py file).