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'.
master
Mike Gerwitz 2018-01-05 00:01:22 -05:00
parent 49142b6630
commit 01990614cc
Signed by: mikegerwitz
GPG Key ID: 8C917B7F5DC51BA2
1 changed files with 18 additions and 27 deletions

View File

@ -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)"))