CSC 533: Programming Languages
Spring 2020

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 currently exist). When execution reaches the end of that scope, the local variable's lifetime ends. As before, declaring a variable that already exists, even if it was declared in a larger scope, should result in a runtime error. 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 times
        var temp = "inside" ;
        output "temp" , "=" , temp ;
      end
  temp = inside 
  temp = inside 
  >>> var temp = "outside" ;
  >>> output "temp" , "=" , temp ;
  temp = outside 
  >>> var x = "global" ;
  >>> repeat 4 times
        output x ;
        var y = "local" ;
        output y ;
      end
  global 
  local 
  global 
  local 
  global 
  local 
  global 
  local 
  >>> output x ;
  global 
  >>> var test1 = "outer" ;
  >>> repeat 1 times
        var test2 = "middle" ;
        repeat 1 times
          var test3 = "inner" ;
          output test1 , test2 , test3 ;
        end
        output test1 , test2 ;
      end
  outer middle inner
  outer middle   			
  >>> output test1 ;
  outer        
  >>> 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
  >>> output i ;
  6
  >>> if i > 0 then
        var double = 2 * i ;
        output double ;
      else
        var double = 0 - i * 2 ;
        output double ;
      end
  12
  >>> i = -1 * i ;
  >>> output i ;
  -6
  >>> if i > 0 then
        var double = 2 * i ;
        output double ;
      else
        var double = 0 - i * 2 ;
        output double ;
      end
  12
  >>> var double = "new" ;
  >>> output double ;
  new