fixing assembly of basic blocks
This commit is contained in:
parent
d7554b2ae4
commit
c8c3947e0a
16
assemble.rkt
16
assemble.rkt
|
@ -10,17 +10,16 @@
|
||||||
assemble-statement)
|
assemble-statement)
|
||||||
|
|
||||||
|
|
||||||
;; assemble/write-invoke: (listof statement) output-port -> void
|
|
||||||
(: assemble/write-invoke ((Listof Statement) Output-Port -> Void))
|
(: assemble/write-invoke ((Listof Statement) Output-Port -> Void))
|
||||||
|
;; Writes out the JavaScript code that represents the anonymous invocation expression.
|
||||||
(define (assemble/write-invoke stmts op)
|
(define (assemble/write-invoke stmts op)
|
||||||
(let ([basic-blocks (fracture stmts)])
|
(let ([basic-blocks (fracture stmts)])
|
||||||
(fprintf op "function(success, fail, params) {\n")
|
(fprintf op "(function(success, fail, params) {\n")
|
||||||
(fprintf op "var param;\n")
|
(fprintf op "var param;\n")
|
||||||
(for-each (lambda: ([basic-block : BasicBlock])
|
(for-each (lambda: ([basic-block : BasicBlock])
|
||||||
(displayln (assemble-basic-block basic-block) op)
|
(displayln (assemble-basic-block basic-block) op)
|
||||||
(newline op))
|
(newline op))
|
||||||
basic-blocks)
|
basic-blocks)
|
||||||
(fprintf op "MACHINE.cont = function() {success(MACHINE.val)};\n")
|
|
||||||
(fprintf op "MACHINE.params.currentErrorHandler = function(e) { fail(e); };\n")
|
(fprintf op "MACHINE.params.currentErrorHandler = function(e) { fail(e); };\n")
|
||||||
(fprintf op #<<EOF
|
(fprintf op #<<EOF
|
||||||
for (param in params) {
|
for (param in params) {
|
||||||
|
@ -30,7 +29,7 @@ for (param in params) {
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
(fprintf op "trampoline(~a, function() {}, function(e) { MACHINE.params.currentErrorHandler(e)}); }"
|
(fprintf op "trampoline(~a, function() {success(MACHINE.val)}, fail); })"
|
||||||
(BasicBlock-name (first basic-blocks)))))
|
(BasicBlock-name (first basic-blocks)))))
|
||||||
|
|
||||||
|
|
||||||
|
@ -219,11 +218,14 @@ EOF
|
||||||
(format "return ~a();"
|
(format "return ~a();"
|
||||||
(assemble-location (GotoStatement-target stmt)))]
|
(assemble-location (GotoStatement-target stmt)))]
|
||||||
[(PushControlFrame? stmt)
|
[(PushControlFrame? stmt)
|
||||||
"fixme"]
|
(format "MACHINE.control.push(~a);" (PushControlFrame-label stmt))]
|
||||||
[(PopControlFrame? stmt)
|
[(PopControlFrame? stmt)
|
||||||
"fixme"]
|
"MACHINE.control.pop();"]
|
||||||
[(PushEnvironment? stmt)
|
[(PushEnvironment? stmt)
|
||||||
"fixme"]
|
(format "MACHINE.env.push(~a);" (string-join
|
||||||
|
(build-list (PushEnvironment-n stmt) (lambda: ([i : Natural])
|
||||||
|
"undefined"))
|
||||||
|
", "))]
|
||||||
[(PopEnvironment? stmt)
|
[(PopEnvironment? stmt)
|
||||||
"fixme"]))
|
"fixme"]))
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
"parse.rkt"
|
"parse.rkt"
|
||||||
"il-structs.rkt"
|
"il-structs.rkt"
|
||||||
racket/port
|
racket/port
|
||||||
|
racket/promise
|
||||||
racket/runtime-path)
|
racket/runtime-path)
|
||||||
|
|
||||||
(define-runtime-path runtime.js "runtime.js")
|
(define-runtime-path runtime.js "runtime.js")
|
||||||
|
@ -18,7 +19,7 @@
|
||||||
(with-syntax ([stx stx])
|
(with-syntax ([stx stx])
|
||||||
(syntax/loc #'stx
|
(syntax/loc #'stx
|
||||||
(begin
|
(begin
|
||||||
(printf "Running ~s ...\n" 'exp)
|
(printf "Running ~s ...\n" (syntax->datum #'expr))
|
||||||
(let ([actual expr])
|
(let ([actual expr])
|
||||||
(unless (equal? actual expected)
|
(unless (equal? actual expected)
|
||||||
(raise-syntax-error #f (format "Expected ~s, got ~s" exp actual)
|
(raise-syntax-error #f (format "Expected ~s, got ~s" exp actual)
|
||||||
|
@ -26,38 +27,76 @@
|
||||||
(printf "ok.\n\n")))))]))
|
(printf "ok.\n\n")))))]))
|
||||||
|
|
||||||
|
|
||||||
|
;; evaluating single expression
|
||||||
(define -E (make-evaluate
|
(define -E (delay (make-evaluate
|
||||||
(lambda (a-statement+inspector op)
|
(lambda (a-statement+inspector op)
|
||||||
(let* ([a-statement (car a-statement+inspector)]
|
(let* ([a-statement (car a-statement+inspector)]
|
||||||
[inspector (cdr a-statement+inspector)]
|
[inspector (cdr a-statement+inspector)]
|
||||||
|
[snippet (assemble-statement a-statement)]
|
||||||
[code
|
[code
|
||||||
(string-append
|
(string-append
|
||||||
"(function() { "
|
"(function() { "
|
||||||
runtime
|
runtime
|
||||||
"return function(success, fail, params){" (assemble-statement a-statement)
|
"return function(success, fail, params){" snippet
|
||||||
(format "success(String(~a)); };" inspector)
|
(format "success(String(~a)); };" inspector)
|
||||||
"});")])
|
"});")])
|
||||||
(display code op)))))
|
(displayln snippet)
|
||||||
|
(display code op))))))
|
||||||
|
(define (E-single a-statement (inspector "MACHINE.val"))
|
||||||
|
(evaluated-value ((force -E) (cons a-statement inspector))))
|
||||||
|
|
||||||
|
;; evaluating many expressions[.
|
||||||
|
(define -E-many (delay (make-evaluate
|
||||||
|
(lambda (a-statement+inspector op)
|
||||||
|
(let* ([a-statement (car a-statement+inspector)]
|
||||||
|
[inspector (cdr a-statement+inspector)])
|
||||||
|
|
||||||
|
(display "(function() { " op)
|
||||||
|
(display runtime op)
|
||||||
|
|
||||||
|
(display "var myInvoke = " op)
|
||||||
|
(assemble/write-invoke a-statement op)
|
||||||
|
(display ";" op)
|
||||||
|
|
||||||
|
(fprintf op
|
||||||
|
"return function(succ, fail, params) { console.log('here'); myInvoke(function(v) { console.log('there!');succ(String(~a));}, fail, params); }"
|
||||||
|
inspector)
|
||||||
|
(display "})" op))))))
|
||||||
|
(define (E-many stmts (inspector "MACHINE.val"))
|
||||||
|
(evaluated-value ((force -E-many) (cons stmts inspector))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(define (E a-statement (inspector "MACHINE.val"))
|
|
||||||
(evaluated-value (-E (cons a-statement inspector))))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
;; Assigning a number
|
;; Assigning a number
|
||||||
(test (E (make-AssignImmediateStatement 'val (make-Const 42)))
|
(test (E-single (make-AssignImmediateStatement 'val (make-Const 42)))
|
||||||
"42")
|
"42")
|
||||||
;; Assigning a string
|
;; Assigning a string
|
||||||
(test (E (make-AssignImmediateStatement 'val (make-Const "Danny")))
|
(test (E-single (make-AssignImmediateStatement 'val (make-Const "Danny")))
|
||||||
"Danny")
|
"Danny")
|
||||||
;; Assigning a cons
|
;; Assigning a cons
|
||||||
(test (E (make-AssignImmediateStatement 'val (make-Const (cons 1 2))))
|
(test (E-single (make-AssignImmediateStatement 'val (make-Const (cons 1 2))))
|
||||||
"1,2")
|
"1,2")
|
||||||
;; Assigning to proc means val should still be uninitialized.
|
;; Assigning to proc means val should still be uninitialized.
|
||||||
(test (E (make-AssignImmediateStatement 'proc (make-Const "Danny")))
|
(test (E-single (make-AssignImmediateStatement 'proc (make-Const "Danny")))
|
||||||
"undefined")
|
"undefined")
|
||||||
;; But we should see the assignment if we inspect MACHINE.proc.
|
;; But we should see the assignment if we inspect MACHINE.proc.
|
||||||
(test (E (make-AssignImmediateStatement 'proc (make-Const "Danny"))
|
(test (E-single (make-AssignImmediateStatement 'proc (make-Const "Danny"))
|
||||||
"MACHINE.proc")
|
"MACHINE.proc")
|
||||||
"Danny")
|
"Danny")
|
||||||
|
|
||||||
|
|
||||||
|
(test (E-single (make-PushEnvironment 1)
|
||||||
|
"MACHINE.env.length")
|
||||||
|
"1")
|
||||||
|
(test (E-single (make-PushEnvironment 20)
|
||||||
|
"MACHINE.env.length")
|
||||||
|
"20")
|
||||||
|
|
||||||
|
(test (E-many (list (make-PushEnvironment 1))
|
||||||
|
"MACHINE.env.length")
|
||||||
|
"1")
|
Loading…
Reference in New Issue
Block a user