getting call/cc working

This commit is contained in:
Danny Yoo 2011-03-11 19:33:24 -05:00
parent aecc0ad896
commit 0c497bef5c
3 changed files with 28 additions and 10 deletions

View File

@ -34,11 +34,6 @@
1
'()))
(define e (exp 1))
(define lookup-primitive (make-lookup #:functions (+ - * / = < <= > >= cons list car cdr

View File

@ -16,10 +16,13 @@
(provide new-machine can-step? step! current-instruction
current-simulated-output-port
machine-control-size)
(define current-simulated-output-port (make-parameter (current-output-port)))
(: new-machine ((Listof Statement) -> machine))
(define (new-machine program-text)
(make-machine (make-undefined) (make-undefined) '() '() 0 (list->vector program-text) 0))
@ -247,10 +250,13 @@
(ApplyPrimitiveProcedure-arity op)))])
(cond
[(primitive-proc? prim)
(target-updater! m (ensure-primitive-value (apply (primitive-proc-f prim)
m
(ApplyPrimitiveProcedure-label op)
args)))]
(target-updater! m (ensure-primitive-value
(parameterize ([current-output-port
(current-simulated-output-port)])
(apply (primitive-proc-f prim)
m
(ApplyPrimitiveProcedure-label op)
args))))]
[else
(error 'apply-primitive-procedure)]))]

View File

@ -544,5 +544,22 @@
1)
;; Reference: http://lists.racket-lang.org/users/archive/2009-January/029812.html
(let ([op (open-output-string)])
(parameterize ([current-simulated-output-port op])
(test (begin (define program (lambda ()
(let ((y (call/cc (lambda (c) c))))
(display 1)
(call/cc (lambda (c) (y c)))
(display 2)
(call/cc (lambda (c) (y c)))
(display 3))))
(program))
(void))
(unless (string=? (get-output-string op)
"11213")
(error "puzzle failed: ~s" (get-output-string op)))))
;(simulate (compile (parse '42) 'val 'next))
;(compile (parse '(+ 3 4)) 'val 'next)