This problem requires Lecture 21 and book section 3.8.
Exercise 3.53, p.114 - (90 minutes)
In lanugages supporting call-by-reference it is usual for call-by value to be supported also, with a method for specifying wich is to be used for each formal parameter. Extend the inplementation of this section in this way.
Use the following syntax:
| The BNF for <expression> (slightly modified) |
|---|
|
| The BNF for <formal> (New!) |
|---|
|
Note that "<formal>*(,)" denotes a comma-separated list of <formal> nonterminals.
Using this syntax, the semantics for the assignment should be to have
let a = 3
p = proc (x) set x = 4
in (p a)
pass a by value (a equals 3 when finished) and
let a = 3
p = proc (var x) set x = 4
in (p a)
pass a by reference (a is changed to 4).
Here's a useful way to re-write eval-rands for this assignment:
(define eval-rands
(lambda (formals rands env)
(map (lambda (form x) (eval-rand form x env)) formals rands)))Notice that eval-rands now takes as a parameter the list of formal parameters for the function you are about to pass the operands to. This lets you do the right kind of parameter passing (by value or by reference). (Note the use of Scheme's map function here: it is used to map a two-parameter function onto two lists: one call to eval-rand with the first element in both lists, a second call with the second element from both lists, etc.)
The rest is up to you. Make sure to think about
proc-exp, letrec-exp, and app-exp cases; and
eval-rand should do.You may also want to incorporate your "begin" expression code into this new interpreter to make testing your code easier. If not, you can use nested 'let's to emulate a "begin" like in the code below (which should return 11):
let a = 4
p1 = proc (var x) set x = 10
p2 = proc (var x) set x = +(x, 1)
in let tmp = (p1 a)
in let tmp2 = (p2 a)
in
Notice: Uninitialized string offset: 1 in /users/home2/ta/cs330ta/public_html/cs330/inc/codeformat.php on line 333
a
The interpreter with call-by-reference parameter passing.
Instructions on submitting homework is here
Last updated at 3:43 pm on Tuesday, January 20, 2004.