-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeta.fs
571 lines (517 loc) · 17.5 KB
/
meta.fs
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
variable meta-program'
variable meta-exp1'
variable meta-exp3'
variable meta-arg'
variable meta-output'
variable meta-exp2'
variable meta-exp1'
variable meta-comment'
variable meta-stat'
variable meta-support'
variable meta-declist'
variable meta-main'
\ input string | string length | token buffer | token length
variable s variable slen 0 value t variable tlen
\ Current location in string
variable p 0 p !
\ Flags
false value flagged? false value newlined?
\ Indentation level
variable indent
\ Line counter
variable lines 1 lines !
10 constant \n 9 constant \t 126 constant tilde
39 constant tick 34 constant dtick
: set-flag! ( -- ) true to flagged? ;
: unflag! ( -- ) false to flagged? ;
\ Run a parser, which is a pointer to a word.
: do-parse ( -- ) @ execute ;
: set-source! ( c-addr u -- ) slen ! s ! ;
: c-array-ref ( a b -- a[b] ) + c@ ;
: isspace ( c -- # ) bl over = over \t = or swap \n = or ;
: isdelim ( c -- # ) dup tick = swap tilde = or ;
: curr-char ( -- c ) s @ p @ c-array-ref ;
: advance ( -- ) 1 p +! ;
: inc-lines ( -- ) 1 lines +! ;
: skip-whitespace ( -- )
begin curr-char isspace while
curr-char \n = negate lines +! advance
repeat
;
: ?free ( p|0 -- )
?dup-if free if s" failed to free token buffer" exception throw then then
;
: ?free-token ( -- ) t ?free ;
: realloc-token ( -- )
tlen @ 1+ allocate if ." failed to allocate memory for token" then
to t
;
: nul-terminate-token ( -- ) 0 t tlen @ + c! ;
: copy-token-from-string ( sp -- ) s @ + t tlen @ cmove ;
: write-token ( sp -- ) nul-terminate-token copy-token-from-string ;
: calc-token-length ( sp -- sp ) p @ over - tlen ! ;
\ Make a token up to char sp.
: make-token ( sp -- ) ?free-token calc-token-length realloc-token write-token ;
\ Emit a character in a string, possibly quoted.
: emit-string-char ( c -- )
case
\n of .\" \\n" endof
dtick of .\" \\\"" endof
tick of .\" \\\'" endof
[char] \ of .\" \\\\" endof
\ Otherwise, print the character.
dup emit
endcase
;
\ Current character from the token buffer.
: tok-char ( -- c ) t c@ ;
: emit-string ( -- )
tok-char
tlen @ 1 do
t i c-array-ref 2dup
= if 2drop leave then
emit-string-char
loop
dtick emit
;
: emit-token ( -- ) tok-char isdelim if emit-string else t tlen @ type then ;
: print-indent ( -- ) indent @ spaces ;
: mtype ( c-addr u -- ) newlined? if print-indent then type 0 to newlined? ;
: emit-newline 1 to newlined? cr ;
: read-literal ( c-addr u -- )
{ length }
p @ 0 { l e i }
skip-whitespace
length 0 do
curr-char 0<>
l i c-array-ref 0<>
and
curr-char l i c-array-ref =
and
if
advance
i 1+ to i
else
leave
then
loop
i length = if
set-flag!
e make-token
else
e p ! unflag!
then
;
: isupper ( c -- # ) [char] A [char] Z 1+ within ;
: islower ( c -- # ) [char] a [char] z 1+ within ;
: isalpha ( c -- # ) dup isupper swap islower or ;
: isdigit ( c -- # ) [char] 0 [char] 9 1+ within ;
: isalnum ( c -- # ) dup isalpha swap isdigit or ;
: advance-while-alnum ( -- ) begin curr-char isalnum while advance repeat ;
: advance-while-digit ( -- ) begin curr-char isdigit while advance repeat ;
: read-id ( -- )
skip-whitespace
p @
curr-char isalpha if
advance set-flag!
else
unflag! drop exit
then
advance-while-alnum make-token
;
: read-number ( -- )
skip-whitespace
p @
\ Possibly with a leading dash.
curr-char [char] - = if advance then
curr-char isdigit if
advance set-flag!
advance-while-digit make-token
else
drop unflag! exit
then
;
\ Advance the pointer until the next occurence of c.
: advance-while-<> ( c -- )
begin dup curr-char <> while
curr-char \n = if inc-lines then
advance
repeat
;
: read-string ( -- )
skip-whitespace
p @
curr-char isdelim if
curr-char advance advance-while-<>
curr-char = if
advance set-flag! make-token
else
\ If we hit the end of the file, backtrack.
curr-char 0= if p ! then
then
else
drop unflag!
then
;
: maybe-error
flagged? invert if ( -- )
." Error in line " lines ? ." at token '" t tlen @ type ." ' "
." at character " p @ . cr
s" Parse error" exception throw
then
;
\ File ID in | File ID out
0 value fd-in 0 value fd-out
: open-input ( addr u -- ) r/o open-file throw to fd-in ;
: open-output ( addr u -- ) w/o create-file throw to fd-out ;
\ Size of each read.
1000 1000 * constant blk-size
\ Current size of the file buffer | Pointer to the file buffer.
0 value curr-buf-size 0 value file-buffer
blk-size allocate throw to file-buffer
\ Read a file, zero-delimited.
: do-read-file ( -- )
file-buffer blk-size fd-in read-file
if s" failed to read file" exception throw then
dup to curr-buf-size
file-buffer + 0 swap !
;
: close-input ( -- ) fd-in close-file throw ;
: close-output ( -- ) fd-out close-file throw ;
: set-file-as-input file-buffer curr-buf-size set-source! ;
: print-file file-buffer curr-buf-size type ;
: check-args
argc @ 3 <> if s" usage: meta <input> <output>" exception throw then ;
: process-input-arg ( -- )
next-arg 2dup ." Input file: " type cr open-input
do-read-file set-file-as-input close-input
;
: process-output-arg ( -- )
next-arg 2dup ." Output file: " type cr open-output ;
: process-args ( -- ) process-input-arg process-output-arg ;
: start-msg ( -- ) cr ." meta-yacc has started." cr ;
: assert-clean-stack ( -- )
depth if
s" stack not empty on exit" exception throw
else
cr ." Parsed without errors." cr
then
;
: run-meta-program find-name name>int fd-out outfile-execute ;
: main
start-msg check-args process-args
s" meta-program" run-meta-program
close-output assert-clean-stack bye
;
meta-arg'
: meta-arg
1 0 do
s\" *" read-literal
flagged? if
s\" emit-token" mtype
emit-newline
then
flagged? if leave then
read-string
flagged? if
s\" s\\\" " mtype
emit-token
s\" mtype" mtype
emit-newline
then
loop
; latestxt swap !
meta-output'
: meta-output
1 0 do
1 0 do
s\" {" read-literal
flagged? if
0 0 do
meta-arg' do-parse
flagged? invert if leave then loop
set-flag!
maybe-error
s\" }" read-literal
maybe-error
s\" emit-newline" mtype
emit-newline
then
flagged? if leave then
s\" <" read-literal
flagged? if
0 0 do
meta-arg' do-parse
flagged? invert if leave then loop
set-flag!
maybe-error
s\" >" read-literal
maybe-error
then
loop
flagged? if
then
loop
; latestxt swap !
meta-exp3'
: meta-exp3
1 0 do
read-id
flagged? if
s\" meta-" mtype
emit-token
s\" \' do-parse" mtype
emit-newline
then
flagged? if leave then
read-string
flagged? if
s\" s\\\" " mtype
emit-token
s\" read-literal" mtype
emit-newline
then
flagged? if leave then
s\" .id" read-literal
flagged? if
s\" read-id" mtype
emit-newline
then
flagged? if leave then
s\" .number" read-literal
flagged? if
s\" read-number" mtype
emit-newline
then
flagged? if leave then
s\" .string" read-literal
flagged? if
s\" read-string" mtype
emit-newline
then
flagged? if leave then
s\" .lm+" read-literal
flagged? if
s\" 2 indent +!" mtype
emit-newline
then
flagged? if leave then
s\" .lm-" read-literal
flagged? if
s\" -2 indent +!" mtype
emit-newline
then
flagged? if leave then
s\" (" read-literal
flagged? if
meta-exp1' do-parse
maybe-error
s\" )" read-literal
maybe-error
then
flagged? if leave then
s\" .e" read-literal
flagged? if
s\" set-flag!" mtype
emit-newline
then
flagged? if leave then
s\" $" read-literal
flagged? if
s\" 0 0 do" mtype
emit-newline
2 indent +!
maybe-error
meta-exp3' do-parse
maybe-error
-2 indent +!
maybe-error
s\" flagged? invert if leave then loop" mtype
emit-newline
s\" set-flag!" mtype
emit-newline
then
loop
; latestxt swap !
meta-exp2'
: meta-exp2
1 0 do
1 0 do
meta-exp3' do-parse
flagged? if
s\" flagged? if" mtype
emit-newline
then
flagged? if leave then
meta-output' do-parse
flagged? if
s\" true if" mtype
emit-newline
then
loop
flagged? if
2 indent +!
maybe-error
0 0 do
1 0 do
meta-exp3' do-parse
flagged? if
s\" maybe-error" mtype
emit-newline
then
flagged? if leave then
meta-output' do-parse
flagged? if
then
loop
flagged? invert if leave then loop
set-flag!
maybe-error
-2 indent +!
maybe-error
s\" then" mtype
emit-newline
then
loop
; latestxt swap !
meta-exp1'
: meta-exp1
1 0 do
s\" 1 0 do" mtype
emit-newline
true if
2 indent +!
maybe-error
meta-exp2' do-parse
maybe-error
0 0 do
1 0 do
s\" |" read-literal
flagged? if
s\" flagged? if leave then" mtype
emit-newline
meta-exp2' do-parse
maybe-error
then
loop
flagged? invert if leave then loop
set-flag!
maybe-error
-2 indent +!
maybe-error
s\" loop" mtype
emit-newline
then
loop
; latestxt swap !
meta-comment'
: meta-comment
1 0 do
s\" [" read-literal
flagged? if
read-string
maybe-error
s\" ]" read-literal
maybe-error
then
loop
; latestxt swap !
meta-stat'
: meta-stat
1 0 do
read-id
flagged? if
s\" meta-" mtype
emit-token
s\" \'" mtype
emit-newline
s\" : meta-" mtype
emit-token
emit-newline
2 indent +!
maybe-error
s\" =" read-literal
maybe-error
meta-exp1' do-parse
maybe-error
s\" ;" read-literal
maybe-error
-2 indent +!
maybe-error
s\" ; latestxt swap ! " mtype
emit-newline
then
flagged? if leave then
meta-comment' do-parse
flagged? if
then
loop
; latestxt swap !
meta-support'
: meta-support
1 0 do
s\" \n\\ input string | string length | token buffer | token length\nvariable s variable slen 0 value t variable tlen\n\n\\ Current location in string\nvariable p 0 p !\n\n\\ Flags\nfalse value flagged? false value newlined?\n\n\\ Indentation level\nvariable indent\n\n\\ Line counter\nvariable lines 1 lines !\n\n10 constant \\n 9 constant \\t 126 constant tilde\n39 constant tick 34 constant dtick\n\n: set-flag! ( -- ) true to flagged? ;\n: unflag! ( -- ) false to flagged? ;\n\n\\ Run a parser, which is a pointer to a word.\n: do-parse ( -- ) @ execute ;\n\n: set-source! ( c-addr u -- ) slen ! s ! ;\n: c-array-ref ( a b -- a[b] ) + c@ ;\n: isspace ( c -- # ) bl over = over \\t = or swap \\n = or ;\n\n: isdelim ( c -- # ) dup tick = swap tilde = or ;\n: curr-char ( -- c ) s @ p @ c-array-ref ;\n\n: advance ( -- ) 1 p +! ;\n: inc-lines ( -- ) 1 lines +! ;\n\n: skip-whitespace ( -- )\n begin curr-char isspace while\n curr-char \\n = negate lines +! advance\n repeat\n;\n\n: ?free ( p|0 -- )\n ?dup-if free if s\" failed to free token buffer\" exception throw then then\n;\n: ?free-token ( -- ) t ?free ;\n\n: realloc-token ( -- )\n tlen @ 1+ allocate if .\" failed to allocate memory for token\" then\n to t\n;\n\n: nul-terminate-token ( -- ) 0 t tlen @ + c! ;\n: copy-token-from-string ( sp -- ) s @ + t tlen @ cmove ;\n\n: write-token ( sp -- ) nul-terminate-token copy-token-from-string ;\n: calc-token-length ( sp -- sp ) p @ over - tlen ! ;\n\n\\ Make a token up to char sp.\n: make-token ( sp -- ) ?free-token calc-token-length realloc-token write-token ;\n\n\\ Emit a character in a string, possibly quoted.\n: emit-string-char ( c -- )\n case\n \\n of .\\\" \\\\n\" endof\n dtick of .\\\" \\\\\\\"\" endof\n tick of .\\\" \\\\\\\'\" endof\n [char] \\ of .\\\" \\\\\\\\\" endof\n \\ Otherwise, print the character.\n dup emit\n endcase\n;\n\n\\ Current character from the token buffer.\n: tok-char ( -- c ) t c@ ;\n\n: emit-string ( -- )\n tok-char\n tlen @ 1 do\n t i c-array-ref 2dup\n = if 2drop leave then\n emit-string-char\n loop\n dtick emit\n;\n\n: emit-token ( -- ) tok-char isdelim if emit-string else t tlen @ type then ;\n\n\n: print-indent ( -- ) indent @ spaces ;\n: mtype ( c-addr u -- ) newlined? if print-indent then type 0 to newlined? ;\n: emit-newline 1 to newlined? cr ;\n\n: read-literal ( c-addr u -- )\n { length }\n p @ 0 { l e i }\n skip-whitespace\n\n length 0 do\n curr-char 0<>\n l i c-array-ref 0<>\n and\n curr-char l i c-array-ref =\n and\n if\n advance\n i 1+ to i\n else\n leave\n then\n loop\n\n i length = if\n set-flag!\n e make-token\n else\n e p ! unflag!\n then\n;\n\n: isupper ( c -- # ) [char] A [char] Z 1+ within ;\n: islower ( c -- # ) [char] a [char] z 1+ within ;\n: isalpha ( c -- # ) dup isupper swap islower or ;\n\n: isdigit ( c -- # ) [char] 0 [char] 9 1+ within ;\n: isalnum ( c -- # ) dup isalpha swap isdigit or ;\n\n: advance-while-alnum ( -- ) begin curr-char isalnum while advance repeat ;\n: advance-while-digit ( -- ) begin curr-char isdigit while advance repeat ;\n\n: read-id ( -- )\n skip-whitespace\n p @\n curr-char isalpha if\n advance set-flag!\n else\n unflag! drop exit\n then\n\n advance-while-alnum make-token\n;\n\n\n: read-number ( -- )\n skip-whitespace\n p @\n \n \\ Possibly with a leading dash.\n curr-char [char] - = if advance then\n\n curr-char isdigit if\n advance set-flag!\n advance-while-digit make-token\n else\n drop unflag! exit\n then\n;\n\n\\ Advance the pointer until the next occurence of c.\n: advance-while-<> ( c -- )\n begin dup curr-char <> while\n curr-char \\n = if inc-lines then\n advance\n repeat\n;\n\n: read-string ( -- )\n skip-whitespace\n p @\n \n curr-char isdelim if\n curr-char advance advance-while-<>\n \n curr-char = if\n advance set-flag! make-token\n else\n \\ If we hit the end of the file, backtrack.\n curr-char 0= if p ! then\n then\n else\n drop unflag!\n then\n;\n\n: maybe-error\n flagged? invert if ( -- )\n .\" Error in line \" lines ? .\" at token \'\" t tlen @ type .\" \' \"\n .\" at character \" p @ . cr\n s\" Parse error\" exception throw\n then\n;\n\n\\ File ID in | File ID out\n0 value fd-in 0 value fd-out\n\n: open-input ( addr u -- ) r/o open-file throw to fd-in ;\n: open-output ( addr u -- ) w/o create-file throw to fd-out ;\n\n\\ Size of each read.\n1000 1000 * constant blk-size\n\n\\ Current size of the file buffer | Pointer to the file buffer.\n0 value curr-buf-size 0 value file-buffer\n\nblk-size allocate throw to file-buffer\n\n\\ Read a file, zero-delimited.\n: do-read-file ( -- )\n file-buffer blk-size fd-in read-file\n if s\" failed to read file\" exception throw then\n dup to curr-buf-size\n file-buffer + 0 swap !\n;\n\n: close-input ( -- ) fd-in close-file throw ;\n: close-output ( -- ) fd-out close-file throw ;\n\n: set-file-as-input file-buffer curr-buf-size set-source! ;\n: print-file file-buffer curr-buf-size type ;\n" mtype
emit-newline
true if
then
loop
; latestxt swap !
meta-declist'
: meta-declist
1 0 do
s\" [" read-literal
flagged? if
0 0 do
1 0 do
read-id
flagged? if
s\" variable meta-" mtype
emit-token
s\" \'" mtype
emit-newline
then
loop
flagged? invert if leave then loop
set-flag!
maybe-error
s\" ]" read-literal
maybe-error
then
loop
; latestxt swap !
meta-main'
: meta-main
1 0 do
read-id
flagged? if
s\" \n: check-args\n argc @ 3 <> if s\" usage: meta <input> <output>\" exception throw then ;\n\n: process-input-arg ( -- )\n next-arg 2dup .\" Input file: \" type cr open-input\n do-read-file set-file-as-input close-input\n;\n\n: process-output-arg ( -- )\n next-arg 2dup .\" Output file: \" type cr open-output ;\n\n: process-args ( -- ) process-input-arg process-output-arg ;\n: start-msg ( -- ) cr .\" meta-yacc has started.\" cr ;\n: assert-clean-stack ( -- )\n depth if\n s\" stack not empty on exit\" exception throw\n else\n cr .\" Parsed without errors.\" cr\n then\n;\n\n: run-meta-program find-name name>int fd-out outfile-execute ;\n: main\n start-msg check-args process-args\n s\" meta-" mtype
emit-token
s\" \" run-meta-program\n close-output assert-clean-stack bye\n;" mtype
emit-newline
then
loop
; latestxt swap !
meta-program'
: meta-program
1 0 do
s\" .syntax" read-literal
flagged? if
meta-declist' do-parse
maybe-error
meta-support' do-parse
maybe-error
meta-main' do-parse
maybe-error
0 0 do
meta-stat' do-parse
flagged? invert if leave then loop
set-flag!
maybe-error
s\" .end" read-literal
maybe-error
s\" main" mtype
emit-newline
then
loop
; latestxt swap !
main