-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfloop.fs
920 lines (866 loc) · 18 KB
/
floop.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
variable meta-arglist'
variable meta-arglistp'
variable meta-assignst'
variable meta-block'
variable meta-floop'
variable meta-breakst'
variable meta-callarglist'
variable meta-callarglistp'
variable meta-callexpr'
variable meta-callst'
variable meta-compare'
variable meta-expr'
variable meta-factor'
variable meta-foreverst'
variable meta-ifst'
variable meta-intst'
variable meta-loopst'
variable meta-pmain'
variable meta-printlnst'
variable meta-printst'
variable meta-proc'
variable meta-continuest'
variable meta-returnst'
variable meta-st'
variable meta-term'
\ 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 -- # )
dup dup
bl =
swap
\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 = if inc-lines then
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-floop" run-meta-program
close-output assert-clean-stack bye
;
meta-arglistp'
: meta-arglistp
1 0 do
s\" ," read-literal
flagged? if
read-id
maybe-error
s\" , int " mtype
emit-token
then
loop
; latestxt swap !
meta-arglist'
: meta-arglist
1 0 do
s\" (" read-literal
flagged? if
s\" (int " mtype
read-id
maybe-error
emit-token
0 0 do
1 0 do
meta-arglistp' do-parse
flagged? if
then
loop
flagged? invert if leave then loop
set-flag!
maybe-error
s\" )" read-literal
maybe-error
s\" :" read-literal
maybe-error
s\" )" mtype
then
loop
; latestxt swap !
meta-intst'
: meta-intst
1 0 do
s\" int" read-literal
flagged? if
read-id
maybe-error
s\" int " mtype
emit-token
s\" = 0;" mtype
emit-newline
then
loop
; latestxt swap !
meta-assignst'
: meta-assignst
1 0 do
read-id
flagged? if
s\" " mtype
emit-token
s\" = " mtype
s\" <-" read-literal
maybe-error
meta-expr' do-parse
maybe-error
s\" ;" mtype
emit-newline
then
loop
; latestxt swap !
meta-printst'
: meta-printst
1 0 do
s\" print" read-literal
flagged? if
1 0 do
read-id
flagged? if
s\" printf(\"%d \"," mtype
emit-token
s\" );" mtype
emit-newline
then
flagged? if leave then
read-string
flagged? if
s\" printf(\"" mtype
emit-token
s\" );" mtype
emit-newline
then
loop
maybe-error
then
loop
; latestxt swap !
meta-printlnst'
: meta-printlnst
1 0 do
s\" println" read-literal
flagged? if
1 0 do
read-id
flagged? if
s\" printf(\"%d\\n\"," mtype
emit-token
s\" );" mtype
emit-newline
then
flagged? if leave then
read-string
flagged? if
s\" puts(\"" mtype
emit-token
s\" );" mtype
emit-newline
then
loop
maybe-error
then
loop
; latestxt swap !
meta-ifst'
: meta-ifst
1 0 do
s\" if" read-literal
flagged? if
s\" " mtype
s\" if (" mtype
meta-expr' do-parse
maybe-error
s\" ) {" mtype
emit-newline
2 indent +!
maybe-error
s\" ," read-literal
maybe-error
s\" then" read-literal
maybe-error
s\" :" read-literal
maybe-error
meta-st' do-parse
maybe-error
s\" ;" read-literal
maybe-error
-2 indent +!
maybe-error
s\" } else { " mtype
emit-newline
2 indent +!
maybe-error
meta-st' do-parse
maybe-error
-2 indent +!
maybe-error
s\" }" mtype
emit-newline
then
loop
; latestxt swap !
meta-continuest'
: meta-continuest
1 0 do
s\" continue" read-literal
flagged? if
s\" continue;" mtype
emit-newline
then
loop
; latestxt swap !
meta-breakst'
: meta-breakst
1 0 do
s\" break" read-literal
flagged? if
s\" f = 0;" mtype
emit-newline
then
loop
; latestxt swap !
meta-loopst'
: meta-loopst
1 0 do
s\" loop" read-literal
flagged? if
s\" at" read-literal
maybe-error
s\" most" read-literal
maybe-error
s\" " mtype
s\" for(int i = 0, f = 1; i < " mtype
meta-expr' do-parse
maybe-error
s\" && f; i++) {" mtype
emit-newline
2 indent +!
maybe-error
s\" times" read-literal
maybe-error
s\" :" read-literal
maybe-error
meta-block' do-parse
maybe-error
-2 indent +!
maybe-error
s\" }" mtype
emit-newline
then
loop
; latestxt swap !
meta-foreverst'
: meta-foreverst
1 0 do
s\" forever" read-literal
flagged? if
s\" :" read-literal
maybe-error
s\" {" mtype
emit-newline
2 indent +!
maybe-error
s\" int f = 1;" mtype
emit-newline
s\" " mtype
s\" while(1 && f)" mtype
s\" {" mtype
emit-newline
2 indent +!
maybe-error
meta-block' do-parse
maybe-error
-2 indent +!
maybe-error
s\" }" mtype
emit-newline
-2 indent +!
maybe-error
s\" }" mtype
emit-newline
then
loop
; latestxt swap !
meta-st'
: meta-st
1 0 do
meta-printlnst' do-parse
flagged? if
then
flagged? if leave then
meta-printst' do-parse
flagged? if
then
flagged? if leave then
meta-ifst' do-parse
flagged? if
then
flagged? if leave then
meta-loopst' do-parse
flagged? if
then
flagged? if leave then
meta-foreverst' do-parse
flagged? if
then
flagged? if leave then
meta-continuest' do-parse
flagged? if
then
flagged? if leave then
meta-breakst' do-parse
flagged? if
then
flagged? if leave then
meta-block' do-parse
flagged? if
then
flagged? if leave then
meta-returnst' do-parse
flagged? if
then
flagged? if leave then
meta-callst' do-parse
flagged? if
then
flagged? if leave then
meta-assignst' do-parse
flagged? if
then
loop
; latestxt swap !
meta-block'
: meta-block
1 0 do
s\" begin" read-literal
flagged? if
0 0 do
1 0 do
meta-intst' do-parse
flagged? if
s\" ;" read-literal
maybe-error
then
loop
flagged? invert if leave then loop
set-flag!
maybe-error
s\" do {" mtype
emit-newline
2 indent +!
maybe-error
meta-st' do-parse
maybe-error
0 0 do
1 0 do
s\" ;" read-literal
flagged? if
meta-st' do-parse
maybe-error
then
loop
flagged? invert if leave then loop
set-flag!
maybe-error
-2 indent +!
maybe-error
s\" } while (0);" mtype
emit-newline
s\" end" read-literal
maybe-error
then
loop
; latestxt swap !
meta-proc'
: meta-proc
1 0 do
s\" def" read-literal
flagged? if
read-id
maybe-error
s\" int " mtype
emit-token
meta-arglist' do-parse
maybe-error
s\" {" mtype
emit-newline
2 indent +!
maybe-error
0 0 do
1 0 do
read-string
flagged? if
then
loop
flagged? invert if leave then loop
set-flag!
maybe-error
meta-block' do-parse
maybe-error
-2 indent +!
maybe-error
s\" }" mtype
emit-newline
then
loop
; latestxt swap !
meta-pmain'
: meta-pmain
1 0 do
s\" main" read-literal
flagged? if
s\" int main(void) {" mtype
emit-newline
2 indent +!
maybe-error
meta-block' do-parse
maybe-error
-2 indent +!
maybe-error
s\" }" mtype
emit-newline
then
loop
; latestxt swap !
meta-expr'
: meta-expr
1 0 do
meta-factor' do-parse
flagged? if
0 0 do
1 0 do
1 0 do
s\" +" read-literal
flagged? if
s\" + " mtype
meta-factor' do-parse
maybe-error
then
loop
flagged? if
then
flagged? if leave then
1 0 do
s\" -" read-literal
flagged? if
s\" - " mtype
meta-factor' do-parse
maybe-error
then
loop
flagged? if
then
loop
flagged? invert if leave then loop
set-flag!
maybe-error
then
loop
; latestxt swap !
meta-term'
: meta-term
1 0 do
meta-callexpr' do-parse
flagged? if
then
flagged? if leave then
1 0 do
read-id
flagged? if
then
flagged? if leave then
read-number
flagged? if
then
loop
flagged? if
emit-token
then
flagged? if leave then
s\" (" read-literal
flagged? if
s\" (" mtype
meta-expr' do-parse
maybe-error
s\" )" read-literal
maybe-error
s\" )" mtype
then
loop
; latestxt swap !
meta-compare'
: meta-compare
1 0 do
meta-term' do-parse
flagged? if
0 0 do
1 0 do
1 0 do
s\" <" read-literal
flagged? if
s\" < " mtype
meta-term' do-parse
maybe-error
then
loop
flagged? if
then
flagged? if leave then
1 0 do
s\" >" read-literal
flagged? if
s\" > " mtype
meta-term' do-parse
maybe-error
then
loop
flagged? if
then
flagged? if leave then
1 0 do
s\" =" read-literal
flagged? if
s\" == " mtype
meta-term' do-parse
maybe-error
then
loop
flagged? if
then
flagged? if leave then
1 0 do
s\" >=" read-literal
flagged? if
s\" >=" mtype
meta-term' do-parse
maybe-error
then
loop
flagged? if
then
flagged? if leave then
1 0 do
s\" <=" read-literal
flagged? if
s\" <=" mtype
meta-term' do-parse
maybe-error
then
loop
flagged? if
then
loop
flagged? invert if leave then loop
set-flag!
maybe-error
then
loop
; latestxt swap !
meta-factor'
: meta-factor
1 0 do
meta-compare' do-parse
flagged? if
0 0 do
1 0 do
s\" *" read-literal
flagged? if
s\" * " mtype
meta-compare' do-parse
maybe-error
then
loop
flagged? invert if leave then loop
set-flag!
maybe-error
then
loop
; latestxt swap !
meta-callarglistp'
: meta-callarglistp
1 0 do
s\" ," read-literal
flagged? if
s\" , " mtype
meta-expr' do-parse
maybe-error
then
loop
; latestxt swap !
meta-callarglist'
: meta-callarglist
1 0 do
s\" (" read-literal
flagged? if
s\" (" mtype
meta-expr' do-parse
maybe-error
0 0 do
1 0 do
meta-callarglistp' do-parse
flagged? if
then
loop
flagged? invert if leave then loop
set-flag!
maybe-error
s\" )" read-literal
maybe-error
s\" )" mtype
then
loop
; latestxt swap !
meta-callst'
: meta-callst
1 0 do
s\" do" read-literal
flagged? if
read-id
maybe-error
s\" " mtype
emit-token
meta-callarglist' do-parse
maybe-error
s\" ;" mtype
emit-newline
then
loop
; latestxt swap !
meta-returnst'
: meta-returnst
1 0 do
s\" return" read-literal
flagged? if
s\" " mtype
s\" return " mtype
meta-expr' do-parse
maybe-error
s\" ;" mtype
emit-newline
then
loop
; latestxt swap !
meta-callexpr'
: meta-callexpr
1 0 do
s\" do" read-literal
flagged? if
read-id
maybe-error
s\" " mtype
emit-token
meta-callarglist' do-parse
maybe-error
then
loop
; latestxt swap !
meta-floop'
: meta-floop
1 0 do
s\" #include <stdio.h>\n#include <stdlib.h>\n" mtype
emit-newline
true if
0 0 do
1 0 do
meta-proc' do-parse
flagged? if
then
flagged? if leave then
read-string
flagged? if
then
loop
flagged? invert if leave then loop
set-flag!
maybe-error
meta-pmain' do-parse
maybe-error
then
loop
; latestxt swap !
main