CSC 533: Programming Languages
Spring 2017

HW3: Implementing Locals and Scopes


The SILLY interpreter you wrote for HW2 provides the basic functionality of a scripting language. For this assignment, you will add simple scopes and local variables to the language. 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 -------------- ----------------------------------------------------------- Local new class that implements the declaration of one or more local variables in the current scope ... MODIFIED DESCRIPTION -------------- ----------------------------------------------------------- Statement added a case to getStatement to recognize a Local Token added "local" to the list of keywords ...


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 in which local variables can be declared. A Local statement, which declares one or more local variables in the current scope, is defined as follows:

<local> --> 'local' <identifier> { <identifier> } ';'

For example:

SAMPLE CODE (output in red)
  >>> x = 6
  >>> repeat 4
        local x ;
        x = "Dolly" ;
        output "Hello" + x ;
      end
  "HelloDolly" 
  "HelloDolly" 
  "HelloDolly" 
  "HelloDolly"
  >>> output x ;
  6  

  >>> word1 = "global1" ;
  >>> word2 = "global2" ;
  >>> word3 = "global3" ;
  >>> if word1 != word2
        local word2 word3 ;
        word2 = "middle2" ;
        word3 = "middle3" ;
        if true
          local word3 ;
          word3 = "inner3" ;
          output word1 + "_" + word2 + "_" + word3 ;
        end
        output word1 + "_" + word2 + "_" + word3 ;
      end
  "global1_middle2_inner3" 
  "global1_middle2_middle3"        
  >>> output word1 + "_" + word2 + "_" + word3 ;
  "global1_global2_global3" 

To implement nested scopes, you will need to modify the MemorySpace and/or StackSegment classes so that a stack of scopes is maintained (as opposed to a single, global scope). 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.

Finally, you will need to implement the Local statement, which declares one or more variables within the current scope (i.e., puts entries for those variables in the current activation record). Note: attempting to declare a variable that already exists in a given scope (e.g., declaring it twice) should result in a runtime error.