CSC 533: Programming Languages
Spring 2022

HW4: Implementing Subroutines


The SILLY interpreter you wrote for HW3 extends the original version to allow for local variables and nested scopes. For this final SILLY assignment, you are to extend the language further by implementing simple subroutines (without return values).

In order to implement simple subroutines, you will need to define two new classes, a subDecl class for declaring a subroutine and a subCall class for calling a subroutine. The grammar rules for these new statements are:

<subdecl> --> 'sub' <id> '(' { <id> } ')' { <statement> } 'end' <subcall> --> 'call' <id> '(' { <expr> } ')'

Note that a subroutine declaration specifies the name of the subroutine, an arbitrary number of parameters in parentheses, and a seqeunce of statements specifying the body of the subroutine (terminated by 'end'). A subroutine call specifies the subroutine name and the sequence of expressions (in parentheses) that correspond to the parameters. A subroutine call should result in a run-time error is there is no declared subroutine with that name or if the number of expressions in a subroutine call does not match the number of parameters in its declaration. Note: every subroutine declaration is assumed to be in the global scope, regardless of where that declaration occurs.

When a subroutine is called, it defines a new, independent scope. Parameters are treated as local variables in that scope, initialized based on the expressions provided in the subroutine call. Because a subroutine's scope is independent, statements within a subroutine cannot access variables outside that subroutine.

For example:

SAMPLE CODE (output in red)
  >>> sub foo ( )
          output ( "foo" )
      end
  >>> call foo ( )
  foo
  >>> sub add ( val1 val2 ) 
          sum = ( val1 + val2 )
          output ( val1 "+" val2 "=" sum )
      end
  >>> call add ( 2 4 )
  2 + 4 = 6  
  >>> call add ( "foo" "bar" )
  foo + bar = foobar    
  >>> sub sumTo ( num ) 
          sum = 0
          while ( num > 0 )
              sum = ( sum + num )
              num = ( num - 1 )
          end
          output ( sum )
      end
  >>> call sumTo ( 10 )
  55 
  >>> call sumTo ( ( 10 * 10 ) )
  5050 
  >>> sub stamp ( word times ) {
          final = "" 
          repeat times 
              final = ( final + word )
          end
          output ( final )
      end
  >>> call stamp ( "foo" 3 )
  foofoofoo 
  >>> call stamp ( ( "X" + "Y" ) ( 4 * 2 ) )
  XYXYXYXYXYXYXYXY
  >>> sub hailstone ( n ) 
          if ( n == 1 ) 
              output ( "1" )
              output ( "STUCK" )
          else 
              output ( n )
              local rem = ( n % 2 )
              if ( rem == 0 )
                  n = ( n / 2 )
              else
                  local triple = ( n * 3 )
                  n = ( triple + 1 )
              end
              call hailstone ( n ) 
          end    
      end
  >>> call hailstone ( 5 )
  5
  16
  8
  4
  2
  1
  STUCK 
  >>> call hailstone ( 17 )
  17 
  52 
  26 
  13 
  40 
  20 
  10 
  5 
  16 
  8 
  4 
  2 
  1 
  STUCK

Note that executing a subroutine declaration does NOT result in its code being executed. Declaring a subroutine simply stores the code associated with that subroutine in memory so that it can be called later. You will need to add an additional field to the MemorySpace class corresponding to the code segment. When a subroutine declaration is executed, the parameters and code for that subroutine should be stored in the code segment. Subsequently, when a subroutine call is executed, the corresponding parameters and statements must be accessed from the code segment so that they can be executed.