diff --git a/bootstrapped-primitives.rkt b/bootstrapped-primitives.rkt index 79b13d6..d313f2c 100644 --- a/bootstrapped-primitives.rkt +++ b/bootstrapped-primitives.rkt @@ -7,10 +7,12 @@ "find-toplevel-variables.rkt" "sets.rkt" "compile.rkt" + "typed-parse.rkt" racket/list) -(provide call/cc-label - make-call/cc-code) + + +(provide get-bootstrapping-code) @@ -66,3 +68,44 @@ ,(make-AssignPrimOpStatement 'proc (make-GetControlStackLabel)) ,(make-PopControlFrame) ,(make-GotoStatement (make-Reg 'proc))))))) + +(: make-bootstrapped-primitive-code (Symbol Any -> (Listof Statement))) +(define (make-bootstrapped-primitive-code name src) + (parameterize ([current-defined-name name]) + (append + (compile (parse src) (make-PrimitivesReference name) 'next) + ;; Remove the prefix after the Primitives assignment. + `(,(make-PopEnvironment 1 0))))) + + + + + +(: get-bootstrapping-code (-> (Listof Statement))) +(define (get-bootstrapping-code) + + (append + + (make-bootstrapped-primitive-code 'double + '(lambda (x) + (* x x))) + + (make-bootstrapped-primitive-code 'map + '(letrec ([map (lambda (f l) + (if (null? l) + null + (cons (f (car l)) + (map f (cdr l)))))]) + map)) + + ;; The call/cc code is special: + (let ([after-call/cc-code (make-label 'afterCallCCImplementation)]) + (append + + `(,(make-AssignPrimOpStatement (make-PrimitivesReference 'call/cc) + (make-MakeCompiledProcedure call/cc-label 1 '() 'call/cc)) + ,(make-AssignPrimOpStatement (make-PrimitivesReference 'call-with-current-continuation) + (make-MakeCompiledProcedure call/cc-label 1 '() 'call/cc)) + ,(make-GotoStatement (make-Label after-call/cc-code))) + (make-call/cc-code) + `(,after-call/cc-code))))) \ No newline at end of file diff --git a/compile.rkt b/compile.rkt index 6a601c9..ab45d73 100644 --- a/compile.rkt +++ b/compile.rkt @@ -12,6 +12,7 @@ (provide (rename-out [-compile compile]) compile-procedure-call append-instruction-sequences + current-defined-name adjust-target-depth) diff --git a/expression-structs.rkt b/expression-structs.rkt index 0b6c2d3..ca67e9b 100644 --- a/expression-structs.rkt +++ b/expression-structs.rkt @@ -37,11 +37,6 @@ #:transparent) - -#;(define-struct: Letrec ([names : (Listof Symbol)] - [procs : (Listof Lam)] - [body : ExpressionCore])) - (: last-exp? ((Listof Expression) -> Boolean)) (define (last-exp? seq) (null? (cdr seq))) diff --git a/package.rkt b/package.rkt index 3bafb4a..2769e66 100644 --- a/package.rkt +++ b/package.rkt @@ -18,12 +18,6 @@ ;; package: s-expression output-port -> void (define (package source-code op) - ;; The support code for call/cc - (for-each (lambda (code) - (displayln code op)) - (map assemble-basic-block - (fracture (make-call/cc-code)))) - ;; The runtime code (call-with-input-file* runtime.js (lambda (ip) @@ -31,6 +25,14 @@ (newline op) + ;; The support code for call/cc + (for-each (lambda (code) + (displayln code op)) + (map assemble-basic-block + (fracture (get-bootstrapping-code)))) + + (newline op) + (fprintf op "var invoke = ") (assemble/write-invoke (compile (parse source-code) 'val diff --git a/runtime.js b/runtime.js index 3a16aad..c5d5595 100644 --- a/runtime.js +++ b/runtime.js @@ -292,16 +292,17 @@ var Primitives = (function() { } } return true; - }, - - 'call/cc': new Closure(callCCEntry, - 1, - [], - "call/cc"), - 'call-with-current-continuation': new Closure(callCCEntry, - 1, - [], - "call-with-current-continuation") + } + +// , +// 'call/cc': new Closure(callCCEntry, +// 1, +// [], +// "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 8f2f54e..7474776 100644 --- a/simulator-primitives.rkt +++ b/simulator-primitives.rkt @@ -45,12 +45,12 @@ (make-undefined)] )))))])) -(define call/cc - (make-closure call/cc-label - 1 - '() - 'call/cc)) -(define call-with-current-continuation call/cc) +;(define call/cc +; (make-closure call/cc-label +; 1 +; '() +; 'call/cc)) +;(define call-with-current-continuation call/cc) (define e (exp 1)) @@ -154,7 +154,7 @@ symbol?) #:constants (null pi e - call/cc - call-with-current-continuation))) + #;call/cc + #;call-with-current-continuation))) diff --git a/simulator.rkt b/simulator.rkt index ce8e5e6..4c5c311 100644 --- a/simulator.rkt +++ b/simulator.rkt @@ -41,9 +41,7 @@ (let*: ([after-bootstrapping : Symbol (make-label 'afterBootstrapping)] [program-text : (Listof Statement) (cond [with-bootstrapping-code? - (append `(,(make-GotoStatement (make-Label after-bootstrapping))) - (make-call/cc-code) - `(,after-bootstrapping) + (append (get-bootstrapping-code) program-text)] [else program-text])]) diff --git a/test-assemble.rkt b/test-assemble.rkt index 4541a2c..61e61a6 100644 --- a/test-assemble.rkt +++ b/test-assemble.rkt @@ -44,15 +44,15 @@ (string-append "(function() { " - ;; The support code for call/cc - (string-join (map assemble-basic-block - (fracture (make-call/cc-code))) - "\n") - runtime - - "return function(success, fail, params){" snippet + ;; The support code for call/cc + (string-join (map assemble-basic-block + (fracture (get-bootstrapping-code))) + "\n") + + "return function(success, fail, params){" + snippet (format "success(String(~a)); };" inspector) "});")]) (displayln snippet) @@ -68,14 +68,14 @@ (display "(function() { " op) + (display runtime op) + (display (string-join (map assemble-basic-block - (fracture (make-call/cc-code))) + (fracture (get-bootstrapping-code))) "\n") op) - (display runtime op) - (display "var myInvoke = " op) (assemble/write-invoke a-statement op) (display ";" op)