From 01990614cc6d3b3c86db9556447710b94b1b34fe Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 5 Jan 2018 00:01:22 -0500 Subject: [PATCH] rebirth: Use cdr in libprebirth replacement proc definitions This removes the need to use the ES `arguments' reference. Not only is this what we want to do, but it's _necessary_---the next commit will introduce environments, and wrapping procedures in lambdas breaks the `arguments' reference in each of these cases. Note that fold and map can now be written in Rebirth Lisp, but I'm just leaving them alone for now. * build-aux/bootstrap/rebirth.scm (append, string-append, +, -, map): Use define cdr in place of ES `arguments'. --- build-aux/bootstrap/rebirth.scm | 45 +++++++++++++-------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/build-aux/bootstrap/rebirth.scm b/build-aux/bootstrap/rebirth.scm index 5243348..d8f1c15 100644 --- a/build-aux/bootstrap/rebirth.scm +++ b/build-aux/bootstrap/rebirth.scm @@ -240,8 +240,7 @@ (define (es:arg->arr args) (string->es "Array.prototype.slice.call($$args)")) - (define (list) - (es:arg->arr (string->es "arguments"))) + (define (list . xs) xs) ;; warning: only compares two values (define (= x y) @@ -284,12 +283,12 @@ (es:-assert-pair pair) (string->es "$$pair.slice(1)")) - (define (append) + (define (append . args) (fold (lambda (x xs) (es:-assert-list x) (string->es "$$xs.concat($$x)")) (list) - (es:arg->arr (string->es "arguments")))) + args)) ;; warning: these two are wholly inadequate (define (list? xs) @@ -307,26 +306,23 @@ (string->es "typeof $$s1 === 'string' && $$s1 === $$s2")) (define (string-ref s i) (string->es "$$s[$$i] || $$error(`value out of range: ${$$i}`)")) - (define (string-append) - (let ((args (es:arg->arr (string->es "arguments")))) - (string->es "$$args.join('')"))) + (define (string-append . xs) + (string->es "$$xs.join('')")) (define (eq? x y) (string->es "$$x === $$y")) ;; R7RS math - (define (+) - (let ((args (es:arg->arr (string->es "arguments")))) - (fold (lambda (y x) - (string->es "$$x + $$y")) - 0 - args))) - (define (-) - (let ((args (es:arg->arr (string->es "arguments")))) - (fold (lambda (y x) - (string->es "$$x - $$y")) - (car args) - (cdr args)))) + (define (+ . xs) + (fold (lambda (y x) + (string->es "$$x + $$y")) + 0 + xs)) + (define (- . xs) + (fold (lambda (y x) + (string->es "$$x - $$y")) + (car xs) + (cdr xs))) (define (zero? x) (eq? x 0)) @@ -336,14 +332,9 @@ (string->es "$$xs.reduce((prev, x) => $$f(x, prev), $$init)")) ;; warning: map here uses the length of the first list, not the shortest - ;; (we implement this in ES for now so that we don't have to augment - ;; Prebirth Lisp to support the "rest" procedure definition syntax) - (define (map f) - (string->es "__a = arguments") ; because let introduces a function - (let* ((args (es:arg->arr (string->es "__a"))) - (xs (cdr args))) - (string->es - "$$xs[0].map((_, i) => $$f.apply(null, $$xs.map(x => x[i])))"))) + (define (map f . xs) + (string->es + "$$xs[0].map((_, i) => $$f.apply(null, $$xs.map(x => x[i])))")) (define (es:regexp s opts) (string->es "new RegExp($$s, $$opts)"))