Implement 5.30; on to last section; let's go!

This commit is contained in:
2021-04-11 13:50:56 -04:00
parent 59b58cfb9a
commit f60931f80e
3 changed files with 67 additions and 10 deletions

View File

@@ -158,14 +158,38 @@
(display "\nex-5.30 - error-handling\n") (display "\nex-5.30 - error-handling\n")
; a
(set-register-contents! eceval 'exp (set-register-contents! eceval 'exp
'(begin '(begin
(define (foo x) x) (define (foo x) x)
(foo a))) (foo a)))
(start eceval) (start eceval)
(newline) (newline)
(assert (get-register-contents eceval 'val) 'unbound-variable-error)) (assert (get-register-contents eceval 'val) 'error-unbound-variable))
; b
(define (check-apply-division args)
(cond
((not (= (length args) 2)) 'error-div-wrong-number-of-args)
((= (cadr args) 0) 'error-div-by-zero)
(else 'ok)))
(set-register-contents! eceval 'exp
'(begin
(/ 1 0)))
(start eceval)
(newline) (newline)
(assert (get-register-contents eceval 'val) 'error-div-by-zero)
;; CONTINUE HERE (define (check-apply-car args)
(cond
((not (pair? (car args))) 'error-car-no-pair)
(else 'ok)))
(set-register-contents! eceval 'exp
'(begin
(car 3)))
(start eceval)
(newline)
(assert (get-register-contents eceval 'val) 'error-car-no-pair)

View File

@@ -185,10 +185,32 @@
(define apply-in-underlying-scheme apply) (define apply-in-underlying-scheme apply)
(define (check-apply-primitive proc args)
(cond
((eq? proc /) (check-apply-division args))
((eq? proc car) (check-apply-car args))
(else 'ok)))
(define (apply-primitive-error? val)
(eq? (car val) 'error))
(define (apply-primitive-result val) (cadr val))
(define (apply-primitive-procedure-safe proc args)
(let ((code (check-apply-primitive
(primitive-implementation proc)
args)))
(if (eq? code 'ok)
(list 'ok
(apply-in-underlying-scheme
(primitive-implementation proc)
args))
(list 'error code))))
(define (apply-primitive-procedure proc args) (define (apply-primitive-procedure proc args)
(apply-in-underlying-scheme (apply-in-underlying-scheme
(primitive-implementation proc) args)) (primitive-implementation proc)
args))
(define (prompt-for-input string) (define (prompt-for-input string)
(newline) (newline) (display string) (newline)) (newline) (newline) (display string) (newline))
@@ -203,7 +225,7 @@
(procedure-body object) (procedure-body object)
'<procedure-env>)) '<procedure-env>))
(display object))) (display object)))
;;; Simulation of new machine operations needed by ;;; Simulation of new machine operations needed by
;;; eceval machine (not used by compiled code) ;;; eceval machine (not used by compiled code)
@@ -312,5 +334,8 @@
;;5.30 ;;5.30
(list 'unbound-variable? unbound-variable?) (list 'unbound-variable? unbound-variable?)
(list 'apply-primitive-procedure-safe apply-primitive-procedure-safe)
(list 'apply-primitive-result apply-primitive-result)
(list 'apply-primitive-error? apply-primitive-error?)
)) ))

View File

@@ -36,8 +36,12 @@ unknown-procedure-type
(assign val (const unknown-procedure-type-error)) (assign val (const unknown-procedure-type-error))
(goto (label signal-error)) (goto (label signal-error))
unbound-variable-error error-unbound-variable
(assign val (const unbound-variable-error)) (assign val (const error-unbound-variable))
(goto (label signal-error))
apply-primitive-error
(assign val (op apply-primitive-result) (reg val))
(goto (label signal-error)) (goto (label signal-error))
signal-error signal-error
@@ -74,10 +78,9 @@ ev-self-eval
(assign val (reg exp)) (assign val (reg exp))
(goto (reg continue)) (goto (reg continue))
ev-variable ev-variable
;(perform (op user-print) (reg exp))
(assign val (op lookup-variable-value) (reg exp) (reg env)) (assign val (op lookup-variable-value) (reg exp) (reg env))
(test (op unbound-variable?) (reg val)) (test (op unbound-variable?) (reg val))
(branch (label unbound-variable-error)) (branch (label error-unbound-variable))
(goto (reg continue)) (goto (reg continue))
ev-quoted ev-quoted
(assign val (op text-of-quotation) (reg exp)) (assign val (op text-of-quotation) (reg exp))
@@ -137,9 +140,14 @@ apply-dispatch
(goto (label unknown-procedure-type)) (goto (label unknown-procedure-type))
primitive-apply primitive-apply
(assign val (op apply-primitive-procedure) (assign val (op apply-primitive-procedure-safe)
(reg proc) (reg proc)
(reg argl)) (reg argl))
;(perform (op user-print) (reg val))
;(perform (op newline))
(test (op apply-primitive-error?) (reg val))
(branch (label apply-primitive-error))
(assign val (op apply-primitive-result) (reg val))
(restore continue) (restore continue)
(goto (reg continue)) (goto (reg continue))