diff --git a/runtime.js b/runtime.js index 474133d..12c3c02 100644 --- a/runtime.js +++ b/runtime.js @@ -129,6 +129,11 @@ var Primitives = (function() { var firstArg = MACHINE.env[MACHINE.env.length-1]; return firstArg[1]; }, + + 'not': function(arity, returnLabel) { + var firstArg = MACHINE.env[MACHINE.env.length-1]; + return (!firstArg); + }, 'null' : NULL, @@ -150,7 +155,11 @@ var Primitives = (function() { 'call/cc': new Closure(callCCEntry, 1, [], - "call/cc") + "call/cc"), + 'call-with-current-continuation': new Closure(callCCEntry, + 1, + [], + "call-with-current-continuation") }; })(); diff --git a/simulator-primitives.rkt b/simulator-primitives.rkt index 51d3e46..5f60338 100644 --- a/simulator-primitives.rkt +++ b/simulator-primitives.rkt @@ -33,9 +33,12 @@ (make-closure call/cc-label 1 '())) +(define call-with-current-continuation call/cc) (define e (exp 1)) + + (define lookup-primitive (make-lookup #:functions (+ - * / = < <= > >= cons list car cdr sub1 display newline displayln @@ -52,4 +55,4 @@ display displayln newline) - #:constants (null pi e call/cc))) + #:constants (null pi e call/cc call-with-current-continuation))) diff --git a/test-browser-evaluate.rkt b/test-browser-evaluate.rkt index ee9f597..cf866e3 100644 --- a/test-browser-evaluate.rkt +++ b/test-browser-evaluate.rkt @@ -93,3 +93,35 @@ ) "32") +(test '(begin + (define (ctak x y z) + (call-with-current-continuation + (lambda (k) + (ctak-aux k x y z)))) + + (define (ctak-aux k x y z) + (cond ((not (< y x)) ;xy + (k z)) + (else (call-with-current-continuation + (ctak-aux + k + (call-with-current-continuation + (lambda (k) + (ctak-aux k + (- x 1) + y + z))) + (call-with-current-continuation + (lambda (k) + (ctak-aux k + (- y 1) + z + x))) + (call-with-current-continuation + (lambda (k) + (ctak-aux k + (- z 1) + x + y)))))))) + (displayln (ctak 18 12 6))) + "7\n") diff --git a/test-compiler.rkt b/test-compiler.rkt index 7f73fc5..17721c2 100644 --- a/test-compiler.rkt +++ b/test-compiler.rkt @@ -561,5 +561,47 @@ (error "puzzle failed: ~s" (get-output-string op))))) + + +;; ctak +(test (begin + (define (ctak x y z) + (call-with-current-continuation + (lambda (k) + (ctak-aux k x y z)))) + + (define (ctak-aux k x y z) + (cond ((not (< y x)) ;xy + (k z)) + (else (call-with-current-continuation + (ctak-aux + k + (call-with-current-continuation + (lambda (k) + (ctak-aux k + (- x 1) + y + z))) + (call-with-current-continuation + (lambda (k) + (ctak-aux k + (- y 1) + z + x))) + (call-with-current-continuation + (lambda (k) + (ctak-aux k + (- z 1) + x + y)))))))) + (ctak 18 12 6)) + 7) + + + + + + + ;(simulate (compile (parse '42) 'val 'next)) ;(compile (parse '(+ 3 4)) 'val 'next) \ No newline at end of file