Due Thursday June 5 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));

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));

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);

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)

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)

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);



Amr A Sabry
Tue Apr 1 08:12:00 PST 1997