In-class exercise: Parameter passing modes

1. Set up

Set up a directory for this exercise. Copy the base code for the FunJay interpreter.

mkdir parampass
cd parampass
cp ~tvandrun/Public/cs365/parampass/* .

Review the code from Friday as necessary. From a high level view, the classes we saw then and the classes we will finish today make the following hierarchy:

2. Pass-by-value-return

Look at the example program PBRef.funjay. Don't worry that the name of the example suggests it's for pass-by-reference; it will work for pass-by-value-return as well. Consider how it the results will be different based on whether pass-by-value or pass-by-value-return is used.

Finish the method visit(CallStmt) in InterpreterVisitorPBVR. The place where you see // ... is the only place where you will need to add code (at least to correspond to my solution).

Compile and then run the sample programing using each set of semantics.

java funjay.FunJay --pbv PBRef.funjay
java funjay.FunJay --pbvr PBRef.funjay

3. Pass-by-reference

Look at the program Sethi.funjay. What will the results be for pass-by-value and pass-by-value-return? Now consider both PBRef.funjay and Sethi.funjay using pass-by-reference semantics. What will the results be?

Now finish the code for InterpreterVisitorPBRef.visit(CallStmt). There are at least four places where you need to add to the code. Remember that we assume that all actual parameters are variables. Hence at some point we'll need to cast the actual parameters (which are Expressions according to the grammar) to Variable Notice the try/catch.

Compile and run PBRef.funjay using pass-by-reference, and run Sethi.funjay using all three semantics we have so far. To use pass-by-reference semantics, the flag is --pbref.

4. Pass-by-name

Study PBN.funjay and PBN2.funjay. How will they differ in their interpretation depnding on whether pass-by-name or pass-by-value is used?

In pass-by-name, we store expressions, not (necessarily) ints. Study the use of the variable mu and the sigmaU() methods in InterpreterVisitorPBN. Then implement sigma().

visit(CallStmt) must be updated in two places. Finish this, and then test pass-by-name and pass-by-value on both PBN.funjay and PBN2.funjay.

5. Turn in

To demonstrate what you've done, display the relevant files and execution of the examples in a typescript and print it out.

script
cat funjay/astvisitor/InterpreterVisitorPBVR.java
cat funjay/astvisitor/InterpreterVisitorPBRef.java
cat funjay/astvisitor/InterpreterVisitorPBN.java
java funjay.FunJay --pbv PBRef.funjay
java funjay.FunJay --pbvr PBRef.funjay
java funjay.FunJay --pbref PBRef.funjay
java funjay.FunJay --pbv Sethi.funjay
java funjay.FunJay --pbvr Sethi.funjay
java funjay.FunJay --pbref Sethi.funjay
java funjay.FunJay --pbv PBN.funjay
java funjay.FunJay --pbn PBN.funjay
java funjay.FunJay --pbv PBN2.funjay
java funjay.FunJay --pbn PBN2.funjay
exit
a2ps typescript

Thomas VanDrunen
Last modified: Tue Feb 16 14:21:35 CST 2010