-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.lisp
More file actions
192 lines (166 loc) · 7.52 KB
/
tests.lisp
File metadata and controls
192 lines (166 loc) · 7.52 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
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
(defpackage tests
(:use :cl))
(in-package tests)
(defun tokens-eq (a b)
(and
(eq (lexer:token-type a) (lexer:token-type b))
(and
(if (stringp b)
(string= (lexer:token-lexeme a) (lexer:token-lexeme b))
(equal (lexer:token-lexeme a) (lexer:token-lexeme b)))
(and
(eq (lexer:token-literal a) (lexer:token-literal b))
(eq (lexer:token-line a) (lexer:token-line b))))))
(defun lists-eq (a b func)
(if (and (null a) (null b))
T
(and
(eq (length a) (length b))
(and
(funcall func (car a) (car b))
(lists-eq (cdr a) (cdr b) func)))))
(defun test-lexer-works()
(let ((source (format nil "
// this is a comment\n
(( )){} // grouping stuff
// \"This should be ignored\"
!*+-/=<> <= == // operators
!! 456 \"This is a asdf string\"
\"Numbers in a string are all good 123.4556\"
123
12.56\"asdf\"...
12.1.\"\"
var example = 123;
fun example() {
return x;
}
")))
(assert (lists-eq (list
(lexer:create-left-paren "(")
(lexer:create-left-paren "(")
(lexer:create-right-paren ")")
(lexer:create-right-paren ")")
(lexer:create-left-brace "{")
(lexer:create-right-brace "}")
(lexer:create-bang "!")
(lexer:create-star "*")
(lexer:create-plus "+")
(lexer:create-minus "-")
(lexer:create-slash "/")
(lexer:create-equal "=")
(lexer:create-less "<")
(lexer:create-greater ">")
(lexer:create-less-equal "<=")
(lexer:create-equal-equal "==")
(lexer:create-bang "!")
(lexer:create-bang "!")
(lexer:create-number 456)
(lexer:create-string "This is a asdf string")
(lexer:create-string "Numbers in a string are all good 123.4556")
(lexer:create-number 123)
(lexer:create-number 12.56)
(lexer:create-string "asdf")
(lexer:create-dot ".")
(lexer:create-dot ".")
(lexer:create-dot ".")
(lexer:create-number 12.1)
(lexer:create-dot ".")
(lexer:create-string "")
(lexer:create-var "var")
(lexer:create-identifier "example")
(lexer:create-equal "=")
(lexer:create-number 123)
(lexer:create-semi-colon ";")
(lexer:create-fun "fun")
(lexer:create-identifier "example")
(lexer:create-left-paren "(")
(lexer:create-right-paren ")")
(lexer:create-left-brace "{")
(lexer:create-return "return")
(lexer:create-identifier "x")
(lexer:create-semi-colon ";")
(lexer:create-right-brace "}")
(lexer:create-eof)) (lexer:scan-tokens (coerce source 'list)) #'tokens-eq))))
(defun test-lexer-works-no-trailing-space()
(let ((source "(1 + 1)"))
(assert (lists-eq (list
(lexer:create-left-paren "(")
(lexer:create-number 1)
(lexer:create-plus "+")
(lexer:create-number 1)
(lexer:create-right-paren ")")
(lexer:create-eof))
(lexer:scan-tokens (coerce source 'list)) #'tokens-eq))))
(parser:parse (lexer:scan-tokens (coerce "var x = 2;" 'list)))
(lexer:scan-tokens (coerce "(1+1)" 'list))
(test-lexer-works-no-trailing-space)
(defun test-lexer-works-simple()
(let ((source (format nil "
(123) + 12.6
fun test() {
}
")))
(assert (lists-eq (list
(lexer:create-left-paren "(")
(lexer:create-number 123)
(lexer:create-right-paren ")")
(lexer:create-plus "+")
(lexer:create-number 12.6)
(lexer:create-fun "fun")
(lexer:create-identifier "test")
(lexer:create-left-paren "(")
(lexer:create-right-paren ")")
(lexer:create-left-brace "{")
(lexer:create-right-brace "}")
(lexer:create-eof))
(lexer:scan-tokens (coerce source 'list)) #'tokens-eq))))
(defun test-lexer-works-empty()
(let ((source ""))
(assert (lists-eq (list (lexer:create-eof)) (lexer:scan-tokens (coerce source 'list)) #'tokens-eq ))))
(defun test-parser-works()
(let ((source "(5 + 2) * (5 - 2) / 3;"))
(let ((tree (parser:parse (lexer:scan-tokens (coerce source 'list)))))
(progn
(assert (string= "(/ (* (group (+ 5 2)) (group (- 5 2))) 3)" (printer:accept tree)))))))
(defun test-interpreter-literal-number()
(let ((tree (parser:parse (lexer:scan-tokens (coerce "1;" 'list)))))
(assert (eq 1 (interpreter:interpret tree)))))
(defun test-interpreter-literal-string()
(let ((tree (parser:parse (lexer:scan-tokens (coerce "\"Hello\";" 'list)))))
(assert (string= "Hello" (interpreter:interpret tree)))))
(defun test-interpreter-strings-added()
(let ((tree (parser:parse (lexer:scan-tokens (coerce "\"Hello\" + \"World\";" 'list)))))
(assert (string= "HelloWorld" (interpreter:interpret tree)))))
(defun test-interpreter-arithmetic()
(let ((tree (parser:parse (lexer:scan-tokens (coerce "(5 + 2) * (5 - 2) / 3;" 'list)))))
(assert (eq 7 (interpreter:interpret tree)))))
(defun test-interpreter-boolean-equality()
(let ((tree (parser:parse (lexer:scan-tokens (coerce "true == true;" 'list)))))
(assert (eq T (interpreter:interpret tree)))))
(defun test-interpreter-boolean-inequality()
(let ((tree (parser:parse (lexer:scan-tokens (coerce "true != true;" 'list)))))
(assert (eq nil (interpreter:interpret tree)))))
(defun test-interpreter-boolean-expressions()
(let ((tree (parser:parse (lexer:scan-tokens (coerce "2 < 3 == 4 < 5;" 'list)))))
(assert (eq T (interpreter:interpret tree)))))
(defun run-lexer-tests()
(progn
(test-lexer-works-simple)
(test-lexer-works)
(test-lexer-works-no-trailing-space)
(test-lexer-works-empty)))
(defun run-parser-tests()
(progn
(test-parser-works)))
(defun run-interpreter-tests()
(progn
(test-interpreter-literal-number)
(test-interpreter-literal-string)
(test-interpreter-strings-added)
(test-interpreter-arithmetic)
(test-interpreter-boolean-equality)
(test-interpreter-boolean-inequality)
(test-interpreter-boolean-expressions)))
(run-lexer-tests)
;(run-parser-tests)
(run-interpreter-tests)