mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-17 17:20:29 +02:00
separate js-il functions into actual functions and those for continuations
This commit is contained in:
parent
9e498f2430
commit
54ce470cf8
3 changed files with 20 additions and 131 deletions
|
@ -51,7 +51,7 @@
|
||||||
(not-supported "keyword arguments are not supported" clause))
|
(not-supported "keyword arguments are not supported" clause))
|
||||||
(when alternate
|
(when alternate
|
||||||
(not-supported "alternate continuations are not supported" clause))
|
(not-supported "alternate continuations are not supported" clause))
|
||||||
(make-function self ;; didn't think this js pattern would come in handy
|
(make-function self
|
||||||
(cons tail req)
|
(cons tail req)
|
||||||
(match body
|
(match body
|
||||||
(($ $cont k ($ $kargs () () exp))
|
(($ $cont k ($ $kargs () () exp))
|
||||||
|
@ -76,11 +76,14 @@
|
||||||
(match cont
|
(match cont
|
||||||
(($ $cont k ($ $kargs names syms body))
|
(($ $cont k ($ $kargs names syms body))
|
||||||
;; use the name part?
|
;; use the name part?
|
||||||
(make-var k (make-function syms (compile-term body))))
|
(make-var k (make-continuation syms (compile-term body))))
|
||||||
(($ $cont k ($ $kreceive ($ $arity (arg) _ (? symbol? rest) _ _) k2))
|
(($ $cont k ($ $kreceive ($ $arity req _ (? symbol? rest) _ _) k2))
|
||||||
(make-var k (make-function (list arg rest) (make-continue k2 (list (make-id arg) (make-id rest))))))
|
(make-var k
|
||||||
(($ $cont k ($ $kreceive ($ $arity (arg) _ #f _ _) k2))
|
(make-continuation (append req (list rest))
|
||||||
(make-var k (make-function (list arg) (make-continue k2 (list (make-id arg))))))
|
(make-continue k2
|
||||||
|
(append (map make-id req) (list (make-id rest)))))))
|
||||||
|
(($ $cont k ($ $kreceive ($ $arity req _ #f _ _) k2))
|
||||||
|
(make-var k (make-continuation req (make-continue k2 (map make-id req)))))
|
||||||
(_
|
(_
|
||||||
`(cont:todo: ,cont))
|
`(cont:todo: ,cont))
|
||||||
))
|
))
|
||||||
|
@ -108,6 +111,11 @@
|
||||||
(make-primcall name args))
|
(make-primcall name args))
|
||||||
(($ $closure label nfree)
|
(($ $closure label nfree)
|
||||||
(make-closure label nfree))
|
(make-closure label nfree))
|
||||||
|
(($ $values (val))
|
||||||
|
;; FIXME:
|
||||||
|
;; may happen if a test branch of a conditional compiles to values
|
||||||
|
;; placeholder till I learn if multiple values could be returned.
|
||||||
|
(make-id val))
|
||||||
(_
|
(_
|
||||||
`(exp:todo: ,exp))))
|
`(exp:todo: ,exp))))
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,8 @@
|
||||||
#:use-module (srfi srfi-9 gnu)
|
#:use-module (srfi srfi-9 gnu)
|
||||||
#:use-module (ice-9 match)
|
#:use-module (ice-9 match)
|
||||||
#:export (make-program program
|
#:export (make-program program
|
||||||
(make-function* . make-function) function
|
make-function function
|
||||||
|
make-continuation continuation
|
||||||
make-local local
|
make-local local
|
||||||
make-var var
|
make-var var
|
||||||
make-continue continue ; differ from conts
|
make-continue continue ; differ from conts
|
||||||
|
@ -49,14 +50,7 @@
|
||||||
|
|
||||||
(define-js-type program entry body)
|
(define-js-type program entry body)
|
||||||
(define-js-type function name params body)
|
(define-js-type function name params body)
|
||||||
|
(define-js-type continuation params body)
|
||||||
(define make-function*
|
|
||||||
(case-lambda
|
|
||||||
((name params body)
|
|
||||||
(make-function name params body))
|
|
||||||
((params body)
|
|
||||||
(make-function #f params body))))
|
|
||||||
|
|
||||||
(define-js-type local bindings body) ; local scope
|
(define-js-type local bindings body) ; local scope
|
||||||
(define-js-type var id exp)
|
(define-js-type var id exp)
|
||||||
(define-js-type continue cont args)
|
(define-js-type continue cont args)
|
||||||
|
@ -72,6 +66,8 @@
|
||||||
(match exp
|
(match exp
|
||||||
(($ program entry body)
|
(($ program entry body)
|
||||||
`(program ,(unparse-js entry) . ,(map unparse-js body)))
|
`(program ,(unparse-js entry) . ,(map unparse-js body)))
|
||||||
|
(($ continuation params body)
|
||||||
|
`(continuation ,params ,(unparse-js body)))
|
||||||
(($ function name params body)
|
(($ function name params body)
|
||||||
`(function ,name ,params ,(unparse-js body)))
|
`(function ,name ,params ,(unparse-js body)))
|
||||||
(($ local bindings body)
|
(($ local bindings body)
|
||||||
|
@ -99,117 +95,3 @@
|
||||||
;(error "unexpected js" exp)
|
;(error "unexpected js" exp)
|
||||||
(pk 'unexpected exp)
|
(pk 'unexpected exp)
|
||||||
exp)))
|
exp)))
|
||||||
#|
|
|
||||||
(define (print-js exp port)
|
|
||||||
;; could be much nicer with foof's fmt
|
|
||||||
(match exp
|
|
||||||
(($ program (and entry ($ var name _)) body)
|
|
||||||
;; TODO: I should probably put call to entry in js-il
|
|
||||||
(format port "(function(){\n")
|
|
||||||
(print-js entry port) (display ";\n" port)
|
|
||||||
(print-terminated body print-js ";\n" port)
|
|
||||||
;; call to entry point
|
|
||||||
(format port "return ~a(scheme.initial_cont);" (lookup-cont name))
|
|
||||||
(format port "})();\n"))
|
|
||||||
(($ function #f params body)
|
|
||||||
(format port "function(")
|
|
||||||
(print-separated params print-var "," port)
|
|
||||||
(format port "){\n")
|
|
||||||
(print-js body port)(display ";" port)
|
|
||||||
(format port "}"))
|
|
||||||
;; TODO: clean this code up
|
|
||||||
(($ function name params body)
|
|
||||||
(format port "function (~a," (lookup-cont name))
|
|
||||||
(print-separated params print-var "," port)
|
|
||||||
(format port "){\n")
|
|
||||||
(print-js body port)(display ";" port)
|
|
||||||
(format port "}"))
|
|
||||||
(($ local bindings body)
|
|
||||||
(display "{" port)
|
|
||||||
(print-terminated bindings print-js ";\n" port)
|
|
||||||
(print-js body port)
|
|
||||||
(display ";\n")
|
|
||||||
(display "}" port))
|
|
||||||
(($ var id exp)
|
|
||||||
(format port "var ~a = " (lookup-cont id))
|
|
||||||
(print-js exp port))
|
|
||||||
(($ continue k args)
|
|
||||||
(format port "return ~a(" (lookup-cont k))
|
|
||||||
(print-js exp port)
|
|
||||||
(display ")" port))
|
|
||||||
(($ branch test then else)
|
|
||||||
(display "if (scheme.is_true(" port)
|
|
||||||
(print-js test port)
|
|
||||||
(display ")) {\n" port)
|
|
||||||
(print-js then port)
|
|
||||||
(display ";} else {\n" port)
|
|
||||||
(print-js else port)
|
|
||||||
(display ";}" port))
|
|
||||||
;; values
|
|
||||||
(($ const c)
|
|
||||||
(print-const c port))
|
|
||||||
(($ primcall name args)
|
|
||||||
(format port "scheme.primitives[\"~s\"](" name)
|
|
||||||
(print-separated args print-var "," port)
|
|
||||||
(format port ")"))
|
|
||||||
(($ call name args)
|
|
||||||
;; TODO: need to also add closure env
|
|
||||||
(format port "return ~a.fun(~a," (lookup-cont name) (lookup-cont name))
|
|
||||||
(print-separated args print-var "," port)
|
|
||||||
(format port ")"))
|
|
||||||
(($ jscall name args)
|
|
||||||
(format port "return ~a(" (lookup-cont name))
|
|
||||||
(print-separated args print-var "," port)
|
|
||||||
(format port ")"))
|
|
||||||
(($ closure label nfree)
|
|
||||||
(format port "new scheme.Closure(~a,~a)" (lookup-cont label) nfree))
|
|
||||||
(($ values vals)
|
|
||||||
(display "new scheme.Values(" port)
|
|
||||||
(print-separated vals print-var "," port)
|
|
||||||
(display ")" port))
|
|
||||||
;; (($ return val)
|
|
||||||
;; (display "return " port)
|
|
||||||
;; (print-js val port))
|
|
||||||
(($ id name)
|
|
||||||
(print-var name port))
|
|
||||||
(_
|
|
||||||
(error "print: unexpected js" exp))))
|
|
||||||
|
|
||||||
(define (print-var var port)
|
|
||||||
(if (number? var)
|
|
||||||
(display (lookup-cont var) port)
|
|
||||||
(display var port)))
|
|
||||||
|
|
||||||
(define (lookup-cont k)
|
|
||||||
(format #f "kont_~s" k))
|
|
||||||
|
|
||||||
(define (print-separated args printer separator port)
|
|
||||||
(unless (null? args)
|
|
||||||
(let ((first (car args))
|
|
||||||
(rest (cdr args)))
|
|
||||||
(printer first port)
|
|
||||||
(for-each (lambda (x)
|
|
||||||
(display separator port)
|
|
||||||
(printer x port))
|
|
||||||
rest))))
|
|
||||||
|
|
||||||
(define (print-terminated args printer terminator port)
|
|
||||||
(for-each (lambda (x)
|
|
||||||
(printer x port)
|
|
||||||
(display terminator port))
|
|
||||||
args))
|
|
||||||
|
|
||||||
(define (print-const c port)
|
|
||||||
(cond ((number? c) (display c port))
|
|
||||||
((eqv? c #t) (display "scheme.TRUE" port))
|
|
||||||
((eqv? c #f) (display "scheme.FALSE" port))
|
|
||||||
((eqv? c '()) (display "scheme.EMPTY" port))
|
|
||||||
((unspecified? c) (display "scheme.UNSPECIFIED" port))
|
|
||||||
((symbol? c) (format port "new scheme.Symbol(\"~s\")" c))
|
|
||||||
((list? c)
|
|
||||||
(display "scheme.list(" port)
|
|
||||||
(print-separated c print-const "," port)
|
|
||||||
(display ")" port))
|
|
||||||
(else
|
|
||||||
(throw 'not-implemented))))
|
|
||||||
|#
|
|
||||||
|
|
|
@ -29,11 +29,10 @@
|
||||||
(list (compile-exp entry) entry-call)))
|
(list (compile-exp entry) entry-call)))
|
||||||
'())))
|
'())))
|
||||||
|
|
||||||
(($ il:function #f params body)
|
(($ il:continuation params body)
|
||||||
(make-function (map rename params) (list (compile-exp body))))
|
(make-function (map rename params) (list (compile-exp body))))
|
||||||
|
|
||||||
(($ il:function name params body)
|
(($ il:function name params body)
|
||||||
;; TODO: split il:function into closure (with self) and cont types
|
|
||||||
(make-function (map rename (cons name params)) (list (compile-exp body))))
|
(make-function (map rename (cons name params)) (list (compile-exp body))))
|
||||||
|
|
||||||
(($ il:local bindings body)
|
(($ il:local bindings body)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue