[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 11/99: Compile rest args
From: |
Christopher Allan Webber |
Subject: |
[Guile-commits] 11/99: Compile rest args |
Date: |
Sun, 10 Oct 2021 21:50:43 -0400 (EDT) |
cwebber pushed a commit to branch compile-to-js-merge
in repository guile.
commit 86fabef4ca8c73ea6aa293d3685b52df0f442eb2
Author: Ian Price <ianprice90@googlemail.com>
AuthorDate: Sun Jun 7 21:47:08 2015 +0100
Compile rest args
---
module/language/cps/compile-js.scm | 9 ++++-----
module/language/js-il.scm | 8 +++++---
module/language/js-il/compile-javascript.scm | 23 +++++++++++++++++++++--
module/language/js-il/direct.scm | 4 ++--
4 files changed, 32 insertions(+), 12 deletions(-)
diff --git a/module/language/cps/compile-js.scm
b/module/language/cps/compile-js.scm
index 250b7a1..826f646 100644
--- a/module/language/cps/compile-js.scm
+++ b/module/language/cps/compile-js.scm
@@ -40,20 +40,19 @@
;; add function argument prelude
(unless (null? opt)
(not-supported "optional arguments are not supported" clause))
- (when rest
- (not-supported "rest arguments are not supported" clause))
(unless (or (null? kw) allow-other-keys?)
(not-supported "keyword arguments are not supported" clause))
(when alternate
(not-supported "alternate continuations are not supported" clause))
- (make-function self
- (cons tail req)
+ (make-function (make-params self (cons tail req) rest)
(match body
(($ $cont k ($ $kargs () () exp))
(compile-term exp))
(($ $cont k _)
(make-local (list (compile-cont body))
- (make-continue k (map make-id req)))))))))
+ (make-continue
+ k
+ (map make-id (append req (if rest (list
rest) '())))))))))))
(define (not-supported msg clause)
(error 'not-supported msg clause))
diff --git a/module/language/js-il.scm b/module/language/js-il.scm
index 921bac6..4c6c346 100644
--- a/module/language/js-il.scm
+++ b/module/language/js-il.scm
@@ -4,6 +4,7 @@
#:use-module (ice-9 match)
#:export (make-program program
make-function function
+ make-params params
make-continuation continuation
make-local local
make-var var
@@ -48,7 +49,8 @@
(format port "#<js-il ~S>" (unparse-js exp)))
(define-js-type program entry body)
-(define-js-type function name params body)
+(define-js-type function params body)
+(define-js-type params self req rest)
(define-js-type continuation params body)
(define-js-type local bindings body) ; local scope
(define-js-type var id exp)
@@ -67,8 +69,8 @@
`(program ,(unparse-js entry) . ,(map unparse-js body)))
(($ continuation params body)
`(continuation ,params ,(unparse-js body)))
- (($ function name params body)
- `(function ,name ,params ,(unparse-js body)))
+ (($ function ($ params self req opt) body)
+ `(function ,(append (list self) req (if opt (list opt) '())) ,(unparse-js
body)))
(($ local bindings body)
`(local ,(map unparse-js bindings) ,(unparse-js body)))
(($ var id exp)
diff --git a/module/language/js-il/compile-javascript.scm
b/module/language/js-il/compile-javascript.scm
index 373d5a9..676d448 100644
--- a/module/language/js-il/compile-javascript.scm
+++ b/module/language/js-il/compile-javascript.scm
@@ -17,6 +17,20 @@
(define (rename name)
(format #f "kont_~a" name))
+(define (bind-rest-args rest num-drop)
+ (define (ref i l)
+ (if (null? l)
+ i
+ (ref (make-refine i (make-const (car l)))
+ (cdr l))))
+ (define this (rename rest))
+ (make-var this
+ (make-call (ref *scheme* (list "list" "apply"))
+ (list
+ (ref *scheme* (list "list"))
+ (make-call (ref (make-id "Array") (list "prototype"
"slice" "call"))
+ (list (make-id "arguments") (make-const
num-drop)))))))
+
(define (compile-exp exp)
;; TODO: handle ids for js
(match exp
@@ -34,8 +48,13 @@
(($ il:continuation params body)
(make-function (map rename params) (list (compile-exp body))))
- (($ il:function name params body)
- (make-function (map rename (cons name params)) (list (compile-exp body))))
+ (($ il:function ($ il:params self req #f) body)
+ (make-function (map rename (cons self req)) (list (compile-exp body))))
+
+ (($ il:function ($ il:params self req rest) body)
+ (make-function (map rename (cons self req))
+ (list (bind-rest-args rest (length (cons self req)))
+ (compile-exp body))))
(($ il:local bindings body)
(make-block (append (map compile-exp bindings) (list (compile-exp
body)))))
diff --git a/module/language/js-il/direct.scm b/module/language/js-il/direct.scm
index 6e97e3e..e431649 100644
--- a/module/language/js-il/direct.scm
+++ b/module/language/js-il/direct.scm
@@ -12,8 +12,8 @@
(($ continuation params body)
(make-continuation params (remove-immediate-calls body)))
- (($ function name params body)
- (make-function name params (remove-immediate-calls body)))
+ (($ function params body)
+ (make-function params (remove-immediate-calls body)))
(($ local
(($ var id ($ continuation () body)))
- [Guile-commits] 02/99: Replace values object with values passed as continuation arguments, (continued)
- [Guile-commits] 02/99: Replace values object with values passed as continuation arguments, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 01/99: Temp commit, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 09/99: get rid of unused match case, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 03/99: Remove jscall type, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 04/99: fix makefile, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 05/99: separate js-il functions into actual functions and those for continuations, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 06/99: Get rid of comments and dead branches, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 07/99: Simple inlining of immediate calls, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 08/99: conditional->branch, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 10/99: fix makefile, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 11/99: Compile rest args,
Christopher Allan Webber <=
- [Guile-commits] 16/99: Remove superfluous space, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 20/99: Implement keyword argument parsing, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 12/99: Compile string constants, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 13/99: Mangle js identifiers, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 15/99: Handle case-lambda via a jump table, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 19/99: Simplify output Javascript, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 21/99: Primitives should return Scheme Booleans, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 23/99: Compile cps $prompt form to javascript, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 26/99: abort-to-prompt takes multiple arguments, Christopher Allan Webber, 2021/10/10
- [Guile-commits] 34/99: Change function type representation, Christopher Allan Webber, 2021/10/10