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:
AbstractInterpreterVisitor
contains the
environment (gammaG
and gammaM
),
the methods allocate()
and deallocate()
, and
all the interesting visit methods except the one for CallStmt
.
It also has the instance variables paramMap
and
bodyMap
, and the pass over the procedures to
populate them.
Finally, it has abstract methods (ie, signatures) for
sigma()
and sigmaU()
(for former retrieves state information, the latter updates it).
AbstractInterpreterVisitorNonPBN
,
which extends AbstractInterpreterVisitor
, contains
an implementation of the memory (mu
) as an array of integers,
and it defines the sigma()
and sigmaU()
methods to interact with the memory and environment.
InterpreterVisitorPBV
, which extends
AbstractInterpreterVisitorNonPBN
, implements the
visit method for CallStmt
using pass-by-value semantics.
InterpreterVisitorPBVR
, which extends
AbstractInterpreterVisitorNonPBN
, implements
the visit method for CallStmt
using pass-by-value-return
semantics; you must finish this class.
InterpreterVisitorPBRef
, which extends
AbstractInterpreterVisitorNonPBN
, implements
the visit method for CallStmt
using pass-by-reference
semantics; you must finish this class.
InterpreterVisitorPBN
,
which extends AbstractInterpreterVisitor
, contains
an implementation of the memory (mu
) as an array
of Expression
s.
Likewise, it implements the sigma()
and sigmaU()
methods.
In also implements the visito method for CallStmt
using
pass-by-name semantics.
You must finish this class.
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
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 Expression
s 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
.
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
.
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