diff --git a/simulator-primitives.rkt b/simulator-primitives.rkt index c2caaa0..51d3e46 100644 --- a/simulator-primitives.rkt +++ b/simulator-primitives.rkt @@ -34,11 +34,6 @@ 1 '())) - - - - - (define e (exp 1)) (define lookup-primitive (make-lookup #:functions (+ - * / = < <= > >= cons list car cdr diff --git a/simulator.rkt b/simulator.rkt index d07e2c0..12075f1 100644 --- a/simulator.rkt +++ b/simulator.rkt @@ -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)]))] diff --git a/test-compiler.rkt b/test-compiler.rkt index 64cc05c..7f73fc5 100644 --- a/test-compiler.rkt +++ b/test-compiler.rkt @@ -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) \ No newline at end of file