Skip to content
This repository has been archived by the owner on Jan 1, 2018. It is now read-only.

Commit

Permalink
js
Browse files Browse the repository at this point in the history
  • Loading branch information
zaoqi committed Dec 3, 2017
1 parent 34bac48 commit 0b820ee
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
19 changes: 14 additions & 5 deletions cs-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
(define-syntax-rule (test [c r] ...)
(begin
(check-equal? (run-sexp (quote c)) (quote r)) ...))
(define-syntax-rule (load/test f [c r] ...)
(check-equal?
(run-sexps (append (include/quote/list f)
(list (quote (list c ...)))))
(list (quote r) ...)))
(test
[`(list ,(+ 1 2) 4) (list 3 4)]
[(let ((name 'a)) `(list ,name ',name)) (list a (quote a))]
Expand Down Expand Up @@ -61,11 +66,6 @@
x
(cell-content a-cell)
(cell? a-cell)))) (0 1 #t)])
(define-syntax-rule (load/test f [c r] ...)
(check-equal?
(run-sexps (append (include/quote/list f)
(list (quote (list c ...)))))
(list (quote r) ...)))
(load/test
"macroexpand.cscm"
[(macroexpand '(begin
Expand Down Expand Up @@ -109,4 +109,13 @@
(define-record-type promise (%delay x) promise? (x %force))
(define-record-type ERROR (ERROR x) ERROR? (x ERROR-x))
(+ 0 0))]
)

(load/test
"js.cscm"
[(js '(begin
(define (displayln x)
(: console log x))
(define writeln displayln)
(displayln 0))) |var displayln=(function(x){return console.log(x);});var writeln=displayln;displayln(0);|]
)
23 changes: 13 additions & 10 deletions js.cscm
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
(define (EVAL x f)
(cond
[(pair? x) (APPLY (car x) (cdr x) f)]
[(string? x) (f (->id x))]
[(string? x) (f (id x))]
[(number? x) (f (number->string x))]
[(eq? x #t) (f "true")]
[(eq? x #f) (f "false")]
Expand All @@ -45,18 +45,21 @@
(! xs
(λ (xs)
(k (cons (cons (id (first x)) v) xs)))))))))
(define (DEFINE xs k)
(cond
[(null? (cdr xs)) (++ "var "(id (car xs))N (k undefined))]
[(pair? (car xs))
(let ([a (car xs)])
(DEFINE (list (car a) (cons 'λ (cons (cdr a) (cdr xs)))) k))]
[else (EVAL (second xs)
(λ (x)
(++ "var "(id (first xs))"="x N
(k undefined))))]))
(define (APPLY f xs k)
(cond
[(string? f)
(cond
[(eq? f 'define)
(if (null? (cdr xs))
(++ "var "(id (first xs))N
(k undefined))
(EVAL (second xs)
(λ (x)
(++ "var "(id (first xs))N
(k undefined)))))]
[(eq? f 'define) (DEFINE xs k)]
[(eq? f 'set!)
(EVAL (first xs)
(λ (x)
Expand All @@ -79,7 +82,7 @@
(EVAL (second xs)
(λ (key)
(k (++ o"["key"]"))))))]
[(eq? f '@)
[(eq? f '/)
(EVAL (first xs)
(λ (o)
(k (++ o"."(id (second xs))))))]
Expand Down

0 comments on commit 0b820ee

Please sign in to comment.