-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.html
114 lines (99 loc) · 2.62 KB
/
test.html
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
<html>
<head>
<title>LISP Interpreter</title>
<script src="jquery-1.7.2.min.js"></script>
<script src="jquery.terminal-0.4.15.js"></script>
<script src="lisp.js"></script>
<script src="lisp-parser.js"></script>
<script src="lisp-eval.js"></script>
<script src="lisp-macro.js"></script>
<script src="lisp-builtins.js"></script>
<script src="lisp-repl.js"></script>
<script src="lisp-test.js"></script>
<link rel="stylesheet" type="text/css" href="terminal.css" />
<script>
$(function() {
lisp.replTerminal($('#term'));
lisp.replLoader($('#source'), $('#load'));
lisp.test();
$('body').click(function() { lisp.terminal.disable(); });
$('#clear').click(function () { lisp.terminal.clear(); });
});
</script>
</head>
<body>
<div id="term" style="width: 48%; height: 300px; float: left;"></div>
<div style="width: 48%; float: right;">
<textarea id="source" style="width: 100%; height: 300px;">
;; A few examples. Click 'Load source' to load them.
; Closures
(let (count 0)
(define (counter) (set! count (+ 1 count))))
; You can also define variables
(define x 2)
(set! x 3)
; Let
(let (x 2 y 3) (+ x y))
; Lambda
(set! x (lambda () 'bla))
(x)
; Factorial example
(define (fact n)
(if (= n 0)
1
(* n (fact (- n 1)))))
(fact 5)
; Length example
(define (length list)
(if (empty? list)
0
(+ 1 (length (cdr list)))))
(length '(1 2 3))
; Map example
(define (map func list)
(if (empty? list)
nil
(cons (func (car list))
(map func (cdr list)))))
(map (lambda (x) (+ x 1)) '(1 2 3))
; And macro example
(defmacro (and x . xs)
(if (empty? xs)
x
`(when ,x (and . ,xs))))
(expand-code '(and a b c d))
; and to demonstrate the short-circuit mechanism:
; ((unknown) should crash when run)
(and (= 2 2) (= 2 3) (unknown))
;; Hygiene in macros
; Consider a simple macro:
(defmacro (bad-swap a b)
`(let (tmp ,a)
(set! ,a ,b)
(set! ,b tmp)))
; Usage example:
(let (x 1 y 2)
(bad-swap x y)
(list x y))
; (2 1)
; Unfortunately, this fails if we have a variable called tmp!
(let (x 1 tmp 2)
(bad-swap x tmp)
(list x tmp))
; (1 2)
; We solve that by using the gensym function to generate a new name
; that will not conflict with anything else:
(defmacro (swap a b)
(let (tmp (gensym 'tmp))
`(let (,tmp ,a)
(set! ,a ,b)
(set! ,b ,tmp))))
; Now try:
(expand-code '(swap x tmp))
; (let (#tmp-0 x) (set! x tmp) (set! tmp #tmp-0))
</textarea>
<input type="button" id="clear" value="Clear terminal" />
<input type="button" id="load" value="Load source" />
</div>
</body>
</html>