-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreparse.ss
90 lines (86 loc) · 2.18 KB
/
preparse.ss
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
;;
;; This script takes Scheme expressions and preparses it into a form that is
;; ready to be directly loaded into FoxScheme. This script automatically adds
;; JavaScript code to load the expressions into FoxScheme at runtime, so all
;; that is needed is to execute the JavaScript file.
;;
;;
;; Sends the result of preparse to the current-output-port (STDOUT)
;;
(define (preparse expr)
(cond
((list? expr)
(echo-list expr))
((pair? expr)
(format "p(~a, ~a)"
(preparse (car expr))
(preparse (cdr expr))))
((symbol? expr)
(format "s(\"~a\")" expr))
((vector? expr)
(echo-vector expr))
((string? expr)
(format "t(\"~a\")" expr))
((number? expr)
(format "~s" expr))
((boolean? expr)
(echo-boolean expr))
((char? expr)
(format "c(\"~a\")" expr))
(else (error 'preparse "Don't know what to do with " expr))))
(define (echo-boolean expr)
(if expr
"true"
"false"))
(define (echo-list expr)
(if (null? expr)
"nil"
(string-append
"ls(["
(letrec ((loop-list
(lambda (ls)
(if (null? (cdr ls))
(format "~a])\n" (preparse (car ls)))
(format "~a,~a"
(preparse (car ls))
(loop-list (cdr ls)))))))
(loop-list expr)))))
(define (echo-vector expr)
(do ((i 0 (+ 1 i))
(str "v([" (string-append str "," (preparse (vector-ref expr i)))))
((= i (vector-length expr))
(string-append str "])"))))
(display "(function (default_load_function, ls, p, v, s, t, c, nil) {\n")
(let loop ((dat (read)))
(if (eof-object? dat)
#t
(begin
(display "default_load_function(")
(display (preparse dat))
(display ");\n")
(loop (read)))))
(display "
})(
$interpreter.eval,
// ls
FoxScheme.Util.listify,
// p
function(a, b) {
return new FoxScheme.Pair(a, b);
},
// v
function(v) {
return new FoxScheme.Vector(v);
},
// s
function(s) {
return new FoxScheme.Symbol(s);
},
function(t) {
return new FoxScheme.String(t);
},
function(c) {
return new FoxScheme.Char(c);
},
FoxScheme.nil
)")