cs173: Assignment 2

Implement the language with the following features:

numbers

Same as before.

binary arithmetic operators

In place of having separate rules for + and -, define a single syntactic rule for binary arithmetic operators. Parse these into a binop datatype variant. Define a table that maps operator names (symbols) to actual functions (Scheme procedures) that perform the corresponding operation. For now, define multiplication and division also (using * and / to represent them in the source). Having a single rule like this, accompanied by a table, makes your language easier to extend: once you have modified your parser and interpreter once to support binary operators, you won't need to touch either one to add any number of new ones.

multi-armed with

Implement a multi-armed with. Each identifier bound by the with is bound only in the body of the with expression. There will be zero or more identifiers bound by each with. Syntax:

  {with {{<id> <expr>}
         {<id> <expr>}
         ...} 
    <expr>}
   

conditionals

Add if0 using the syntax described in class. This saves the bother of adding boolean values and operators over them. Note that if0 has three branches: a test expression, a "then" expression which evaluates if the test expression evaluates to 0, and an "else" expression that evaluates otherwise.

multi-argument fun

Change the datatype so that a function has a list of arguments, not just one. All arguments to the function evaluate in the same environment. You may assume that the number of arguments in a function invocation matches the number in the procedure definition.

multi-armed rec

Using the datatype definition of environments, implement a multi-armed rec construct. Each named expression can access all the identifiers bound by the rec. The named expressions must all syntactically be functions. There will be zero or more identifiers bound by each rec. Syntax:

  {rec {{<id> {fun {<id>} <expr>}} 
        {<id> {fun {<id>} <expr>}}
        ...}
    <expr>}
   
Example:
  {with {{true 1}
	 {false 0}}
    {rec {{odd? {fun {n}
		  {if0 n
		       false
		       {even? {- n 1}}}}}
	  {even? {fun {n}
		   {if0 n
			true
			{odd? {- n 1}}}}}}
      {odd? 5}}}
   
should evaluate to
  1
   
representing truth.