Due Thursday, April 10, 11:59pm
Modify the interpreter a9start.ss (taken from the script10.) The new interpreter should accept C like call-by-reference procedures, besides the normal call-by-value procedures. For example:| Your interpreter | answer | C |
|---|---|---|
(let ([f (lambda (a b)
(set! a 100)
(set! (* b) 1000)
(+ a (* b)))])
(let ([x 1] [y 10])
(let ([fxy (f (& x) (& y))])
(+ fxy (+ x y)))))
| 2101 |
int f(int *a, int *b) {
a = (int *)100;
*b = 1000;
return((int)a + *b);
}
main() {
int x = 1;
int y = 10;
int fxy = f(&x, &y);
printf("%d\n", fxy+x+y);
}
|
(let ([x 2]
[y 3]
[swap
(lambda (x y)
(let ([*x (* x)])
(set! (* x) (* y))
(set! (* y) *x)))])
(swap (& x) (& y))
(- x 2))
| 1 |
swap(int *x, int *y) {
int tmp = *x;
*x = *y;
*y = tmp;
}
main() {
int x = 2; int y = 3;
swap(&x, &y);
printf("%d\n", x-2);
}
|
(let ([x 1])
(let ([f (lambda ()
(& x))])
(set! (* (f)) 10)
x))
| 10 |
int x = 1;
int *f() {
return(&x);
}
main() {
*f() = 10;
printf("%d\n", x);
}
|
(let ([x 1])
(let ([&x (& x)])
(let ([&&x (& &x)])
(let ([&&&x (& &&x)])
(set! (*(*(* &&&x))) 10)
(set! (*(* &&&x)) 100)
(set! (* &&&x) 1000)
(set! &&&x 10000)
(+ (+ x &x)
(+ &&x &&&x))))))
| 11110 |
main() {
int x = 1;
int *ax = &x;
int **aax = &ax;
int ***aaax = &aax;
***aaax = 10;
**aaax = (int *)100;
*aaax = (int **)1000;
aaax = (int ***)10000;
printf("%d\n", x+(int)ax+
(int)aax+(int)aaax);
}
|
(let ([x 1])
(let ([f (lambda ()
(& x))]
[id (lambda (x) x)])
(set! (* (f)) 10)
(id (* (f)))))
| 10 |
int x = 1;
int *f() {
return &x;
}
int id(int x) {
return x;
}
void main() {
*f() = 10;
printf("%d\n",id(*f()));
}
|