CSC 533: Programming Languages
Spring 2018

HW3: Implementing Booleans and Scopes


As part of your submission, you must include a text file named changeLog.txt that identifies each class that you created or modified for this assignment, along with a short note describing the modifications. For example, your file might include: AUTHOR your_name_here CREATED DESCRIPTION -------------- ------------------------------------------------------------ If new class that implements if statements (with optional else) ... MODIFIED DESCRIPTION -------------- ------------------------------------------------------------ Statement added a case to getStatement to recognize an If Token added "if" and "then" to the list of keywords ...

Part 1: Completing the Basic Interpreter

There are two features of the basic SILLY language that were not implemented in HW2. For the first part of this assignment, you must complete the following:

Part 2: Adding scopes

Currently, all of the variables in a SILLY program are in the same, global scope. You are to modify the control statements (If, While and Repeat) so that they define new scopes for variables. That is, statements inside an If, While or Repeat statement are considered to be inside a new, nested scope. When a variable is declared, it is assumed to be local to the current scope (assuming it does not already exist). When execution reaches the end of that scope, the local variable's lifetime ends. When looking up the value of a variable, static scoping should be used.

To implement nested scopes, you will need to modify the MemorySpace and/or StackSegment classes so that a stack of scopes (variable/value maps) is maintained (as opposed to a single, global scope/map). New methods should be added to the MemorySpace class for beginning a new scope (i.e., pushing a new scope on the stack) and ending the current scope (i.e., popping the current scope off the stack). These methods should be called to begin and end the nested scopes when executing a control statement. The methods for storing and looking up variables in the stack segment will also need to be modified so that they properly access variable values from the stack.

SAMPLE CODE (output in red)
  >>> if true then
        output "OKAY"
      end
  OKAY
  >>> var count = 5
      if ( count < 0 ) then
        output "neg"
      else 
        if ( count > 0 ) then
          output "pos" 
        else
          output "zero"
        end
      end
  pos
  >>> var flag = true
  >>> var opposite = ( not flag )
  >>> output opposite
  false
  >>> output ( flag or ( not flag ) )
  true
  >>> var n1 = 9
  >>> var n2 = 4
  >>> var n3 = 0
  >>> while ( ( n1 > 0 ) and ( n2 > 0 ) ) do
        n1 = ( n1 - 1 )
        n2 = ( n2 - 1 )
        n3 = ( n3 + 1 )
        output { n1 "-" n2 } 
      end   
  8 - 3  
  7 - 2
  6 - 1
  5 - 0
  >>> output n3
  4  
  >>> repeat 4 times
        var x = "Dolly"
        output { "Hello" x }
      end
  Hello Dolly 
  Hello Dolly 
  Hello Dolly 
  Hello Dolly
  >>> var x = 33
  >>> output x
  33  
  >>> var i = 1
      while ( i <= 5 ) do
        var sum = 0
        var j = 1
        while ( j <= i ) do
          sum = ( sum + j )
          j = ( j + 1 )
        end
        output { i sum }
        i = ( i + 1 )
      end
  1 1
  2 3
  3 6
  4 10
  5 15        
  >>> var sum = 0
  >>> output sum
  0 
  >>> if ( i > 0 ) then
        var double = ( 2 * i )
        output double
      else
        var double = ( 2 * ( 0 - i ) )
        output double
      end
  12