CSC 533: Programming Languages
Spring 2019

HW3: Implementing 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 in that scope). 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. That is, if the variable is declared within the current scope, the corresponding value should be accessed. If it is not declared in the current scope, the enclosing scope should be checked next, then the scope that encloses that scope, etc.

To implement nested scopes, you will need to modify the MemorySpace class 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 when executing a control statement. For example, when executing a repeat statement, a new scope must begin before each loop pass (so that any variable declarations are local to that loop), and that scope must be ended when the loop pass is completed. The methods for declaring, 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)
  >>> repeat 2 {
        var temp = "inside"
        output { "temp" "=" temp }
      }
  temp = inside 
  temp = inside 
  >>> var temp = "outside"
  >>> output { "temp" "=" temp }
  temp = outside 
  >>> var x = "global"
  >>> repeat 4 {
        output x
        var x = "local"
        output x
      }
  global 
  local 
  global 
  local 
  global 
  local 
  global 
  local 
  >>> output x
  global 
  >>> var test = "outer"
  >>> repeat 1 {
        var test = "middle"
        repeat 1 {
          var test = "inner"
          output test
        }
        output test
      }
  inner
  middle   			
  >>> output test
  outer        
  >>> var i = 1
  >>> while ( i <= 5 ) {
        var sum = 0
        var j = 1
        while ( j <= i ) {
          sum = ( sum + j )
          j = ( j + 1 )
        }
        output { i sum }
        i = ( i + 1 )
      }
  1 1
  2 3
  3 6
  4 10
  5 15
  >>> output i
  6
  >>> if ( i > 0 ) {
        var double = ( 2 * i )
        output double
      }else{
        var double = ( 2 * ( 0 - i ) )
        output double
      }
  12
  >>> i = ( -1 * i )
  >>> output i
  -6
  >>> if ( i > 0 ) {
        var double = ( 2 * i )
        output double
      }else{
        var double = ( 2 * ( 0 - i ) )
        output double
      }
  12
  >>> var double = "new"
  >>> output double
  new