From 0b820eeb328a94d8dc1ec46bf0546dedc888b599 Mon Sep 17 00:00:00 2001 From: zaoqi Date: Sun, 3 Dec 2017 15:47:37 +0800 Subject: [PATCH] js --- cs-test.rkt | 19 ++++++++++++++----- js.cscm | 23 +++++++++++++---------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/cs-test.rkt b/cs-test.rkt index 4090a9f..5c3bd50 100644 --- a/cs-test.rkt +++ b/cs-test.rkt @@ -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))] @@ -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 @@ -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);|] ) \ No newline at end of file diff --git a/js.cscm b/js.cscm index cff3918..2708b59 100644 --- a/js.cscm +++ b/js.cscm @@ -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")] @@ -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) @@ -79,7 +82,7 @@ (EVAL (second xs) (λ (key) (k (++ o"["key"]"))))))] - [(eq? f '@) + [(eq? f '/) (EVAL (first xs) (λ (o) (k (++ o"."(id (second xs))))))]