Implementing CATCH and OOPS

(define (eval-catch exp env)
  ;; (CATCH name normal error)
  (let ((labels *labels*))
    (define (done)
      (set! *labels* labels)
      (eval (fourth exp) env))
    (add-label (second exp) done)
    (let ((result (eval (third exp) env)))
      (set! *labels* labels)
      result)))

(define (eval-oops exp env)
  ;; (OOPS name)
  (let ((label (find-label (second exp))))
      (if label
          (label)
          (error "No handler for error" name))))

The Code is Buggy

(catch george (oops george) 'george) ==>  george

(catch george (+ 2 3) 'george) ==> 5

(catch george
  (begin (oops george) (+ 2 3))
  'george) ==> 5 ; Should be GEORGE

Finding the Bug

(define (eval-sequence exps env)
  (cond ((last-exp? exps) (eval (first-exp exps) env))
        (else (eval (first-exp exps) env)
              (eval-sequence (rest-exps exps) env))))
Note: The call to eval is a subproblem of the call to eval-sequence. The call to eval returns an answer, and we go on to the next thing to do.

Fixing the Bug

Note: We change j-eval so that it never returns an answer. Instead, we pass it a third argument (its continuation) and it calls that when it has computed the answer.
(define (j-eval-block exp env next)
  (define (loop exprs)
    (if (null? exprs)
        (next "End of block")
        (j-eval (car exprs) env
                (lambda (val) (loop (cdr exprs))))))
  (loop (cdr exp)))

(define (j-eval-oops exp env next)
  ; Never calls NEXT!!!
  (let ((name (second exp)))
    (let ((label (find-label name)))
      (if label
          (label)
          (error "No handler for error" name)))))

Syntax of Decaf Scheme-In-Java, Part 1

JAVA DECAF JAVA-IN-SCHEME
3 3
foo foo
new foo() (new foo)
this.F (dot this f)
F(3) (call f 3)
this.F(3, "x") (call (dot this f) 3 "x")
(String) x + y (cast string (+ x y))
predicate ? consequent : alternative (? predicate consequent alternative)
x = 3 (= x 3)

Syntax, Part II

(BLOCK <statement> ...)
(BREAK)
or (BREAK <name>)
(CONTINUE)
or (CONTINUE <name>)
(DO <statement> <expr>)
(FOR <init statement> <boolean expr> <update> <body statement>)

or (FOR <variable declaration> <boolean expr> <update> <body statement>)
(IF <predicate expr> <consequent statement> <alternative statement>)
(LABEL <name> <statement>)
(RETURN)
or (RETURN <expr>)
(VARIABLES <type> (<variable declaration> ...) <statement>)
(WHILE <boolean expr> <statement>)

Syntax, Part III


(CLASS [(modifier ...)] <name>
       [(EXTENDS <name> ...)]
       [(IMPLEMENTS <name> ...)]
       <body form> ...)

(INTERFACE [(modifier ...)] <name>
           [(EXTENDS <name> ...)]
           <body form> ...)

Syntax, example

(class parent
  (field string ((= bar "Parent bar")))
  (method void foo ((string s))
    (block
     (print (+ s (+ ": Parent foo, " (dot this bar))))
     (newline))))
class Parent
{ String bar = "Parent bar;

  void foo (String S)
  { System.out.println(S + ": Parent foo,  " + this.bar);
  }
}