-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.scm
524 lines (453 loc) · 16.8 KB
/
eval.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
;;
;;
;; Ersatz evaluation.
;;
;; Based on the Ocaml Jingoo library, which is in turn based on the
;; Python Jinja2 library.
;;
;; Copyright 2012-2014 Ivan Raikov
;;
;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program 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
;; General Public License for more details.
;;
;; A full copy of the GPL license can be found at
;; <http://www.gnu.org/licenses/>.
;;
(define (eval-expr env ctx x)
(cases texpr x
(LiteralExpr (x) x)
(IdentExpr (name)
(get-value ctx name))
(NotOpExpr (x)
(test-not (eval-expr env ctx x)))
(NegateOpExpr (x)
(op-negate (eval-expr env ctx x)))
(PlusOpExpr (l r)
(op-plus (eval-expr env ctx l)
(eval-expr env ctx r)))
(MinusOpExpr (l r)
(op-minus (eval-expr env ctx l)
(eval-expr env ctx r)))
(TimesOpExpr (l r)
(op-times (eval-expr env ctx l)
(eval-expr env ctx r)))
(PowerOpExpr (l r)
(op-power (eval-expr env ctx l)
(eval-expr env ctx r)))
(DivOpExpr (l r)
(op-div (eval-expr env ctx l)
(eval-expr env ctx r)))
(ModOpExpr (l r)
(op-mod (eval-expr env ctx l)
(eval-expr env ctx r)))
(AndOpExpr (l r)
(op-and (eval-expr env ctx l)
(eval-expr env ctx r)))
(OrOpExpr (l r)
(op-or (eval-expr env ctx l)
(eval-expr env ctx r)))
(EqEqOpExpr (l r)
(eq-eq (eval-expr env ctx l)
(eval-expr env ctx r)))
(NotEqOpExpr (l r)
(not-eq (eval-expr env ctx l)
(eval-expr env ctx r)))
(LtOpExpr (l r)
(op-lt (eval-expr env ctx l)
(eval-expr env ctx r)))
(GtOpExpr (l r)
(op-gt (eval-expr env ctx l)
(eval-expr env ctx r)))
(LtEqOpExpr (l r)
(op-lteq (eval-expr env ctx l)
(eval-expr env ctx r)))
(GtEqOpExpr (l r)
(op-gteq (eval-expr env ctx l)
(eval-expr env ctx r)))
(InOpExpr (l r)
(op-in (eval-expr env ctx l)
(eval-expr env ctx r)))
(ListExpr (lst)
(Tlist (map (lambda (x) (eval-expr env ctx x)) lst)))
(SetExpr (lst)
(Tset (map (lambda (x) (eval-expr env ctx x)) lst)))
(DotExpr (objexpr propexpr)
(cases texpr objexpr
(IdentExpr (name)
(let ((prop (ident-expr->name propexpr)))
(tobj-lookup ctx name prop)))
(DotExpr (_ _)
(let ((v (eval-expr env ctx objexpr))
(prop (ident-expr->name propexpr)))
(tobjval-lookup v prop)))
(else (error 'eval-expr "invalid object dot expression" objexpr))))
(BracketExpr (objexpr propexpr)
(cases texpr objexpr
(IdentExpr (name)
(cases tvalue (eval-expr env ctx propexpr)
(Tstr (s) (tobj-lookup ctx name (string->symbol s)))
(else (error 'eval-expr "invalid property in bracket expression"
propexpr))
))
(DotExpr (_ _)
(let ((v (eval-expr env ctx objexpr)))
(cases tvalue (eval-expr env ctx propexpr)
(Tstr (s) (tobjval-lookup v (string->symbol s)))
(else (error 'eval-expr "invalid property in bracket expression"
propexpr))
)))
(else (error 'eval-expr "invalid object bracket expression" objexpr))))
(TestOpExpr (objexpr pred)
(cases texpr pred
(IdentExpr (predname)
(case predname
((defined)
(cases texpr objexpr
(IdentExpr (name)
(test-defined ctx name))
(DotExpr (objexpr propexpr)
(let ((name (ident-expr->name objexpr))
(prop (ident-expr->name propexpr)))
(test-obj-defined ctx name prop)))
(BracketExpr (objexpr propexpr)
(let ((name (ident-expr->name objexpr))
(prop (ident-or-string-expr->name propexpr)))
(test-obj-defined ctx name prop)))
(else (error 'eval-expr "invalid predicate object expression"
objexpr))
))
((undefined)
(cases texpr objexpr
(IdentExpr (name)
(test-undefined ctx name))
(DotExpr (objexpr propexpr)
(let ((name (ident-expr->name objexpr))
(prop (ident-expr->name propexpr)))
(test-obj-undefined ctx name prop)))
(else (error 'eval-expr "invalid predicate object expression"
objexpr))
))
((none)
(cases texpr objexpr
(IdentExpr (name)
(test-none ctx name))
(else (error 'eval-expr "invalid predicate object expression"
objexpr))
))
((escaped)
(cases texpr objexpr
(IdentExpr (name)
(test-escaped ctx))
(else (error 'eval-expr "invalid predicate object expression"
objexpr))
))
(else
(tfun-apply (eval-expr env ctx pred)
(list (eval-expr env ctx objexpr))))))
(else
(tfun-apply (eval-expr env ctx pred)
(list (eval-expr env ctx objexpr))))))
(ObjExpr (exprs)
(Tobj (map (lambda (id.val)
(cases texpr (car id.val)
(IdentExpr (name)
(cons name (eval-expr env ctx (cdr id.val))))
(LiteralExpr (strval expr)
(cons (string->symbol (unbox-string strval))
(eval-expr env ctx (cdr id.val))))
(else (error 'eval-expr "invalid object expression" id.val))))
exprs)))
(ApplyExpr (opexpr argexprs)
(let ((opname (ident-expr->name/safe opexpr)))
(if (and opname (eq? 'eval opname))
(let* ((ctx (template-context-with-buffer
ctx (open-output-string)))
(stmts (statements-from-string env (->string (eval-expr env ctx (car argexprs)))))
(ctx (fold (lambda (s ctx) (eval-statement env ctx s)) ctx stmts)))
(Tstr (get-output-string (tmpl-ctx-buffer ctx))))
(let ((name (apply-name-of opexpr))
(nargs (nargs-of env ctx argexprs))
(kwargs (kwargs-of env ctx argexprs))
(callable (eval-expr env ctx opexpr)))
(cases tvalue callable
(Tfun (fn)
(if (null? nargs)
(Tfun (lambda _ (fn argexprs kwargs)))
(tfun-apply callable nargs name: name kwargs: kwargs)))
(else
(let ((mac (get-macro ctx name)))
(if mac
(begin
(eval-macro1 env ctx name nargs kwargs mac)
(Tnull))
(Tnull))))
))
)))
(else (error 'eval-expr "invalid expression" x))
))
(define (apply-name-of fnexpr)
(cases texpr fnexpr
(IdentExpr (name) name)
(DotExpr (objexpr propexpr)
(let ((name (ident-expr->name objexpr))
(prop (ident-expr->name propexpr)))
(string->symbol (sprintf "~A.~A" name prop))))
(ApplyExpr (expr args)
(apply-name-of expr))
(else (string->symbol "<lambda>"))
))
(define (ident-names-of lst)
(map ident-expr->name
(filter (lambda (x)
(cases texpr x
(IdentExpr (x) #t)
(else #f)))
lst)))
(define (alias-names-of lst)
(map (lambda (x)
(cases texpr x
(IdentExpr (name) (cons name name))
(AliasExpr (e1 e2)
(let ((n1 (ident-expr->name e1))
(n2 (ident-expr->name e2)))
(cons n1 n2)))
(else (error 'alias-names-of "invalid argument" x))))
lst))
(define (nargs-of env ctx args)
(map (lambda (x) (eval-expr env ctx x))
(filter (lambda (x) (cases texpr x (KeywordExpr (x y) #f) (else #t)))
args)))
(define (kwargs-of env ctx args)
(map (lambda (x)
(cases texpr x
(KeywordExpr (x y)
(let ((name (ident-expr->name x)))
(cons name (eval-expr env ctx y))))))
(filter (lambda (x) (cases texpr x (KeywordExpr (x y) #t) (else #f)))
args)))
(define (eval-macro1 env ctx name args kwargs mac)
(let ((caller (get-macro ctx 'caller))
(f (lambda (ctx stmts)
(fold (lambda (s ctx)
(eval-statement env ctx s))
ctx stmts))))
(eval-macro env ctx name args kwargs mac f caller: (and caller #t))))
(define (eval-statement env ctx stmt)
(if (> (debug) 0)
(fprintf (current-output-port)
"ersatz: stmt = ~A~%" stmt))
(cases tstmt stmt
(TextStatement (text)
(tvalue-output ctx (Tstr text) safe: #t)
ctx)
(ExpandStatement (expr)
(tvalue-output ctx (eval-expr env ctx expr)
autoescape: (tmpl-env-autoescape env)
safe: (safe-expr? expr))
ctx)
(SetStatement (setexpr expr)
(cases texpr setexpr
(SetExpr (ident-lst)
(let ((ctx1
(bind-names ctx (ident-names-of ident-lst)
(eval-expr env ctx expr))))
ctx1))
(DotExpr (ns-expr prop-expr)
(let ((ns (ident-expr->name ns-expr))
(prop (ident-expr->name prop-expr)))
(extend-namespace ctx ns prop (eval-expr env ctx expr))))
(else (error 'eval-statement "invalid set expression" setexpr))))
(NamespaceStatement (ns bind-exprs)
(let ((h (fold (lambda (bind-expr h)
(let ((name (car bind-expr))
(expr (cdr bind-expr)))
(cons `(,name . ,(eval-expr env ctx expr)) h)))
'() bind-exprs)))
(let ((ctx1 (set-namespace ctx ns h)))
ctx1)))
(FilterStatement (nexpr stmts)
(let ((name (ident-expr->name nexpr)))
(let* ((ctx (set-filter ctx name))
(ctx (fold (lambda (s ctx) (eval-statement env ctx s))
ctx stmts)))
(pop-filter ctx)
)))
(IfStatement (conds elses)
(letrec ((select-case
(lambda (lst)
(if (pair? lst)
(let ((h (car lst)))
(let ((expr (car h)))
(if (is-true (eval-expr env ctx expr))
(cdr h)
(select-case (cdr lst)))
))
elses))))
(fold (lambda (x ctx) (eval-statement env ctx x)) ctx (select-case conds))
))
(ForStatement (iterator list-expr stmts)
(let ((iterator
(cases texpr iterator
(IdentExpr (name) (list name))
(SetExpr (lst) (ident-names-of lst))
(ListExpr (lst) (ident-names-of lst))
(else (error 'eval-statement "invalid iterator" iterator)))))
(iter ctx iterator
(lambda (ctx)
(fold (lambda (x ctx) (eval-statement env ctx x)) ctx stmts))
(eval-expr env ctx list-expr))
))
(BlockStatement (idexpr endexpr stmts)
(let* ((name (ident-expr->name idexpr))
(endname (or (and endexpr (ident-expr->name endexpr)) name)))
(if (equal? name endname)
(fold (lambda (x ctx) (eval-statement env ctx x)) ctx stmts)
(error 'eval-statement "mismatch between block begin name and end name"
name endname))))
(CallStatement (idexpr call-args-def macro-args call-stmts)
(let* ((name (ident-expr->name idexpr))
(mac (get-macro ctx name)))
(if mac
(let ((call-arg-names (ident-names-of call-args-def))
(call-defaults (kwargs-of env ctx call-args-def)))
(let* ((ctx (set-macro ctx 'caller
(make-template-macro call-arg-names call-defaults call-stmts)))
(text (eval-expr env ctx (ApplyExpr (IdentExpr name) macro-args)))
(ctx (pop-macro ctx)))
(cases tvalue text
(Tnull () (begin))
(else (tvalue-output ctx (Tstr (->string text)))))
ctx
))
(error 'eval-statement "macro not found" name)
)
))
(IncludeStatement (path w)
(if w
(let ((stmts (statements-from-file env path)))
(fold (lambda (x ctx) (eval-statement env ctx x)) ctx stmts))
(let ((ctx1 (init-context env)))
(let ((stmts (statements-from-file env path)))
(fold (lambda (x ctx) (eval-statement env ctx x)) ctx1 stmts)
ctx))
))
(WithStatement (binds stmts)
(let* ((kwargs (kwargs-of env ctx binds))
(names (map car kwargs))
(values (map cdr kwargs))
(ctx (push-frame ctx))
(ctx (set-values ctx names values))
(ctx (fold (lambda (s ctx) (eval-statement env ctx s)) ctx stmts)))
(pop-frame ctx)))
(AutoEscapeStatement (expr stmts)
(let ((ctx (cases tvalue (eval-expr env ctx expr)
(Tbool (v)
(if v (set-filter ctx 'escape)
(set-filter ctx 'safe)))
(else
(error 'eval-statement "invalid autoescape argument" expr)))))
(fold (lambda (s ctx) (eval-statement env ctx s)) ctx stmts)
(pop-filter ctx)
))
(else ctx)
))
(define (safe-expr? x)
(cases texpr x
(ApplyExpr (idexpr _)
(or
(eq? 'safe (ident-expr->name/safe idexpr))
(safe-expr? idexpr)))
(else #f)))
(define (unfold-extends env ctx stmts)
(let recur ((ret '()) (stmts stmts))
(if (null? stmts) ret
(cases tstmt (car stmts)
(ExtendsStatement (path)
(let ((stmts1 (unfold-extends env ctx (statements-from-file env path))))
(recur (append ret stmts1) (cdr stmts))))
(else (recur (append ret (list (car stmts))) (cdr stmts)))
))
))
(define (align-block stmts)
(define (same-block? name stmt)
(cases tstmt stmt
(BlockStatement (idexpr _ _)
(let ((name1 (ident-expr->name/safe idexpr)))
(equal? name1 name)))
(else #f)))
(define (erase-block name lst)
(filter (lambda (x) (not (same-block? name x))) lst))
(let recur ((ret '()) (stmts stmts))
(if (null? stmts) (reverse ret)
(begin
(let ((block (car stmts)))
(cases tstmt block
(BlockStatement (idexpr _ _)
(let* ((name (ident-expr->name idexpr))
(block1 (find (lambda (x) (same-block? name x)) (cdr stmts))))
(if block1
(recur (cons block1 ret)
(erase-block name (cdr stmts)))
(recur (cons block ret) (cdr stmts)))))
(else (recur (cons block ret) (cdr stmts))))
)))
))
(define (import-macro env ctx codes #!key (namespace #f) (select #f))
(let ((macro-name (lambda (name) (if namespace (string->symbol (sprintf "~A.~A" namespace name)) name)))
(alias-name (lambda (name) (if select (alist-ref name select) name)))
(can-import? (lambda (name) (if select (assoc name select) #t))))
(fold (lambda (code ctx)
(cases tstmt code
(MacroStatement (idexpr def-args stmts)
(let ((name (ident-expr->name idexpr)))
(if (can-import? name)
(let ((arg-names (ident-names-of def-args))
(kwargs (kwargs-of env ctx def-args)))
(let ((full-name (macro-name (alias-name name))))
(set-macro ctx
full-name
(make-template-macro arg-names kwargs stmts))))
ctx
)))
(BlockStatement (x x1 stmts)
(import-macro env ctx stmts namespace: namespace select: select))
(IncludeStatement (path namespace)
(import-macro env ctx
(statements-from-file env path)
namespace: namespace select: select ))
(ImportStatement (path namespace)
(let ((res (import-macro env ctx (statements-from-file env path)
namespace: namespace
select: select)))
res))
(FromImportStatement (path select-macros)
(let ((alias-names (alias-names-of select-macros)))
(let ((res (import-macro env ctx
(statements-from-file env path)
namespace: namespace
select: alias-names
)))
res
)))
(else ctx))
)
ctx codes)
))
(define (eval-statements codes #!key
(env (template-std-env))
(models '())
(ctx #f))
(let* ((ctx (or ctx (init-context env: env models: models)))
(codes (align-block (unfold-extends env ctx codes)))
(ctx (import-macro env ctx codes)))
(fold (lambda (s ctx) (eval-statement env ctx s)) ctx codes)
(let ((output (get-output-string (tmpl-ctx-buffer ctx))))
output)
))