-
Notifications
You must be signed in to change notification settings - Fork 0
/
script-lib.scm
110 lines (97 loc) · 3.24 KB
/
script-lib.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(define (js-statement . x) (list x ";"))
(define (js-statement* . x) x)
(define (js-if condition . body) (list "if(" condition "){" body "}"))
(define (js-else-if condition . body) (list "else" " " (apply js-if condition body)))
(define (js-else . body) (list "else{" body "}"))
(define (js-with obj . body) (list "with(" obj "){" body "}"))
(define (js-?: x y z) (list "((" x ")?(" y "):(" z "))"))
(define-syntax js-let
(syntax-rules ()
((_ (vars ...) body ...)
(js-let-helper () (vars ...) body ...)
)))
(define-syntax js-let-helper
(syntax-rules ()
((_ ((var init) ...) () body ...)
(let ((var (string->symbol (string-append "$" (symbol->string (gensym))))) ...)
(list (js-multi-defver (var init) ...)
body ...)))
((_ (part ...) ((var init) rest ...) body ...)
(js-let-helper (part ... (var init)) (rest ...) body ...))
((_ (part ...) ((var) rest ...) body ...)
(js-let-helper (part ... (var ())) (rest ...) body ...))
))
(define-syntax js-defvar-helper
(syntax-rules ()
((_ var ())
var)
((_ var init ...)
(list var "=" init ...))
))
(define-syntax js-multi-defver
(syntax-rules ()
((_ (var1 init1))
(js-statement "var" " " (js-defvar-helper var1 init1)))
((_ (var1 init1) (rest-var rest-init) ...)
(js-statement "var" " "
(js-defvar-helper var1 init1)
(list "," (js-defvar-helper rest-var rest-init)) ...))
))
(define-syntax js-join-with-comma
(syntax-rules ()
((_) ())
((_ x) x)
((_ x rest ...) (list x (list "," rest) ...))
))
(define-syntax js-function
(syntax-rules ()
((_ (var ...) body ...)
(let ((var (string-append "$" (symbol->string (gensym)))) ...)
(list "function" "(" (js-join-with-comma var ...) ")" "{"
body ... "}")))
))
(define-syntax js-defun
(syntax-rules ()
((_ name (var ...) body ...)
(let ((var (string-append "$" (symbol->string (gensym)))) ...)
(list "function" (if (null? name) () (list " " name)) "(" (js-join-with-comma var ...) ")" "{"
body ... "}")))
))
(define-syntax js-for/defvar
(syntax-rules ()
((_ ((var init) ...) condition succ body ...)
(let ((var (string-append "$" (symbol->string (gensym)))) ...)
(list "for" "("
(js-multi-defver (var init) ...)
condition
succ ")" "{"
body ... "}")
)
)
))
(define-syntax js-for/iter
(syntax-rules (->)
((_ (x -> s) body ...)
(let1 x (string-append "$" (symbol->string (gensym)))
(list "for" "(" "var" " " x " " "in" " " s ")" "{" body ... "}")
))
))
(define (js-call func . args)
(apply string-append `(,(x->string func) "(" ,@(map x->string args) ")")))
#;(define-syntax js-.
(syntax-rules ()
((_ first rest ...)
(fold (lambda (a b) (string-append b "." a))
(symbol->string 'first)
(list (symbol->string 'rest) ...)))))
(define (js-. first . rest)
(fold (lambda (a b) (string-append b "." a))
(x->string first)
(map x->string rest)))
(define-syntax define-tag
(syntax-rules ()
((_ (name args ...) body ...)
(js-statement
"D.prototype." name "="
(js-function (args ...) body ...)
))))