Permalink
Browse files

add optional arguments, simplify macro generation

  • Loading branch information...
1 parent 40a6096 commit 1cf529cab8a0266e3a0b7f8a2250c7c92d1205fb @jlongster committed May 6, 2012
Showing with 1,009 additions and 999 deletions.
  1. +88 −88 ast.js
  2. +177 −159 backends/js.js
  3. +34 −18 backends/js.ol
  4. +400 −412 compiler.js
  5. +2 −12 compiler.ol
  6. +0 −2 prog.ol
  7. +125 −125 reader.js
  8. +85 −85 runtime.js
  9. +98 −98 test.js
View
176 ast.js

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -158,31 +158,47 @@
((ast.list? args)
(define comma (inline-writer ","))
(define capture-name #f)
+ (define opt-args #f)
+ (define opt-args-idx #f)
- (define (write-args args)
+ (define (write-args args i)
(if (not (null? args))
(let ((arg (ast.node-data (car args))))
- (if (== arg '.)
- (set! capture-name (cadr args))
- (begin
- (comma)
- (write-term (car args) #t)
- (write-args (cdr args)))))))
+ (cond
+ ((== arg '.)
+ (set! capture-name (cadr args)))
+ ((== arg '&)
+ (set! opt-args (cdr args))
+ (set! opt-args-idx i))
+ (else
+ (comma)
+ (write-term (car args) #t)
+ (write-args (cdr args) (+ i 1)))))))
(write "(function(")
- (write-args (ast.node-data args))
+ (write-args (ast.node-data args) 0)
(write "){" #t)
- (if capture-name
- (begin
- (write "var ")
- (write-term capture-name #t)
- (write " = ")
- (write-term (ast.make-atom 'vector->list capture-name) #t)
- (write "(Array.prototype.slice.call(arguments, ")
- ;; only slice args from where the dot started
- (write (- (length (ast.node-data args)) 2))
- (write "));" #t))))
+ (cond
+ (capture-name
+ (write "var ")
+ (write-term capture-name #t)
+ (write " = ")
+ (write-term (ast.make-atom 'vector->list capture-name) #t)
+ (write "(Array.prototype.slice.call(arguments, ")
+ ;; only slice args from where the dot started
+
+ (write (- (length (ast.node-data args)) 2))
+ (write "));" #t))
+ (opt-args
+ (fold (lambda (arg i)
+ (write "var ")
+ (write-term arg #t)
+ (write (str " = arguments[" i "] || false;") #t)
+ (+ i 1))
+ opt-args-idx
+ opt-args))))
+
((symbol? (ast.node-data args))
(write "(function() {" #t)
(write "var ")
Oops, something went wrong.

0 comments on commit 1cf529c

Please sign in to comment.