Due June 2, 1998 before class

In this assignment, you will study the semantics of objects, classes, inheritance, and exceptions in Java. The precise semantics of these constructs in Java is pretty messy so we will use an idealized version of Java. The concrete syntax of the language is explained in the book EOPL. Pay special attention to Chapter 7 about object-oriented languages and Chapter 9 about continuation-passing interpreters (this will help you in implementing exceptions). The parser for the language is available and as usual produces records that you can check with variant-case. So your job is to write an interpreter that converts the abstract syntax to a final answer. The directory idealizedJava contains all the help files needed and the beginnings of the interpreter.

To run the interpreter, run Chez Scheme, call the read-eval-print loop, and then type expressions to be evaluated by the interpreter. Again the syntax is explained in the EOPL book.

> (load "interp.ss")
> (read-eval-print)
--> +(5,2);
7
--> letrec fact = proc (n) 
                    if equal(n,0)
                    then 1
                    else *(n,fact(sub1(n)))
    in fact(5);
120

For your convenience here are some examples to try. The examples illustrate the concrete syntax of exceptions:

 
letexception multbyzero
in let mul = proc (x,y) if equal(x,0) 
                        then (raise multbyzero 0) 
			else if equal(y,0)
			     then (raise multbyzero 0)
			     else *(x,y)
   in +(mul(4,5),
        tryhandle multbyzero (proc (x) x) in mul(0,2));
==> 20

letexception multbyzero
in let mul = proc (x,y) if equal(x,0) 
                        then (raise multbyzero 0) 
			else if equal(y,0)
			     then (raise multbyzero 0)
			     else *(x,y)
   in tryhandle multbyzero (proc (x) x)
      in +(mul(4,5),mul(0,2));
==> 0

letexception mult1
in letexception mult2
   in let mul = proc (x,y) if equal(x,0) 
                           then (raise mult1 0)
			   else if equal(y,0)
			        then (raise mult1 0)
			        else *(x,y)
      in +(tryhandle mult1 (proc (x) x)
           in tryhandle mult2 (proc (x) (raise mult1 +(100,x)))
              in tryhandle mult1 (proc (x) (raise mult2 +(10,x)))
	         in mul(0,3),
	   10000);
==> 10110

define product = proc (list)
                   letexception foundzero
	           in letrec prod = proc (list)
				      if null(list)
				      then 1
				      else if equal(0,car(list))
					   then (raise foundzero 0)
					   else *(car(list),prod(cdr(list)))
                      in tryhandle foundzero (proc (d) 0) in prod(list);

product(cons(1,(cons(2,emptylist))));
==> 2

define product = 
  letexception toplevel in 
  proc (list)
   tryhandle toplevel (proc (d) 0) in 
   letexception foundzero
   in letrec prod = proc (list)
                      if null(list)
		      then 1
		      else if equal(0,car(list))
			   then (raise foundzero 0)
			   else *(car(list),prod(cdr(list)))
      in tryhandle foundzero (proc (d) (raise toplevel d)) in prod(list);

product(cons(1,(cons(2,emptylist))));
==> 2

newexception xxx in 
  let a = 3
  in let f = proc (x,y) if equal(x,y) 
	                then (raise xxx (proc () a))
		        else (proc () +(+(x,y),a))
     in let v=tryhandle xxx (proc (g) g()) in f(3,3);
            w=(tryhandlefinally xxx (proc (g) g) in f(1,4) finally (a:=99))();
            z=(tryhandlefinally xxx (proc (g) g) in f(5,5) finally (a:=999))()
        in +(+(v,w),z);
==> 1106

define a = class baseobject, (sv1, sv2) (iv1, iv2) 
	(initialize = method () begin &iv1 := 3; &iv2 := 4 end;
	 getsv1 = method () &&sv1;
	 getsv2 = method () &&sv2;
	 getiv1 = method () &iv1;
	 getiv2 = method () &iv2
	)
	begin &&sv1 := 1; &&sv2 := 2 end;

define b = class a, (sv3, sv2) (iv2, iv3)
	(initialize = method () begin 
			$initialize(super); &iv2 := 5; &iv3 := 6 
		      end;
	 getsv2 = method () &&sv2;
	 getsv3 = method () &&sv3;
	 getiv1 = method () &iv1;
	 getiv2 = method () &iv2;
	 getiv3 = method () &iv3
	)
	begin &&sv1 := 7; &&sv2 := 8; &&sv3 := 9 end;

define bi = simpleinstance(b);
...

Page visited times since May 19, 1998.

sabry@cs.uoregon.edu