changeLog.txtthat 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:
Currently, all of the variables in a SILLY program are in the same, global scope. You are to modify the control statements (
Repeat) so that they define new scopes for variables. That is, statements inside an
Repeat statement are considered to be inside a new, nested scope in which local variables can be declared. A
VarDeclaration statement, which declares one or more local variables in the current scope, is defined as follows:
SAMPLE CODE (output in red) >>> x = 6 >>> repeat 4 var ( x ) x = "Dolly" output ( "Hello" x ) end "Hello" "Dolly" "Hello" "Dolly" "Hello" "Dolly" "Hello" "Dolly" >>> output ( x ) 6 >>> word1 = "global1" >>> word2 = "global2" >>> word3 = "global3" >>> if ( word1 =/= word2 ) var ( word2 word3 ) word2 = "middle2" word3 = "middle3" if true var ( 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
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
VarDeclaration 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.