COMP3002 Assignment 1

In this assignment you will be working with the simple calculator style compiler (actually, interpreter) available on the course web page. You are to extend the compiler in the following ways:

  1. [5 marks] Add numeric the mod operator, %, so that a % b evaluates to the value k in the range [0,b) such that a = rb + k for some integer r.
  2. [5 marks] Modify the compiler so that x** denotes x squared, x*** denotes x cubed, and so on.
  3. [5 marks] Add the constant phi that evaluates to the value of the golden ratio, φ.
  4. [5 marks] Add the constants true and false that evaluate to the boolean values true and false, respectively.
  5. [5 marks] Add the unary # operator that converts between numbers and boolean values. Boolean values true and false map to 0 and 1, respectively. Non-zero numeric values and zero map to true and false, respectively.
  6. [5 marks] Add the binary Boolean operators | (or) and & (and) and ^ (xor).
  7. [5 marks] Add the unary Boolean operator ~ (not)
  8. [5 marks] Add the unary negation operator - (minus)
  9. [5 marks] Add the arithmetic functions max(x1,x2,...,xn) and min(x1,x2,...,xn) that take a variable number of arithmetic expressions as arguments and evaluate to their maximum, respectively, minimum value.
  10. [5 marks] Add C-style if-then-else statements that can be of the form:
    if ( condition ) { expression-list } else { expression-list }
    
    or simply
    if ( condition ) { expression-list }
    

  11. [5 marks] Add the C-style choice operator, so that the expression a ? b : c evaluates to b if a is true and c if a is false.
  12. [5 marks] Add the C-style prefix increment and decrement operators ++ and --. That is, the code fragment ++a should have the same effect as (a = a + 1).
  13. [5 marks] Add the C-style postfix increment and decrement operators ++ and --. That is, the code fragment a++ should have the same effect as (a = a + 1), but evaluates to the value of a before the increment takes place. To see the difference, note you should compare the results of the following
        a = 5;
        x = a++;  /* Now x = 5 */
        a = 5;
        x = ++a;  /* Now x = 6 */
    
  14. [10 marks] Add function calls. A function is defined with the following syntax:
       function function-name (arg1, arg2, arg3, ..., argN) 
          { expression-list }
    
    A function is called with the syntax
       call function-name (arg1, arg2, arg3, ..., argN)
    
    A function may be defined anywhere within the main body of the program and can be called from anywhere (even before it is defined). Thus checking whether a non-existent function is being called can only be done once the entire input has been parsed.

    Note that the arguments to a function are variables that are local to that invocation of that function. This means that the execution environment must contain one stack frame per function that is currently executing. It will require non-trivial changes to the Environment class to implement this.