summaryrefslogtreecommitdiff
path: root/module/language/javascript.scm
blob: 2e1ca7dcde9eae961bba244f906c18d21efd3a66 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
;;; JavaScript Language

;; Copyright (C) 2015, 2017 Free Software Foundation, Inc.

;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

;;; Code:

;; Only has enough of the ecmascript language for compilation from cps
(define-module (language javascript)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-9 gnu)
  #:export (
            make-assign assign
            make-const const
            make-function function
            make-return return
            make-call call
            make-block block
            make-new new
            make-id id
            make-refine refine
            make-branch branch
            make-var var
            make-binop binop
            make-ternary ternary
            make-prefix prefix

            print-statement))

;; Copied from (language cps)
;; Should put in a srfi 99 module
(define-syntax define-record-type*
  (lambda (x)
    (define (id-append ctx . syms)
      (datum->syntax ctx (apply symbol-append (map syntax->datum syms))))
    (syntax-case x ()
      ((_ name field ...)
       (and (identifier? #'name) (and-map identifier? #'(field ...)))
       (with-syntax ((cons (id-append #'name #'make- #'name))
                     (pred (id-append #'name #'name #'?))
                     ((getter ...) (map (lambda (f)
                                          (id-append f #'name #'- f))
                                        #'(field ...))))
         #'(define-record-type name
             (cons field ...)
             pred
             (field getter)
             ...))))))

;; TODO: add type predicates to fields so I can only construct valid
;; objects
(define-syntax-rule (define-js-type name field ...)
  (begin
    (define-record-type* name field ...)
    (set-record-type-printer! name print-js)))

(define (print-js exp port)
  (format port "#<js ~S>" (unparse-js exp)))

(define-js-type assign id exp)
(define-js-type const c)
(define-js-type function args body)
(define-js-type return exp)
(define-js-type call function args)
(define-js-type block statements)
(define-js-type new expr)
(define-js-type id name)
(define-js-type refine id field)
(define-js-type branch test then else)
(define-js-type var id exp)
(define-js-type binop op arg1 arg2)
(define-js-type ternary test then else)
(define-js-type prefix op expr)

(define (unparse-js exp)
  (match exp
    (($ assign id exp)
     `(assign ,id ,(unparse-js exp)))
    (($ const c)
     `(const ,c))
    (($ function args body)
     `(function ,args ,@(map unparse-js body)))
    (($ return exp)
     `(return ,(unparse-js exp)))
    (($ call function args)
     `(call ,(unparse-js function) ,@(map unparse-js args)))
    (($ block statements)
     `(block ,@(map unparse-js statements)))
    (($ new expr)
     `(new ,(unparse-js expr)))
    (($ id name)
     `(id ,name))
    (($ refine id field)
     `(refine ,(unparse-js id) ,(unparse-js field)))
    (($ branch test then else)
     `(if ,(unparse-js test)
          (block ,@(map unparse-js then))
          (block ,@(map unparse-js else))))
    (($ var id exp)
     `(var ,id ,(unparse-js exp)))
    (($ binop op arg1 arg2)
     `(binop ,op ,(unparse-js arg1) ,(unparse-js arg2)))
    (($ ternary test then else)
     `(ternary ,(unparse-js test) ,(unparse-js then) ,(unparse-js else)))
    (($ prefix op expr)
     `(prefix ,op ,(unparse-js expr)))
    ))

(define (print-exp exp port)
  (match exp

    (($ assign id exp)
     (print-id id port)
     (format port " = ")
     (display "(" port)
     (print-exp exp port)
     (display ")" port))

    (($ const c)
     (print-const c port))

    (($ id name)
     (print-id name port))

    (($ call (and ($ function _ _) fun) args)
     (format port "(")
     (print-exp fun port)
     (format port ")(")
     (print-separated args print-exp "," port)
     (format port ")"))

    (($ call fun args)
     (print-exp fun port)
     (format port "(")
     (print-separated args print-exp "," port)
     (format port ")"))


    (($ refine expr field)
     (print-exp expr port)
     (format port "[")
     (print-exp field port)
     (format port "]"))

    (($ function params body)
     (format port "function (")
     (print-separated params print-id "," port)
     (format port ")")
     (print-block body port))

    (($ block stmts)
     (print-block stmts port))

    (($ new expr)
     (format port "new ")
     (print-exp expr port))

    (($ binop op arg1 arg2)
     (display "(" port)
     (print-exp arg1 port)
     (display ")" port)
     (print-binop op port)
     (display "(" port)
     (print-exp arg2 port)
     (display ")" port))

    (($ ternary test then else)
     (display "(" port)
     (print-exp test port)
     (display ") ? (" port)
     (print-exp then port)
     (display ") : (" port)
     (print-exp else port)
     (display ")" port))

    (($ prefix op exp)
     (print-prefix op port)
     (display "(" port)
     (print-exp exp port)
     (display ")" port))
    ))

(define (print-binop op port)
  (case op
    ((or) (display "||" port))
    ((and) (display "&&" port))
    ((=) (display "==" port))
    ((begin) (display "," port))
    ((+ - < <= > >= === instanceof) (format port "~a" op))
    (else
     (throw 'unprintable-binop op))))

(define (print-prefix op port)
  (case op
    ((not) (display "!" port))
    ((typeof + -)
     (format port "~a" op))
    (else
     (throw 'unprintable-prefix op))))

(define (print-statement stmt port)
  (match stmt
    (($ var id exp)
     (format port "var ")
     (print-id id port)
     (format port " = ")
     (print-exp exp port)
     (format port ";"))

    (($ branch test then else)
     (format port "if (")
     (print-exp test port)
     (format port ") {")
     (print-block then port)
     (format port "} else {")
     (print-block else port)
     (format port "}"))

    (($ return expr)
     (format port "return ")
     (print-exp expr port)
     (format port ";"))

    (expr
     (print-exp expr port)
     (format port ";"))))

(define (print-id id port)
  (display id port))

(define (print-block stmts port)
  (format port "{")
  (print-statements stmts port)
  (format port "}"))

(define (print-statements stmts port)
  (for-each (lambda (stmt)
              (print-statement stmt port))
            stmts))

(define (print-const c port)
  (cond ((string? c)
         ;; FIXME:
         ;; Scheme strings and JS Strings are different, and not just in
         ;; terms of mutability
         (write c port))
        ((number? c)
         (write c port))
        (else
         (throw 'unprintable-const c))))

(define (print-separated args printer separator port)
  (unless (null? args)
    (let ((first (car args))
          (rest  (cdr args)))
      (printer first port)
      (for-each (lambda (x)
                  (display separator port)
                  (printer x port))
                rest))))