Homework #3

CS 152 | Fall 2008 | Due Date: 2008-10-23 23:59

The Assignment

Enhance the SL1 interpreter from Lecture 12 to a language SL2 with the following features:

  1. Support Boolean values true false and relational operators == != < <= > >= that return Boolean values. Operators should have the same precedence as in Scala. Report an error when a non-Boolean expression is used as an if condition. The < <= > >= are only defined for integers. == != are defined for integers and Boolean values, and they are not defined for function literals.
  2. Change the def construct so that it works like in Scala, for example
    def fac(x) = if (x > 0) x * fac(x - 1) else 1;

    A def fun(params) = can be followed either by a single expression or a block enclosed in { }.

  3. Make the variables mutable. Change val to var, and support an assignment expression ident~"="~expr. It changes the symbol table entry of ident to the value of expr. The symbol table should now be a list of instances of
    case class Binding(name: String, var value: Any)

    Unlike in Java, you need not support compound expressions without parentheses such as x = y = x + 2.

  4. Change the syntax for blocks so that a block is a sequence of def, var and simple statements (expressions followed by a semicolon), followed by an expression.

    Change the if construct so that the “then” and “else” parts can be either a single expression or a block enclosed in { }. (I should have done this in SL1.)

  5. Change the syntax for function literals so that the => is not required for function literals with no parameters
  6. Provide test cases test1.sl2, ... test5.sl2 for steps 1 ... 5.
  7. Write a SL2 function while that takes two function arguments, invoking the second while the first one returns true. Use it to write an iterative definition of factorial in SL2. Place it in a file fac.sl2.

    When you do this, you will find that SL2 is uncomfortably half-way between a functional language and a statement-oriented language such as C or Java. For example, if you have a block that you execute solely for its side effects, you still have to put a dummy expression (such as false) at the end.

Submission Instructions

Make a zip file with name hw3_lastname_firstname.zip, substituting your own names (duh). The extension must be in lowercase. The file must contain files hw3/Main.scala, test1.sl2, etc., with exactly this directory structure (i.e. hw3 being the only subdirectory after unzipping). My grading script

Bonus Points

5 points if you follow all of these instructions so that my grading script runs without requiring manual adjustments. Upload your solution to eCampus. You can submit as often as you like; only your last submission counts. Please note that eCampus will reject your submission after the due date.

For an additional 5 points, put at least one question or at least one answer about this homework onto the eCampus discussion group (starting 2008-10-08).