-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.scm
More file actions
31 lines (26 loc) · 1.24 KB
/
compiler.scm
File metadata and controls
31 lines (26 loc) · 1.24 KB
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
(define (block? expr) (symbol? (car expr)))
(define (tagged-block? expr)
(and (block? expr) (if (list? (cadr expr)) (eq? (caadr expr) '@) #f)))
(define (compile expr)
(cond ((string? expr) expr)
((tagged-block? expr) (compile-block (car expr) (cadr expr) (cddr expr)))
((block? expr) (compile-block (car expr) '(@) (cdr expr)))
(else (compile-sentence expr))))
(define (compile-tag tag-info)
(if (null? tag-info) ""
(let ((var (symbol->string (caar tag-info)))
(val (string-append "\"" (cadar tag-info) "\"")))
(string-append " " var "=" val (compile-tag (cdr tag-info))))))
(define (compile-block block tag body)
(let* ((tag-data (compile-tag (cdr tag)))
(tag-begin (string-append "<" (symbol->string block) tag-data ">"))
(tag-end (string-append "</" (symbol->string block) ">")))
(string-append tag-begin (compile-sentence body) tag-end)))
(define (compile-sentence expr)
(let loop ((acc "") (e expr))
(cond ((null? e) acc)
((= 1 (length e))
(loop (string-append acc (compile (car e))) (cdr e)))
(else (loop (string-append acc (compile (car e)) " ") (cdr e))))))
(define (compile-template expr)
(compile (eval expr (interaction-environment))))