/
parser.rkt
402 lines (346 loc) · 12.1 KB
/
parser.rkt
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
#lang racket
(require rackunit)
(provide parse emit)
;; TODO: Better error handling (TESTS)
;; TODO: 'emit' function
;; Ripped from the original whitespace VM's parser definitions. We first map
;; the three meaningful symbols (space, tab, linefeed) to the symbols A,B,C:
(define A #\space)
(define B #\tab)
(define C #\linefeed)
;; Then define some magical macros which generate the parse rules necessary
;; for parse and emit. They are defined below.
;; parse :: any/c * input-port -> (or/c [WS-Expr] | #f)
;;
;; Reads input from the parameter input-port and exports an AST for the
;; whitespace program, which 'semantics.rkt' can execute.
(define (parse source-name input-port)
(define (recursive accum num-chars)
(cond
[(eof-object? (peek-char input-port)) (reverse accum)]
[(ignorable-char? input-port)
(read-char input-port)
(recursive accum (add1 num-chars))]
[else
(let ([expr (ormap (λ (fn) (fn input-port)) parse-functions)])
(if expr
(recursive (cons expr accum) (add1 num-chars))
(handle-error accum num-chars)))]))
(define (handle-error accum num-chars)
(printf "Error at ~a chars into input stream~n" num-chars)
(printf "Last character until unsafe stream was: ~a~n" (peek-char input-port))
(printf "Current instruction parse: ~a~n" accum)
#f)
(recursive '() 1))
;; emit :: [WS-Expr] -> '()
;;
;; Writes a whitespaced-syntax representation of the input program to
;; current-output-port.
(define (emit ws-prog)
0)
(define emit-functions '())
(define parse-functions '())
(define-syntax (define-ws-parse-rules stx)
(syntax-case stx ()
[(_ parse-name prefix
(case-prefix (case-value case-suffix))
(next-case-prefix (next-case-value next-case-suffix))
...)
(let* (
;; Create a function to check for a certain sequence of characters.
[checker (make-symbol-list-matcher (combine-symbols (list (syntax->datum #'prefix)
(syntax->datum #'case-prefix)))
stx)]
;; Create a function to add that checker to the global set of clauses.
[add-fun #`(set! parse-functions (cons (λ (input-port) (if (#,checker input-port)
(handle-suffix 'case-suffix input-port)
#f))
parse-functions))])
#`(begin
;; Add the check function to the set of all clauses.
#,add-fun
;; Process the next clause.
(define-ws-parse-rules parse-name prefix
(next-case-prefix (next-case-value next-case-suffix))
...)))]
[(_ parse-name prefix) #'(void)]))
;; 'ABAB -> '(A B A B)
(define-for-syntax (split-symbol symb)
(map (λ (x) (string->symbol (string x))) (string->list (symbol->string symb))))
;; '(A BB AA C) -> 'ABBAAC
(define-for-syntax (combine-symbols list-of-symbols)
(string->symbol (apply string-append (map symbol->string list-of-symbols))))
;; make-symbol-list-matcher :: Symbol * Syntax -> #'(input-port -> bool)
;;
;; Given a compound symbol (e.g. 'A or 'CB) will create a function that reads from an input
;; port and returns true or false, if the chars read match the expanded form. Note that in
;; the #t case, we destructively read from the input-port, on the #f case, input-port is
;; unchanged.
(define-for-syntax (make-symbol-list-matcher compound-symbol stx)
(let* ([list-of-symbols (split-symbol compound-symbol)]
[los-as-syntax (datum->syntax stx (cons 'list list-of-symbols))])
#`(λ (input-port)
(letrec ([sequential (λ (pattern-offset port-offset)
(cond
[(= pattern-offset #,(length list-of-symbols)) port-offset]
[(ignorable-char? input-port port-offset)
(sequential pattern-offset (add1 port-offset))]
[(eq? (peek-char input-port port-offset)
(list-ref #,los-as-syntax pattern-offset))
(sequential (add1 pattern-offset) (add1 port-offset))]
[else #f]))])
(let ([result (sequential 0 0)])
(if result
(repeat-read input-port result)
#f))))))
(define (repeat-read input-port len)
(cond
[(= len 0) #t]
[else (read-char input-port)
(repeat-read input-port (sub1 len))]))
;
;
;
;
; ;;;
; ;
; ;
; ; ;; ;;; ; ;; ;;; ;;; ; ;; ; ;; ; ;;; ;;;
; ;;; ; ; ; ;;; ; ; ; ; ; ;;; ; ; ;; ; ; ; ; ;
; ; ;; ;; ;; ; ; ;; ;; ; ;; ; ; ;; ;
; ; ; ;; ; ;;; ;;;;;;; ; ; ;; ; ;;;;;;; ;;;
; ; ; ;;;;;; ; ;;; ; ; ; ;; ; ; ;;;
; ; ; ; ;; ; ;; ; ; ; ;; ; ; ;;
; ;; ; ; ;; ; ;; ; ; ; ; ;; ;; ; ; ; ;; ;
; ; ;;; ;;;;;; ; ;;;; ;;;; ; ;;;;;; ;;;;;; ;;;; ;;;;
; ;
; ;
; ;
(define-ws-parse-rules stack-instr A
(A '(push NUMBER))
(BA '(ref NUMBER))
(BC '(slide NUMBER))
(CA '(dup))
(CB '(swap))
(CC '(discard)))
(define-ws-parse-rules control-instr C
(AA '(label LABEL))
(AB '(call LABEL))
(AC '(jump LABEL))
(BA '(if zero LABEL))
(BB '(if negative LABEL))
(BC '(return))
(CC '(end)))
(define-ws-parse-rules arithmetic-instr BA
(AA '(infix plus))
(AB '(infix minus))
(AC '(infix times))
(BA '(infix divide))
(BB '(infix modulo)))
(define-ws-parse-rules heap-access BB
(A '(store))
(B '(retrieve)))
(define-ws-parse-rules io-instr BC
(AA '(output-char))
(AB '(output-int))
(BA '(read-char))
(BB '(read-int)))
;
;
;
;
; ;; ;;;
; ;; ;
; ;; ;
; ;; ;; ;;; ; ; ;; ;;; ; ;; ;;;
; ;;; ; ; ; ; ;;; ; ; ; ;;; ; ; ;
; ;; ; ; ;; ; ; ;; ; ;; ;; ;
; ;; ;; ;;;;;;; ; ; ; ;;;;;;; ; ;;;
; ;; ;; ; ; ; ; ; ; ;;;
; ;; ;; ; ; ; ; ; ; ;;
; ;; ;; ; ; ; ;; ; ; ; ; ;; ;
; ;; ;; ;;;; ;;;;;; ; ;;; ;;;; ; ;;;;
; ;
; ;
; ;
;; ignorable-char :: input-port -> bool
;;
;; Returns whether or not the next character to be read in should be ignored.
(define (ignorable-char? in [offset 0])
(not (member (peek-char in offset) (list A B C))))
;; parse-number :: input-port -> integer
;;
;; Reads a number from the input port, and returns the number. By whitespace
;; specification, this is a binary number with A = 0, B = 1, C = terminator.
(define (parse-number input-port)
;; turns '(1 0 0 1 1) -> 19
(define (to-number lst)
(letrec ([recur (λ (remaining accum pow)
(cond
[(null? remaining) accum]
[else (recur
(cdr remaining) (+ (* (car remaining) (arithmetic-shift 1 pow)) accum) (add1 pow))]))])
(recur (reverse lst) 0 0)))
(to-number (binary-sequence-helper input-port)))
(define (parse-string input-port)
;; turns '(0 1 1 0 1) -> '|01101|
(define (to-label lst)
(string->symbol (list->string (map (λ (x) (car (string->list (number->string x)))) lst))))
(to-label (binary-sequence-helper input-port)))
(define (binary-sequence-helper input-port)
(letrec ([recur (λ (accum)
(case (read-char input-port)
[(#\linefeed) (reverse accum)]
[(#\space) (recur (cons 0 accum))]
[(#\tab) (recur (cons 1 accum))]
;; It's an ignorable char, we don't care
[else (recur accum)]))])
(recur '())))
;; handle-suffix : Suffix * Input-Port -> WsExpr
;; where
;; Suffix = (instr) | (instr NUMBER) | (instr LABEL) | (instr mod)
;; WsExpr = A whitespace abstract syntax expression.
(define (handle-suffix suffix input-port)
(define (upcase-symbol? s)
(and (symbol? s) (andmap char-upper-case? (string->list (symbol->string s)))))
(define (remap-uppercase-symbol symb)
(cond
[(upcase-symbol? symb)
(match symb
['NUMBER (parse-number input-port)]
['LABEL (parse-string input-port)])]
[else symb]))
(map remap-uppercase-symbol suffix))
;
;
;
;
;
; ;; ;;
; ; ;
; ;;;;;; ;;; ;;; ;;;;;; ;;;
; ; ; ; ; ; ; ; ;
; ; ; ;; ; ; ;
; ; ;;;;;;; ;;; ; ;;;
; ; ; ;;; ; ;;;
; ; ; ;; ; ;;
; ;; ; ; ;; ; ;; ;; ;
; ;;; ;;;; ;;;; ;;; ;;;;
;
;
;
(check-equal?
(parse-number (open-input-string (list->string (list B B A A B C))))
25
"Parse number test for 25")
(check-equal?
(parse-number (open-input-string (list->string (list A C))))
0
"Parse number test for 0")
(check-equal?
(parse-number (open-input-string (list->string (list B C))))
1
"Parse number test for 1")
(check-equal?
(parse-string (open-input-string (list->string (list A B B A B C))))
(string->symbol "01101")
"Simple test")
(check-equal?
(parse-string (open-input-string (list->string (list B C))))
(string->symbol "1")
"Length = 1 test")
(check-equal?
(handle-suffix '(dup) (open-input-string (list->string (list B B B A C))))
'(dup)
"handle-suffix on unary operator")
(check-equal?
(handle-suffix '(push NUMBER) (open-input-string (list->string (list B B B A C))))
'(push 14)
"handle-suffix on number insert")
(check-equal?
(handle-suffix '(jump LABEL) (open-input-string (list->string (list B B B A C))))
'(jump |1110|)
"handle-suffix on label insert")
(check-equal?
(handle-suffix '(infix plus) (open-input-string (list->string (list B B B A C))))
'(infix plus)
"handle-suffix on non-consuming argument")
(define-syntax (parse-test stx)
(syntax-case stx ()
[(_ description (id1 id2 ...) expected)
#'(check-equal?
(parse #f (open-input-string (list->string (list id1 id2 ...))))
expected
description)]))
(parse-test
"simple push test"
(A A B A B C)
'((push 5)))
(parse-test
"ref test"
(A B A B A C)
'((ref 2)))
(parse-test
"slide test"
(A B C B A B A C)
'((slide 10)))
(parse-test
"dup test"
(A C A)
'((dup)))
(parse-test
"swap test"
(A C B)
'((swap)))
(parse-test
"discard test"
(A C C)
'((discard)))
(parse-test
"call test"
(C A B B B B A C)
'((call |1110|)))
(parse-test
"ws-if negative test"
(C B B A A B C)
'((if negative |001|)))
(parse-test
"end test"
(C C C)
'((end)))
(parse-test
"multiple commands"
(A A B A B C
A A B A B C
B A A A
C C C)
'((push 5)
(push 5)
(infix plus)
(end)))
(check-equal?
(ignorable-char? (open-input-string "a "))
#t
"Should ignore 'a'")
(check-equal?
(ignorable-char? (open-input-string "4 "))
#t
"Should ignore '4'")
(check-equal?
(ignorable-char? (open-input-string " 45"))
#f
"Should not ignore ' ")
(parse-test
"ignorable chars"
(#\p #\a #\b C #\l C #\o C)
'((end)))
(parse-test
"ignorable chars"
(#\p #\a #\b A #\l A #\o B A B #\9 C
A A B #\l #\a #\u A B #\r #\e #\n C
#\j #\u #\l #\y B #\6 #\t #\h A A A
C C #\l #\o #\l C)
'((push 5)
(push 5)
(infix plus)
(end)))