C311 Fall 1996 -- Programming Languages

Assignment 9: Call by value/Call by reference

Due Tuesday, November 26, 5:00P

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 interpreteranswerC
(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);
}	    

Submission

As before, but refer to "handin 9".
Gustavo Gomez / ggomezes@cs.indiana.edu
Last modified: Mon Nov 25 03:45:24 EST