-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.lisp
546 lines (508 loc) · 23.7 KB
/
parser.lisp
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
(in-package #:compiler)
(defun compile-class (token-list)
"The highest level parser for each class. Returns a new token-list and a parsed-list.
Structure: 'class' className '{' classVarDec* subroutineDec* '}'"
(let ((parsed-list (list)))
(if (string= "class" (first token-list))
(setf parsed-list (list "<class>" "<keyword> class </keyword>"))
(error "The first token of a class should be \"class\"."))
(if (eql 'identifier (token-type (second token-list)))
(nconc parsed-list (list (concatenate 'string
"<identifier> "
(second token-list)
" </identifier>")))
(error "The second token of a class should be an identifier."))
(if (string= #\{ (third token-list))
(nconc parsed-list (list "<symbol> { </symbol>"))
(error "The third token of a class should be {."))
(setf token-list (cdddr token-list))
;; Check if the next structure is classVarDec
(loop while (member (first token-list) '("static" "field") :test 'string=)
do (multiple-value-bind (new-token-list new-parsed-list)
(compile-class-var-dec token-list parsed-list)
(setf token-list new-token-list)
(setf parsed-list new-parsed-list)))
;; Check if the next structure is subroutineDec
(loop while (member (first token-list) '("constructor" "function" "method") :test 'string=)
do (multiple-value-bind (new-token-list new-parsed-list)
(compile-subroutine token-list parsed-list)
(setf token-list new-token-list)
(setf parsed-list new-parsed-list)))
(if (string= #\} (first token-list))
(setf parsed-list (append parsed-list (list "<symbol> } </symbol>")))
(error "The last token of a class should be }."))
(append parsed-list (list "</class>"))))
(defun compile-class-var-dec (token-list &optional (parsed-list '()))
"Structure: ('static' | 'field') type varName (',' varName)* ';'"
(let ((appendent (list "<classVarDec>")))
(nconc appendent (list (concatenate 'string "<keyword> " (first token-list) " </keyword>")))
(if (is-type (second token-list))
(if (eql 'identifier (token-type (second token-list)))
(nconc appendent (list (concatenate 'string
"<identifier> "
(second token-list)
" </identifier>")))
(nconc appendent (list (concatenate 'string
"<keyword> "
(second token-list)
" </keyword>"))))
(error "The second token of a classVarDec should be a keyword."))
(if (eql 'identifier (token-type (third token-list)))
(nconc appendent (list (concatenate 'string
"<identifier> "
(third token-list)
" </identifier>")))
(error "The third token of a classVarDec should be an identifier."))
(let ((rest-tokens (cdddr token-list)))
(loop while (and (string= "," (first rest-tokens))
(eql 'identifier (token-type (second rest-tokens))))
do
(nconc appendent (list "<symbol> , </symbol>"
(concatenate 'string "<identifier> "
(second rest-tokens)
" </identifier>")))
(setf rest-tokens (cddr rest-tokens)))
(if (string= ";" (first rest-tokens))
(nconc appendent (list "<symbol> ; </symbol>"))
(error "The last token of a classVarDec should be ';'."))
(values (rest rest-tokens) (append parsed-list appendent (list "</classVarDec>"))))))
(defun is-type (token)
"Check if the token is a type."
(or (member token (list "int" "char" "boolean") :test 'string=)
(eql 'identifier (token-type token))))
(defun is-keyword-type (token)
"Check if the token is ('int' | 'char' | boolean')."
(member token (list "int" "char" "boolean") :test 'string=))
(defun compile-subroutine (token-list &optional (parsed-list '()))
"Structure: ('constructor' | 'function' | 'method') ('void' | type) subroutineName '(' parameterList ')' subroutineBody"
(let ((appendent (list "<subroutineDec>")))
(nconc appendent (list (concatenate 'string "<keyword> " (first token-list) " </keyword>")))
(if (or (string= "void" (second token-list)) (is-type (second token-list)))
(if (eql 'identifier (token-type (second token-list)))
(nconc appendent (list (concatenate 'string
"<identifier> "
(second token-list)
" </identifier>")))
(nconc appendent (list (concatenate 'string
"<keyword> "
(second token-list)
" </keyword>"))))
(error "The second token of subroutineDec should be type or 'void'."))
(if (eql 'identifier (token-type (third token-list)))
(nconc appendent (list (concatenate 'string
"<identifier> "
(third token-list)
" </identifier>")))
(error "The third token of subroutineDec should be an identifier."))
(let ((rest-tokens (cdddr token-list)))
(if (string= "(" (first rest-tokens))
(nconc appendent (list "<symbol> ( </symbol>"))
(error "Missing a '(' before parameterList."))
(setf rest-tokens (rest rest-tokens))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-parameter-list rest-tokens appendent)
(setf rest-tokens new-rest-tokens)
(setf appendent new-appendent))
(if (string= ")" (first rest-tokens))
(nconc appendent (list "<symbol> ) </symbol>"))
(error "Missing a ')' after parameterList."))
(if (string= "{" (second rest-tokens))
(nconc appendent (list "<subroutineBody>" "<symbol> { </symbol>")) ;; wrong before here
(error "Missing a '{' before subroutineBody."))
(setf rest-tokens (cddr rest-tokens))
;; varDec
(loop while (string= "var" (first rest-tokens))
do (multiple-value-bind (new-rest-tokens new-appendent)
(compile-var-dec rest-tokens appendent)
(setf rest-tokens new-rest-tokens)
(setf appendent new-appendent)))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-statements rest-tokens appendent)
(setf rest-tokens new-rest-tokens)
(setf appendent new-appendent))
(if (string= "}" (first rest-tokens))
(setf appendent (append appendent
(list "<symbol> } </symbol>" "</subroutineBody>")))
(error "The end of a subroutineBody should be '}'."))
(values (rest rest-tokens) (append parsed-list appendent (list "</subroutineDec>"))))))
(defun compile-parameter-list (token-list &optional (parsed-list '()))
"Structure: ((type varName) (',' type varName)*)?"
(let ((appendent (list "<parameterList>")))
(loop while (is-type (first token-list))
do (if (is-keyword-type (first token-list))
(nconc appendent
(list (concatenate 'string "<keyword> "
(first token-list)
" </keyword>"))
(list (concatenate 'string "<identifier> "
(second token-list)
" </identifier>")))
(nconc appendent
(list (concatenate 'string "<identifier> "
(first token-list)
" </identifier>")
(concatenate 'string "<identifier> "
(second token-list)
" </identifier>"))))
(cond ((string= #\, (third token-list))
(nconc appendent (list "<symbol> , </symbol>"))
(setf token-list (cdddr token-list)))
((string= #\) (third token-list))
(setf token-list (cddr token-list)))
(t
(print appendent)
(error "There's an error in the parameterList."))))
(values token-list (append parsed-list appendent (list "</parameterList>")))))
(defun compile-var-dec (token-list &optional (parsed-list '()))
"Structure: 'var' type varName (',' varName)* ';'"
(let ((appendent (list "<varDec>" "<keyword> var </keyword>")))
(assert (eql 'keyword (token-type (first token-list))))
(if (is-type (second token-list))
(cond ((eql 'keyword (token-type (second token-list)))
(nconc appendent (list (concatenate 'string
"<keyword> "
(second token-list)
" </keyword>"))))
((eql 'identifier (token-type (second token-list)))
(nconc appendent (list (concatenate 'string
"<identifier> "
(second token-list)
" </identifier>"))))
(t (error "Type in varDec should be keyword or identifier.")))
(error "Second element of varDec should be type."))
(if (eql 'identifier (token-type (third token-list)))
(nconc appendent (list (concatenate 'string
"<identifier> "
(third token-list)
" </identifier>")))
(error "Third element of varDec should be an identifier."))
(let ((rest-tokens (cdddr token-list)))
(loop while (string= #\, (first rest-tokens))
do (nconc appendent (list "<symbol> , </symbol>"))
(if (eql 'identifier (token-type (second rest-tokens)))
(nconc appendent (list (concatenate 'string
"<identifier> "
(second rest-tokens)
" </identifier>")))
(error "varName should be an identifier."))
(setf rest-tokens (cddr rest-tokens)))
(if (string= #\; (first rest-tokens))
(values (cdr rest-tokens) (append parsed-list appendent
(list "<symbol> ; </symbol>" "</varDec>")))
(error "Last element of varDec should be ;.")))))
(defun compile-statement (token-list &optional (parsed-list '()))
"Structure: (let | if | while | do | return)"
(let ((first-element (first token-list)))
(cond ((string= "let" first-element)
(compile-let token-list parsed-list))
((string= "if" first-element)
(compile-if token-list parsed-list))
((string= "while" first-element)
(compile-while token-list parsed-list))
((string= "do" first-element)
(compile-do token-list parsed-list))
((string= "return" first-element)
(compile-return token-list parsed-list))
(t (error "Statement not recognized.")))))
(defun compile-statements (token-list &optional (parsed-list '()))
(let ((appendent (list "<statements>"))
(rest-tokens token-list))
(loop while (member (first rest-tokens) '("let" "if" "while" "do" "return") :test 'string=)
do (multiple-value-bind (new-rest-tokens new-appendent)
(compile-statement rest-tokens appendent)
(setf rest-tokens new-rest-tokens)
(setf appendent new-appendent)))
(values rest-tokens (append parsed-list appendent '("</statements>")))))
(defun compile-let (token-list &optional (parsed-list '()))
"Structure: 'let' varName ('[' expression ']')? '=' expression ';'"
(let ((appendent (list "<letStatement>" "<keyword> let </keyword>")))
(if (eql 'identifier (token-type (second token-list)))
(nconc appendent (list (concatenate 'string
"<identifier> "
(second token-list)
" </identifier>")))
(error "varName should be an identifier."))
(let ((rest-tokens (cddr token-list)))
(when (string= #\[ (first rest-tokens))
(nconc appendent (list "<symbol> [ </symbol>"))
(multiple-value-bind (new-token-list new-appendent)
(compile-expression (rest rest-tokens) appendent)
(setf rest-tokens new-token-list)
(setf appendent new-appendent))
(assert (string= #\] (first rest-tokens)))
(nconc appendent (list "<symbol> ] </symbol>"))
(setf rest-tokens (rest rest-tokens)))
(assert (string= #\= (first rest-tokens)))
(nconc appendent (list "<symbol> = </symbol>"))
;; (assert (eql 'identifier (token-type (second rest-tokens))))
(multiple-value-bind (new-token-list new-appendent)
(compile-expression (rest rest-tokens) appendent)
(setf rest-tokens new-token-list)
(setf appendent new-appendent))
(assert (string= #\; (first rest-tokens)))
(values (rest rest-tokens)
(append parsed-list appendent
(list "<symbol> ; </symbol>" "</letStatement>"))))))
(defun compile-do (token-list &optional (parsed-list '()))
"Structure: 'do' subroutineCall ';'"
(let ((appendent (list "<doStatement>" "<keyword> do </keyword>")))
(multiple-value-bind (new-token-list new-appendent)
(compile-subroutine-call (rest token-list) appendent)
(assert (string= #\; (first new-token-list)))
(values (rest new-token-list) (append parsed-list new-appendent
(list "<symbol> ; </symbol>") (list "</doStatement>"))))))
(defun compile-while (token-list &optional (parsed-list '()))
"Structure: 'while' '(' expression ')' '{' statements '}'"
(let ((appendent (list "<whileStatement>" "<keyword> while </keyword>")))
(assert (string= #\( (second token-list)))
(nconc appendent (list "<symbol> ( </symbol>"))
(let ((rest-tokens (cddr token-list)))
(multiple-value-bind (new-token-list new-appendent)
(compile-expression rest-tokens appendent)
(setf rest-tokens new-token-list)
(setf appendent new-appendent))
(assert (string= #\) (first rest-tokens)))
(nconc appendent (list "<symbol> ) </symbol>"))
(assert (string= #\{ (second rest-tokens)))
(nconc appendent (list "<symbol> { </symbol>"))
(multiple-value-bind (new-token-list new-appendent)
(compile-statements (cddr rest-tokens) appendent)
(setf rest-tokens new-token-list)
(setf appendent new-appendent))
(assert (string= #\} (first rest-tokens)))
;; (nconc appendent (list "<symbol> } </symbol>"))
(values (cdr rest-tokens) (append parsed-list appendent
(list "<symbol> } </symbol>")
(list "</whileStatement>"))))))
(defun compile-return (token-list &optional (parsed-list '()))
"Structure: 'return' expression? ';'"
;; (declare (optimize debug))
(let ((appendent (list "<returnStatement>" "<keyword> return </keyword>"))
(rest-tokens token-list))
;; for now assume expression is an identifier
(if (is-term (second rest-tokens))
(multiple-value-bind (new-token-list new-appendent)
(compile-expression (rest rest-tokens) appendent)
(setf rest-tokens new-token-list)
(setf appendent new-appendent))
(setf rest-tokens (rest rest-tokens)))
(assert (string= #\; (first rest-tokens)))
(values (rest rest-tokens) (append parsed-list appendent
(list "<symbol> ; </symbol>" "</returnStatement>")))))
(defun compile-if (token-list &optional (parsed-list '()))
"Structure: 'if' '(' expression ')' '{' statements '}'"
(let ((appendent (list "<ifStatement>" "<keyword> if </keyword>"))
(rest-tokens token-list))
(assert (string= "if" (first rest-tokens)))
(assert (string= #\( (second rest-tokens)))
(nconc appendent (list "<symbol> ( </symbol>"))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-expression (cddr rest-tokens) appendent)
(setf rest-tokens new-rest-tokens)
(setf appendent new-appendent))
(assert (string= #\) (first rest-tokens)))
(nconc appendent (list "<symbol> ) </symbol>"))
(assert (string= #\{ (second rest-tokens)))
(nconc appendent (list "<symbol> { </symbol>"))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-statements (cddr rest-tokens) appendent)
(setf rest-tokens new-rest-tokens)
(setf appendent new-appendent))
(assert (string= #\} (first rest-tokens)))
(setf appendent (append appendent (list "<symbol> } </symbol>")))
(setf rest-tokens (rest rest-tokens))
(when (string= "else" (first rest-tokens))
(nconc appendent (list "<keyword> else </keyword>"))
(assert (string= #\{ (second rest-tokens)))
(nconc appendent (list "<symbol> { </symbol>"))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-statements (cddr rest-tokens) appendent)
(setf rest-tokens new-rest-tokens)
(setf appendent new-appendent))
(assert (string= #\} (first rest-tokens)))
(setf appendent (append appendent (list "<symbol> } </symbol>")))
(setf rest-tokens (rest rest-tokens)))
(values rest-tokens (append parsed-list appendent (list "</ifStatement>")))))
(defun is-op (token)
"Return t if token is an op."
(when (member token '("+" "-" "*" "/" "&" "|" "<" ">" "=") :test 'string=)
t))
(defun normalize-op (token)
"Change '<' to '<', '>' to '>', '&' to '&'."
(cond ((string= #\< token)
"<")
((string= #\> token)
">")
((string= #\& token)
"&")
(t token)))
(defun compile-expression (token-list &optional (parsed-list '()))
"Structure: term (op term)*"
(let ((appendent (list)))
(when (is-term (first token-list))
(setf appendent (list "<expression>"))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-term token-list appendent)
(setf token-list new-rest-tokens)
(setf appendent new-appendent))
(loop while (is-op (first token-list))
do (nconc appendent (list (concatenate 'string
"<symbol> "
(normalize-op (string (first token-list)))
" </symbol>")))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-term (rest token-list) appendent)
(setf token-list new-rest-tokens)
(setf appendent new-appendent)))
(nconc appendent (list "</expression>")))
(values token-list (append parsed-list appendent))))
(defun is-term (token)
"Returns t if the next (several) element(s) is(are) term(s)."
(or (eql 'int-const (token-type token))
(eql 'string-const (token-type token))
(eql 'keyword (token-type token))
(string= #\( token)
(member token '("-" "~") :test 'string=)
(eql 'identifier (token-type token))))
(defun compile-term (token-list &optional (parsed-list '()))
"Structure: integerConstant | stringConstant | keywordConstant | varName | varName '[' expression ']' | subroutineCall | '(' expression ')' | unaryOp term"
(let ((appendent (list "<term>")))
(cond ((eql 'int-const (token-type (first token-list)))
(nconc appendent (list (concatenate 'string "<integerConstant> "
(first token-list)
" </integerConstant>")))
(setf token-list (rest token-list)))
((eql 'string-const (token-type (first token-list)))
(nconc appendent (list (concatenate 'string "<stringConstant> "
(subseq (first token-list) 1 (1- (length (first token-list))))
" </stringConstant>")))
(setf token-list (rest token-list)))
((eql 'keyword (token-type (first token-list)))
(nconc appendent (list (concatenate 'string "<keyword> "
(first token-list)
" </keyword>")))
(setf token-list (rest token-list)))
((string= #\( (first token-list))
(nconc appendent (list "<symbol> ( </symbol>"))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-expression (rest token-list) appendent)
(setf token-list new-rest-tokens)
(setf appendent new-appendent))
(assert (string= #\) (first token-list)))
(setf token-list (rest token-list))
(nconc appendent (list "<symbol> ) </symbol>")))
((eql 'identifier (token-type (first token-list)))
(cond ((string= #\[ (second token-list))
(nconc appendent
(list (concatenate 'string "<identifier> "
(first token-list)
" </identifier>"))
(list "<symbol> [ </symbol>"))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-expression (cddr token-list) appendent)
(setf token-list new-rest-tokens)
(setf appendent new-appendent))
(assert (string= #\] (first token-list)))
(nconc appendent (list "<symbol> ] </symbol>"))
(setf token-list (rest token-list)))
((or (string= #\( (second token-list))
(string= #\. (second token-list)))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-subroutine-call token-list appendent)
(setf token-list new-rest-tokens)
(setf appendent new-appendent)))
(t (nconc appendent (list (concatenate 'string "<identifier> "
(first token-list)
" </identifier>")))
(setf token-list (rest token-list)))))
((member (first token-list) '("-" "~") :test 'string=)
(nconc appendent (list (concatenate 'string "<symbol> "
(normalize-unary-op (first token-list))
" </symbol>")))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-term (rest token-list) appendent)
(setf token-list new-rest-tokens)
(setf appendent new-appendent)))
(t (error "Invalid term.")))
(values token-list (append parsed-list appendent (list "</term>")))))
(defun normalize-unary-op (token)
(if (string= #\~ token)
"~~"
(string token)))
(defun compile-subroutine-call (token-list &optional (parsed-list '()))
"Structure: subroutineName '(' expressionList ')' | (className | varName) '.' subroutineName '(' expressionList ')'"
(let ((appendent (list))
(rest-tokens token-list))
(assert (eql 'identifier (token-type (first rest-tokens))))
(setf appendent (list (concatenate 'string "<identifier> " (first rest-tokens) " </identifier>")))
(cond ((string= #\( (second rest-tokens))
(nconc appendent (list "<symbol> ( </symbol>"))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-expression-list (cddr rest-tokens) appendent)
(setf rest-tokens new-rest-tokens)
(setf appendent new-appendent))
(assert (string= #\) (first rest-tokens)))
(nconc appendent (list "<symbol> ) </symbol>")))
((string= #\. (second rest-tokens))
(nconc appendent (list "<symbol> . </symbol>"))
(assert (eql 'identifier (token-type (third rest-tokens))))
(nconc appendent (list (concatenate 'string "<identifier> " (third rest-tokens) " </identifier>")))
(assert (string= #\( (fourth rest-tokens)))
(nconc appendent (list "<symbol> ( </symbol>"))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-expression-list (nthcdr 4 rest-tokens) appendent)
(setf rest-tokens new-rest-tokens)
(setf appendent new-appendent))
(assert (string= #\) (first rest-tokens)))
(nconc appendent (list "<symbol> ) </symbol>")))
(t (error "Something is wrong with the subroutine calling.")))
(values (cdr rest-tokens) (append parsed-list appendent))))
(defun compile-expression-list (token-list &optional (parsed-list '()))
"Structure: (expression (',' expression)*)?"
(let ((appendent (list "<expressionList>"))
(rest-tokens token-list))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-expression rest-tokens appendent)
(setf rest-tokens new-rest-tokens)
(setf appendent new-appendent))
(loop while (string= #\, (first rest-tokens))
do (nconc appendent (list "<symbol> , </symbol>"))
(multiple-value-bind (new-rest-tokens new-appendent)
(compile-expression (rest rest-tokens) appendent)
(setf rest-tokens new-rest-tokens)
(setf appendent new-appendent)))
(values rest-tokens (append parsed-list appendent (list "</expressionList>")))))
(defun jack-parser (tokenizer file stream)
"Tokenize the file, then write the parsed results to stream."
(let* ((tokenized-list (funcall tokenizer file))
(parsed-list (compile-class tokenized-list)))
(dolist (line parsed-list)
(format stream (concatenate 'string line "~%")))))
(defun parser-writer (dir &optional (tokenizer #'tokenizer))
(let* ((jack-files (directory (concatenate 'string dir "/*.jack")))
(output-folder (concatenate 'string dir "/parsed/")))
(dolist (jack-file jack-files)
(let ((xml-file (concatenate 'string output-folder (pathname-name jack-file) ".xml")))
(with-open-file (stream xml-file :direction :output :if-exists :supersede)
(jack-parser tokenizer jack-file stream))))))
;; Unit tests
(defun compile-var-dec-test ()
(compile-var-dec (list "var" "int" "bar" "," "foo" ";") '()))
(defun compile-expression-test ()
(compile-expression (list "bar" "+" "barz" "-" "foo") '()))
(defun compile-let-test ()
(compile-let (list "let" "tmp" "=" "foo" ";") '()))
(defun compile-return-test ()
(compile-return (list "return" "foo" ";") '()))
(defun compile-statements-test ()
(compile-statements '("let" "temp" "=" "x" ";" "let" "foo" "=" "bar" ";") '()))
(defun compile-if-test ()
(compile-if (list "if" "(" "foo" ")" "{" "return" "zoo" ";" "}" "else" "{" "let" "foo" "=" "bar" ";" "}")))
(defun compile-expression-list-test ()
(compile-expression-list (list "kos" "," "nko" "," "oii")))
(defun compile-subroutine-call-test ()
;; (compile-subroutine-call '("did" "(" ")"))
(compile-subroutine-call '("did" "(" "expr" ")" "." "expr2" "(" ")")))
(defun compile-do-test ()
(compile-do '("do" "func" "(" "expr" ")" "." "expr2" "(" ")" ";")))
;; (parser-writer "~/nand2tetris/projects/10/Square/" #'tokenizer)