-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathc4.sh
executable file
·1839 lines (1806 loc) · 54.1 KB
/
c4.sh
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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
set -e -f
LC_ALL=C
: $((p = len = c = b = 0))
_memset() { # b: $2, c: $3, len: $4
set $@ $b $c $len $p
b=$2
c=$3
len=$4
p=$b
while [ $(((len -= 1) + 1)) != 0 ]; do
: $((_$(((p += 1) - 1)) = c))
done
: $(($1 = b))
: $((__tmp = $1)) $((b = $5)) $((c = $6)) $((len = $7)) $((p = $8)) $(($1 = __tmp))
}
: $((r = l = n = vr = vl = 0))
_memcmp() { # vl: $2, vr: $3, n: $4
set $@ $vl $vr $n $l $r
vl=$2
vr=$3
n=$4
l=$vl
r=$vr
while [ $n != 0 ] && [ $((_$l)) = $((_$r)) ]; do
: $((n -= 1))
: $((l += 1))
: $((r += 1))
done
: $(($1 = n ? ((_$l & 255) - (_$r & 255)): 0))
: $((__tmp = $1)) $((vl = $5)) $((vr = $6)) $((n = $7)) $((l = $8)) $((r = $9)) $(($1 = __tmp))
}
_p=0
_lp=0
_data=0
_e=0
_le=0
_id=0
_sym=0
_tk=0
_ival=0
_ty=0
_loc=0
_line=0
_src=0
_debug=0
# Enum declaration
readonly _Num=128
readonly _Fun=129
readonly _Sys=130
readonly _Glo=131
readonly _Loc=132
readonly _Id=133
readonly _Char=134
readonly _Else=135
readonly _Enum=136
readonly _If=137
readonly _Int=138
readonly _Return=139
readonly _Sizeof=140
readonly _While=141
readonly _Assign=142
readonly _Cond=143
readonly _Lor=144
readonly _Lan=145
readonly _Or=146
readonly _Xor=147
readonly _And=148
readonly _Eq=149
readonly _Ne=150
readonly _Lt=151
readonly _Gt=152
readonly _Le=153
readonly _Ge=154
readonly _Shl=155
readonly _Shr=156
readonly _Add=157
readonly _Sub=158
readonly _Mul=159
readonly _Div=160
readonly _Mod=161
readonly _Inc=162
readonly _Dec=163
readonly _Brak=164
# Enum declaration
readonly _LEA=0
readonly _IMM=1
readonly _JMP=2
readonly _JSR=3
readonly _BZ=4
readonly _BNZ=5
readonly _ENT=6
readonly _ADJ=7
readonly _LEV=8
readonly _LI=9
readonly _LC=10
readonly _SI=11
readonly _SC=12
readonly _PSH=13
readonly _OR=14
readonly _XOR=15
readonly _AND=16
readonly _EQ=17
readonly _NE=18
readonly _LT=19
readonly _GT=20
readonly _LE=21
readonly _GE=22
readonly _SHL=23
readonly _SHR=24
readonly _ADD=25
readonly _SUB=26
readonly _MUL=27
readonly _DIV=28
readonly _MOD=29
readonly _OPEN=30
readonly _READ=31
readonly _CLOS=32
readonly _PRTF=33
readonly _MALC=34
readonly _FREE=35
readonly _MSET=36
readonly _MCMP=37
readonly _EXIT=38
# Enum declaration
readonly _CHAR=0
readonly _INT=1
readonly _PTR=2
# Enum declaration
readonly _Tk=0
readonly _Hash=1
readonly _Name=2
readonly _Class=3
readonly _Type=4
readonly _Val=5
readonly _HClass=6
readonly _HType=7
readonly _HVal=8
readonly _Idsz=9
: $((__t1 = pp = 0))
_next() {
set $@ $pp $__t1
while [ $((_tk = _$_p)) != 0 ]; do
: $((_p += 1))
if [ $_tk = $__NEWLINE__ ] ; then
if [ $_src != 0 ] ; then
printf "%d: (%d - %d = %d) %.*s" $_line $_p $_lp $((_p - _lp)) $((_p - _lp)) "$(_put_pstr __ $_lp)"
_lp=$_p
while [ $_le -lt $_e ]; do
defstr __str_0 "LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,OPEN,READ,CLOS,PRTF,MALC,FREE,MSET,MCMP,EXIT,"
printf "%8.4s" "$(_put_pstr __ $((__str_0 + _$((_le += 1)) * 5)))"
if [ $((_$_le)) -le $_ADJ ] ; then
printf " %d\n" $((_$((_le += 1))))
else
printf "\n"
fi
done
fi
: $((_line += 1))
elif [ $_tk = $__SHARP__ ] ; then
while [ $((_$_p)) != 0 ] && [ $((_$_p)) != $__NEWLINE__ ]; do
: $((_p += 1))
done
elif { [ $_tk -ge $__a__ ] && [ $_tk -le $__z__ ]; } || { [ $_tk -ge $__A__ ] && [ $_tk -le $__Z__ ]; } || [ $_tk = $__UNDERSCORE__ ] ; then
pp=$((_p - 1))
while { [ $((_$_p)) -ge $__a__ ] && [ $((_$_p)) -le $__z__ ]; } || { [ $((_$_p)) -ge $__A__ ] && [ $((_$_p)) -le $__Z__ ]; } || { [ $((_$_p)) -ge $__0__ ] && [ $((_$_p)) -le $__9__ ]; } || [ $((_$_p)) = $__UNDERSCORE__ ]; do
_tk=$(((_tk * 147) + _$(((_p += 1) - 1))))
done
_tk=$(((_tk << 6) + (_p - pp)))
_id=$_sym
while [ $((_$((_id + _Tk)))) != 0 ]; do
if [ $_tk = $((_$((_id + _Hash)))) ] && { _memcmp __t1 $((_$((_id + _Name)))) $pp $((_p - pp)); [ $((!__t1)) != 0 ]; } ; then
_tk=$((_$((_id + _Tk))))
: $((__tmp = $1)) $((pp = $2)) $((__t1 = $3)) $(($1 = __tmp))
return
fi
_id=$((_id + _Idsz))
done
: $((_$((_id + _Name)) = pp))
: $((_$((_id + _Hash)) = _tk))
: $((_tk = _$((_id + _Tk)) = _Id))
break
elif [ $_tk -ge $__0__ ] && [ $_tk -le $__9__ ] ; then
if [ $((_ival = _tk - __0__)) != 0 ] ; then
while [ $((_$_p)) -ge $__0__ ] && [ $((_$_p)) -le $__9__ ]; do
_ival=$((((_ival * 10) + _$(((_p += 1) - 1))) - __0__))
done
elif [ $((_$_p)) = $__x__ ] || [ $((_$_p)) = $__X__ ] ; then
while [ $((_tk = _$((_p += 1)))) != 0 ] && { { [ $_tk -ge $__0__ ] && [ $_tk -le $__9__ ]; } || { [ $_tk -ge $__a__ ] && [ $_tk -le $__f__ ]; } || { [ $_tk -ge $__A__ ] && [ $_tk -le $__F__ ]; }; }; do
_ival=$(((_ival * 16) + (_tk & 15) + ((_tk >= __A__) ? 9: 0)))
done
else
while [ $((_$_p)) -ge $__0__ ] && [ $((_$_p)) -le $__7__ ]; do
_ival=$((((_ival * 8) + _$(((_p += 1) - 1))) - __0__))
done
fi
_tk=$_Num
break
elif [ $_tk = $__SLASH__ ] ; then
if [ $((_$_p)) = $__SLASH__ ] ; then
: $((_p += 1))
while [ $((_$_p)) != 0 ] && [ $((_$_p)) != $__NEWLINE__ ]; do
: $((_p += 1))
done
else
_tk=$_Div
break
fi
elif [ $_tk = $__QUOTE__ ] || [ $_tk = $__DQUOTE__ ] ; then
pp=$_data
while [ $((_$_p)) != 0 ] && [ $((_$_p)) != $_tk ]; do
if [ $((_ival = _$(((_p += 1) - 1)))) = $__BACKSLASH__ ] ; then
if [ $((_ival = _$(((_p += 1) - 1)))) = $__n__ ] ; then
_ival=$__NEWLINE__
fi
fi
if [ $_tk = $__DQUOTE__ ] ; then
: $((_$(((_data += 1) - 1)) = _ival))
fi
done
: $((_p += 1))
if [ $_tk = $__DQUOTE__ ] ; then
_ival=$pp
else
_tk=$_Num
fi
break
elif [ $_tk = $__EQ__ ] ; then
if [ $((_$_p)) = $__EQ__ ] ; then
: $((_p += 1))
_tk=$_Eq
else
_tk=$_Assign
fi
break
elif [ $_tk = $__PLUS__ ] ; then
if [ $((_$_p)) = $__PLUS__ ] ; then
: $((_p += 1))
_tk=$_Inc
else
_tk=$_Add
fi
break
elif [ $_tk = $__MINUS__ ] ; then
if [ $((_$_p)) = $__MINUS__ ] ; then
: $((_p += 1))
_tk=$_Dec
else
_tk=$_Sub
fi
break
elif [ $_tk = $__EXCL__ ] ; then
if [ $((_$_p)) = $__EQ__ ] ; then
: $((_p += 1))
_tk=$_Ne
fi
break
elif [ $_tk = $__LT__ ] ; then
if [ $((_$_p)) = $__EQ__ ] ; then
: $((_p += 1))
_tk=$_Le
elif [ $((_$_p)) = $__LT__ ] ; then
: $((_p += 1))
_tk=$_Shl
else
_tk=$_Lt
fi
break
elif [ $_tk = $__GT__ ] ; then
if [ $((_$_p)) = $__EQ__ ] ; then
: $((_p += 1))
_tk=$_Ge
elif [ $((_$_p)) = $__GT__ ] ; then
: $((_p += 1))
_tk=$_Shr
else
_tk=$_Gt
fi
break
elif [ $_tk = $__BAR__ ] ; then
if [ $((_$_p)) = $__BAR__ ] ; then
: $((_p += 1))
_tk=$_Lor
else
_tk=$_Or
fi
break
elif [ $_tk = $__AMP__ ] ; then
if [ $((_$_p)) = $__AMP__ ] ; then
: $((_p += 1))
_tk=$_Lan
else
_tk=$_And
fi
break
elif [ $_tk = $__CARET__ ] ; then
_tk=$_Xor
break
elif [ $_tk = $__PERCENT__ ] ; then
_tk=$_Mod
break
elif [ $_tk = $__STAR__ ] ; then
_tk=$_Mul
break
elif [ $_tk = $__LBRACK__ ] ; then
_tk=$_Brak
break
elif [ $_tk = $__QUESTION__ ] ; then
_tk=$_Cond
break
elif [ $_tk = $__TILDE__ ] || [ $_tk = $__SEMICOLON__ ] || [ $_tk = $__LBRACE__ ] || [ $_tk = $__RBRACE__ ] || [ $_tk = $__LPAREN__ ] || [ $_tk = $__RPAREN__ ] || [ $_tk = $__RBRACK__ ] || [ $_tk = $__COMMA__ ] || [ $_tk = $__COLON__ ] ; then
break
fi
done
: $((__tmp = $1)) $((pp = $2)) $((__t1 = $3)) $(($1 = __tmp))
}
: $((d = t = lev = 0))
_expr() { # lev: $2
set $@ $lev $t $d
lev=$2
if [ $((!_tk)) != 0 ] ; then
printf "%d: unexpected eof in expression\n" $_line
exit -1
elif [ $_tk = $_Num ] ; then
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = _ival))
_next __
_ty=$_INT
elif [ $_tk = $__DQUOTE__ ] ; then
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = _ival))
_next __
while [ $_tk = $__DQUOTE__ ]; do
_next __
done
_data=$(((_data + 1) & -1))
_ty=$_PTR
elif [ $_tk = $_Sizeof ] ; then
_next __
if [ $_tk = $__LPAREN__ ] ; then
_next __
else
printf "%d: open paren expected in sizeof\n" $_line
exit -1
fi
_ty=$_INT
if [ $_tk = $_Int ] ; then
_next __
elif [ $_tk = $_Char ] ; then
_next __
_ty=$_CHAR
fi
while [ $_tk = $_Mul ]; do
_next __
_ty=$((_ty + _PTR))
done
if [ $_tk = $__RPAREN__ ] ; then
_next __
else
printf "%d: close paren expected in sizeof\n" $_line
exit -1
fi
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = (_ty == _CHAR) ? 1: 1))
_ty=$_INT
elif [ $_tk = $_Id ] ; then
d=$_id
_next __
if [ $_tk = $__LPAREN__ ] ; then
_next __
t=0
while [ $_tk != $__RPAREN__ ]; do
_expr __ $_Assign
: $((_$((_e += 1)) = _PSH))
: $((t += 1))
if [ $_tk = $__COMMA__ ] ; then
_next __
fi
done
_next __
if [ $((_$((d + _Class)))) = $_Sys ] ; then
: $((_$((_e += 1)) = _$((d + _Val))))
elif [ $((_$((d + _Class)))) = $_Fun ] ; then
: $((_$((_e += 1)) = _JSR))
: $((_$((_e += 1)) = _$((d + _Val))))
else
printf "%d: bad function call\n" $_line
exit -1
fi
if [ $t != 0 ] ; then
: $((_$((_e += 1)) = _ADJ))
: $((_$((_e += 1)) = t))
fi
_ty=$((_$((d + _Type))))
elif [ $((_$((d + _Class)))) = $_Num ] ; then
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = _$((d + _Val))))
_ty=$_INT
else
if [ $((_$((d + _Class)))) = $_Loc ] ; then
: $((_$((_e += 1)) = _LEA))
: $((_$((_e += 1)) = _loc - _$((d + _Val))))
elif [ $((_$((d + _Class)))) = $_Glo ] ; then
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = _$((d + _Val))))
else
printf "%d: undefined variable\n" $_line
exit -1
fi
: $((_$((_e += 1)) = ((_ty = _$((d + _Type))) == _CHAR) ? _LC: _LI))
fi
elif [ $_tk = $__LPAREN__ ] ; then
_next __
if [ $_tk = $_Int ] || [ $_tk = $_Char ] ; then
t=$(((_tk == _Int) ? _INT: _CHAR))
_next __
while [ $_tk = $_Mul ]; do
_next __
t=$((t + _PTR))
done
if [ $_tk = $__RPAREN__ ] ; then
_next __
else
printf "%d: bad cast\n" $_line
exit -1
fi
_expr __ $_Inc
_ty=$t
else
_expr __ $_Assign
if [ $_tk = $__RPAREN__ ] ; then
_next __
else
printf "%d: close paren expected\n" $_line
exit -1
fi
fi
elif [ $_tk = $_Mul ] ; then
_next __
_expr __ $_Inc
if [ $_ty -gt $_INT ] ; then
_ty=$((_ty - _PTR))
else
printf "%d: bad dereference\n" $_line
exit -1
fi
: $((_$((_e += 1)) = (_ty == _CHAR) ? _LC: _LI))
elif [ $_tk = $_And ] ; then
_next __
_expr __ $_Inc
if [ $((_$_e)) = $_LC ] || [ $((_$_e)) = $_LI ] ; then
: $((_e -= 1))
else
printf "%d: bad address-of\n" $_line
exit -1
fi
_ty=$((_ty + _PTR))
elif [ $_tk = $__EXCL__ ] ; then
_next __
_expr __ $_Inc
: $((_$((_e += 1)) = _PSH))
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = 0))
: $((_$((_e += 1)) = _EQ))
_ty=$_INT
elif [ $_tk = $__TILDE__ ] ; then
_next __
_expr __ $_Inc
: $((_$((_e += 1)) = _PSH))
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = -1))
: $((_$((_e += 1)) = _XOR))
_ty=$_INT
elif [ $_tk = $_Add ] ; then
_next __
_expr __ $_Inc
_ty=$_INT
elif [ $_tk = $_Sub ] ; then
_next __
: $((_$((_e += 1)) = _IMM))
if [ $_tk = $_Num ] ; then
: $((_$((_e += 1)) = -_ival))
_next __
else
: $((_$((_e += 1)) = -1))
: $((_$((_e += 1)) = _PSH))
_expr __ $_Inc
: $((_$((_e += 1)) = _MUL))
fi
_ty=$_INT
elif [ $_tk = $_Inc ] || [ $_tk = $_Dec ] ; then
t=$_tk
_next __
_expr __ $_Inc
if [ $((_$_e)) = $_LC ] ; then
: $((_$_e = _PSH))
: $((_$((_e += 1)) = _LC))
elif [ $((_$_e)) = $_LI ] ; then
: $((_$_e = _PSH))
: $((_$((_e += 1)) = _LI))
else
printf "%d: bad lvalue in pre-increment\n" $_line
exit -1
fi
: $((_$((_e += 1)) = _PSH))
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = (_ty > _PTR) ? 1: 1))
: $((_$((_e += 1)) = (t == _Inc) ? _ADD: _SUB))
: $((_$((_e += 1)) = (_ty == _CHAR) ? _SC: _SI))
else
printf "%d: bad expression\n" $_line
exit -1
fi
while [ $_tk -ge $lev ]; do
t=$_ty
if [ $_tk = $_Assign ] ; then
_next __
if [ $((_$_e)) = $_LC ] || [ $((_$_e)) = $_LI ] ; then
: $((_$_e = _PSH))
else
printf "%d: bad lvalue in assignment\n" $_line
exit -1
fi
_expr __ $_Assign
: $((_$((_e += 1)) = ((_ty = t) == _CHAR) ? _SC: _SI))
elif [ $_tk = $_Cond ] ; then
_next __
: $((_$((_e += 1)) = _BZ))
d=$((_e += 1))
_expr __ $_Assign
if [ $_tk = $__COLON__ ] ; then
_next __
else
printf "%d: conditional missing colon\n" $_line
exit -1
fi
: $((_$d = (_e + 3)))
: $((_$((_e += 1)) = _JMP))
d=$((_e += 1))
_expr __ $_Cond
: $((_$d = (_e + 1)))
elif [ $_tk = $_Lor ] ; then
_next __
: $((_$((_e += 1)) = _BNZ))
d=$((_e += 1))
_expr __ $_Lan
: $((_$d = (_e + 1)))
_ty=$_INT
elif [ $_tk = $_Lan ] ; then
_next __
: $((_$((_e += 1)) = _BZ))
d=$((_e += 1))
_expr __ $_Or
: $((_$d = (_e + 1)))
_ty=$_INT
elif [ $_tk = $_Or ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Xor
: $((_$((_e += 1)) = _OR))
_ty=$_INT
elif [ $_tk = $_Xor ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_And
: $((_$((_e += 1)) = _XOR))
_ty=$_INT
elif [ $_tk = $_And ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Eq
: $((_$((_e += 1)) = _AND))
_ty=$_INT
elif [ $_tk = $_Eq ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Lt
: $((_$((_e += 1)) = _EQ))
_ty=$_INT
elif [ $_tk = $_Ne ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Lt
: $((_$((_e += 1)) = _NE))
_ty=$_INT
elif [ $_tk = $_Lt ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Shl
: $((_$((_e += 1)) = _LT))
_ty=$_INT
elif [ $_tk = $_Gt ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Shl
: $((_$((_e += 1)) = _GT))
_ty=$_INT
elif [ $_tk = $_Le ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Shl
: $((_$((_e += 1)) = _LE))
_ty=$_INT
elif [ $_tk = $_Ge ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Shl
: $((_$((_e += 1)) = _GE))
_ty=$_INT
elif [ $_tk = $_Shl ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Add
: $((_$((_e += 1)) = _SHL))
_ty=$_INT
elif [ $_tk = $_Shr ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Add
: $((_$((_e += 1)) = _SHR))
_ty=$_INT
elif [ $_tk = $_Add ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Mul
if [ $((_ty = t)) -gt $_PTR ] ; then
: $((_$((_e += 1)) = _PSH))
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = 1))
: $((_$((_e += 1)) = _MUL))
fi
: $((_$((_e += 1)) = _ADD))
elif [ $_tk = $_Sub ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Mul
if [ $t -gt $_PTR ] && [ $t = $_ty ] ; then
: $((_$((_e += 1)) = _SUB))
: $((_$((_e += 1)) = _PSH))
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = 1))
: $((_$((_e += 1)) = _DIV))
_ty=$_INT
elif [ $((_ty = t)) -gt $_PTR ] ; then
: $((_$((_e += 1)) = _PSH))
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = 1))
: $((_$((_e += 1)) = _MUL))
: $((_$((_e += 1)) = _SUB))
else
: $((_$((_e += 1)) = _SUB))
fi
elif [ $_tk = $_Mul ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Inc
: $((_$((_e += 1)) = _MUL))
_ty=$_INT
elif [ $_tk = $_Div ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Inc
: $((_$((_e += 1)) = _DIV))
_ty=$_INT
elif [ $_tk = $_Mod ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Inc
: $((_$((_e += 1)) = _MOD))
_ty=$_INT
elif [ $_tk = $_Inc ] || [ $_tk = $_Dec ] ; then
if [ $((_$_e)) = $_LC ] ; then
: $((_$_e = _PSH))
: $((_$((_e += 1)) = _LC))
elif [ $((_$_e)) = $_LI ] ; then
: $((_$_e = _PSH))
: $((_$((_e += 1)) = _LI))
else
printf "%d: bad lvalue in post-increment\n" $_line
exit -1
fi
: $((_$((_e += 1)) = _PSH))
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = (_ty > _PTR) ? 1: 1))
: $((_$((_e += 1)) = (_tk == _Inc) ? _ADD: _SUB))
: $((_$((_e += 1)) = (_ty == _CHAR) ? _SC: _SI))
: $((_$((_e += 1)) = _PSH))
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = (_ty > _PTR) ? 1: 1))
: $((_$((_e += 1)) = (_tk == _Inc) ? _SUB: _ADD))
_next __
elif [ $_tk = $_Brak ] ; then
_next __
: $((_$((_e += 1)) = _PSH))
_expr __ $_Assign
if [ $_tk = $__RBRACK__ ] ; then
_next __
else
printf "%d: close bracket expected\n" $_line
exit -1
fi
if [ $t -gt $_PTR ] ; then
: $((_$((_e += 1)) = _PSH))
: $((_$((_e += 1)) = _IMM))
: $((_$((_e += 1)) = 1))
: $((_$((_e += 1)) = _MUL))
elif [ $t -lt $_PTR ] ; then
printf "%d: pointer type expected\n" $_line
exit -1
fi
: $((_$((_e += 1)) = _ADD))
: $((_$((_e += 1)) = ((_ty = t - _PTR) == _CHAR) ? _LC: _LI))
else
printf "%d: compiler error tk=%d\n" $_line $_tk
exit -1
fi
done
: $((__tmp = $1)) $((lev = $3)) $((t = $4)) $((d = $5)) $(($1 = __tmp))
}
: $((b = a = 0))
_stmt() {
set $@ $a $b
if [ $_tk = $_If ] ; then
_next __
if [ $_tk = $__LPAREN__ ] ; then
_next __
else
printf "%d: open paren expected\n" $_line
exit -1
fi
_expr __ $_Assign
if [ $_tk = $__RPAREN__ ] ; then
_next __
else
printf "%d: close paren expected\n" $_line
exit -1
fi
: $((_$((_e += 1)) = _BZ))
b=$((_e += 1))
_stmt __
if [ $_tk = $_Else ] ; then
: $((_$b = (_e + 3)))
: $((_$((_e += 1)) = _JMP))
b=$((_e += 1))
_next __
_stmt __
fi
: $((_$b = (_e + 1)))
elif [ $_tk = $_While ] ; then
_next __
a=$((_e + 1))
if [ $_tk = $__LPAREN__ ] ; then
_next __
else
printf "%d: open paren expected\n" $_line
exit -1
fi
_expr __ $_Assign
if [ $_tk = $__RPAREN__ ] ; then
_next __
else
printf "%d: close paren expected\n" $_line
exit -1
fi
: $((_$((_e += 1)) = _BZ))
b=$((_e += 1))
_stmt __
: $((_$((_e += 1)) = _JMP))
: $((_$((_e += 1)) = a))
: $((_$b = (_e + 1)))
elif [ $_tk = $_Return ] ; then
_next __
if [ $_tk != $__SEMICOLON__ ] ; then
_expr __ $_Assign
fi
: $((_$((_e += 1)) = _LEV))
if [ $_tk = $__SEMICOLON__ ] ; then
_next __
else
printf "%d: semicolon expected\n" $_line
exit -1
fi
elif [ $_tk = $__LBRACE__ ] ; then
_next __
while [ $_tk != $__RBRACE__ ]; do
_stmt __
done
_next __
elif [ $_tk = $__SEMICOLON__ ] ; then
_next __
else
_expr __ $_Assign
if [ $_tk = $__SEMICOLON__ ] ; then
_next __
else
printf "%d: semicolon expected\n" $_line
exit -1
fi
fi
: $((__tmp = $1)) $((a = $2)) $((b = $3)) $(($1 = __tmp))
}
: $((__t1 = t = i = cycle = a = bp = sp = pc = idmain = poolsz = ty = bt = fd = argv_ = argc = 0))
_main() { # argc: $2, argv: $3
set $@ $argc $argv_ $fd $bt $ty $poolsz $idmain $pc $sp $bp $a $cycle $i $t $__t1
argc=$2
argv_=$3
: $((argc -= 1))
: $((argv_ += 1))
if [ $argc -gt 0 ] && [ $((_$((_$argv_)))) = $__MINUS__ ] && [ $((_$((_$argv_ + 1)))) = $__s__ ] ; then
_src=1
: $((argc -= 1))
: $((argv_ += 1))
fi
if [ $argc -gt 0 ] && [ $((_$((_$argv_)))) = $__MINUS__ ] && [ $((_$((_$argv_ + 1)))) = $__d__ ] ; then
_debug=1
: $((argc -= 1))
: $((argv_ += 1))
fi
if [ $argc -lt 1 ] ; then
printf "usage: c4 [-s] [-d] file ...\n"
: $(($1 = -1))
: $((__tmp = $1)) $((argc = $4)) $((argv_ = $5)) $((fd = $6)) $((bt = $7)) $((ty = $8)) $((poolsz = $9)) $((idmain = ${10})) $((pc = ${11})) $((sp = ${12})) $((bp = ${13})) $((a = ${14})) $((cycle = ${15})) $((i = ${16})) $((t = ${17})) $((__t1 = ${18})) $(($1 = __tmp))
return
fi
if _open fd $((_$argv_)) 0; [ $fd -lt 0 ] ; then
printf "could not open("
_put_pstr __ $((_$argv_))
printf ")\n"
: $(($1 = -1))
: $((__tmp = $1)) $((argc = $4)) $((argv_ = $5)) $((fd = $6)) $((bt = $7)) $((ty = $8)) $((poolsz = $9)) $((idmain = ${10})) $((pc = ${11})) $((sp = ${12})) $((bp = ${13})) $((a = ${14})) $((cycle = ${15})) $((i = ${16})) $((t = ${17})) $((__t1 = ${18})) $(($1 = __tmp))
return
fi
poolsz=$((256 * 1024))
if _malloc _sym $poolsz; [ $((!_sym)) != 0 ] ; then
printf "could not malloc(%d) symbol area\n" $poolsz
: $(($1 = -1))
: $((__tmp = $1)) $((argc = $4)) $((argv_ = $5)) $((fd = $6)) $((bt = $7)) $((ty = $8)) $((poolsz = $9)) $((idmain = ${10})) $((pc = ${11})) $((sp = ${12})) $((bp = ${13})) $((a = ${14})) $((cycle = ${15})) $((i = ${16})) $((t = ${17})) $((__t1 = ${18})) $(($1 = __tmp))
return
fi
if _malloc _e $poolsz; [ $((!(_le = _e))) != 0 ] ; then
printf "could not malloc(%d) text area\n" $poolsz
: $(($1 = -1))
: $((__tmp = $1)) $((argc = $4)) $((argv_ = $5)) $((fd = $6)) $((bt = $7)) $((ty = $8)) $((poolsz = $9)) $((idmain = ${10})) $((pc = ${11})) $((sp = ${12})) $((bp = ${13})) $((a = ${14})) $((cycle = ${15})) $((i = ${16})) $((t = ${17})) $((__t1 = ${18})) $(($1 = __tmp))
return
fi
if _malloc _data $poolsz; [ $((!_data)) != 0 ] ; then
printf "could not malloc(%d) data area\n" $poolsz
: $(($1 = -1))
: $((__tmp = $1)) $((argc = $4)) $((argv_ = $5)) $((fd = $6)) $((bt = $7)) $((ty = $8)) $((poolsz = $9)) $((idmain = ${10})) $((pc = ${11})) $((sp = ${12})) $((bp = ${13})) $((a = ${14})) $((cycle = ${15})) $((i = ${16})) $((t = ${17})) $((__t1 = ${18})) $(($1 = __tmp))
return
fi
if _malloc sp $poolsz; [ $((!sp)) != 0 ] ; then
printf "could not malloc(%d) stack area\n" $poolsz
: $(($1 = -1))
: $((__tmp = $1)) $((argc = $4)) $((argv_ = $5)) $((fd = $6)) $((bt = $7)) $((ty = $8)) $((poolsz = $9)) $((idmain = ${10})) $((pc = ${11})) $((sp = ${12})) $((bp = ${13})) $((a = ${14})) $((cycle = ${15})) $((i = ${16})) $((t = ${17})) $((__t1 = ${18})) $(($1 = __tmp))
return
fi
defstr __str_1 "char else enum if int return sizeof while open read close printf malloc free memset memcmp exit void main"
_p=$__str_1
i=$_Char
while [ $i -le $_While ]; do
_next __
: $((_$((_id + _Tk)) = (i += 1) - 1))
done
i=$_OPEN
while [ $i -le $_EXIT ]; do
_next __
: $((_$((_id + _Class)) = _Sys))
: $((_$((_id + _Type)) = _INT))
: $((_$((_id + _Val)) = (i += 1) - 1))
done
_next __
: $((_$((_id + _Tk)) = _Char))
_next __
idmain=$_id
if _malloc _p $poolsz; [ $((!(_lp = _p))) != 0 ] ; then
printf "could not malloc(%d) source area\n" $poolsz
: $(($1 = -1))
: $((__tmp = $1)) $((argc = $4)) $((argv_ = $5)) $((fd = $6)) $((bt = $7)) $((ty = $8)) $((poolsz = $9)) $((idmain = ${10})) $((pc = ${11})) $((sp = ${12})) $((bp = ${13})) $((a = ${14})) $((cycle = ${15})) $((i = ${16})) $((t = ${17})) $((__t1 = ${18})) $(($1 = __tmp))
return
fi
if _read i $fd $_p $((poolsz - 1)); [ $i -le 0 ] ; then
printf "read() returned %d\n" $i
: $(($1 = -1))
: $((__tmp = $1)) $((argc = $4)) $((argv_ = $5)) $((fd = $6)) $((bt = $7)) $((ty = $8)) $((poolsz = $9)) $((idmain = ${10})) $((pc = ${11})) $((sp = ${12})) $((bp = ${13})) $((a = ${14})) $((cycle = ${15})) $((i = ${16})) $((t = ${17})) $((__t1 = ${18})) $(($1 = __tmp))
return
fi
: $((_$((_p + i)) = 0))
_close __ $fd
_line=1
_next __
while [ $_tk != 0 ]; do
bt=$_INT
if [ $_tk = $_Int ] ; then
_next __
elif [ $_tk = $_Char ] ; then
_next __
bt=$_CHAR
elif [ $_tk = $_Enum ] ; then
_next __
if [ $_tk != $__LBRACE__ ] ; then
_next __
fi
if [ $_tk = $__LBRACE__ ] ; then
_next __
i=0
while [ $_tk != $__RBRACE__ ]; do
if [ $_tk != $_Id ] ; then
printf "%d: bad enum identifier %d\n" $_line $_tk
: $(($1 = -1))
: $((__tmp = $1)) $((argc = $4)) $((argv_ = $5)) $((fd = $6)) $((bt = $7)) $((ty = $8)) $((poolsz = $9)) $((idmain = ${10})) $((pc = ${11})) $((sp = ${12})) $((bp = ${13})) $((a = ${14})) $((cycle = ${15})) $((i = ${16})) $((t = ${17})) $((__t1 = ${18})) $(($1 = __tmp))
return
fi
_next __
if [ $_tk = $_Assign ] ; then
_next __
if [ $_tk != $_Num ] ; then
printf "%d: bad enum initializer\n" $_line
: $(($1 = -1))
: $((__tmp = $1)) $((argc = $4)) $((argv_ = $5)) $((fd = $6)) $((bt = $7)) $((ty = $8)) $((poolsz = $9)) $((idmain = ${10})) $((pc = ${11})) $((sp = ${12})) $((bp = ${13})) $((a = ${14})) $((cycle = ${15})) $((i = ${16})) $((t = ${17})) $((__t1 = ${18})) $(($1 = __tmp))
return
fi
i=$_ival
_next __
fi
: $((_$((_id + _Class)) = _Num))
: $((_$((_id + _Type)) = _INT))
: $((_$((_id + _Val)) = (i += 1) - 1))
if [ $_tk = $__COMMA__ ] ; then
_next __
fi
done
_next __
fi
fi
while [ $_tk != $__SEMICOLON__ ] && [ $_tk != $__RBRACE__ ]; do
ty=$bt
while [ $_tk = $_Mul ]; do
_next __
ty=$((ty + _PTR))
done
if [ $_tk != $_Id ] ; then
printf "%d: bad global declaration\n" $_line
: $(($1 = -1))
: $((__tmp = $1)) $((argc = $4)) $((argv_ = $5)) $((fd = $6)) $((bt = $7)) $((ty = $8)) $((poolsz = $9)) $((idmain = ${10})) $((pc = ${11})) $((sp = ${12})) $((bp = ${13})) $((a = ${14})) $((cycle = ${15})) $((i = ${16})) $((t = ${17})) $((__t1 = ${18})) $(($1 = __tmp))
return
fi
if [ $((_$((_id + _Class)))) != 0 ] ; then
printf "%d: duplicate global definition\n" $_line
: $(($1 = -1))
: $((__tmp = $1)) $((argc = $4)) $((argv_ = $5)) $((fd = $6)) $((bt = $7)) $((ty = $8)) $((poolsz = $9)) $((idmain = ${10})) $((pc = ${11})) $((sp = ${12})) $((bp = ${13})) $((a = ${14})) $((cycle = ${15})) $((i = ${16})) $((t = ${17})) $((__t1 = ${18})) $(($1 = __tmp))
return
fi
_next __
: $((_$((_id + _Type)) = ty))
if [ $_tk = $__LPAREN__ ] ; then
: $((_$((_id + _Class)) = _Fun))
: $((_$((_id + _Val)) = (_e + 1)))
_next __
i=0
while [ $_tk != $__RPAREN__ ]; do
ty=$_INT
if [ $_tk = $_Int ] ; then
_next __
elif [ $_tk = $_Char ] ; then
_next __
ty=$_CHAR
fi
while [ $_tk = $_Mul ]; do
_next __
ty=$((ty + _PTR))
done
if [ $_tk != $_Id ] ; then
printf "%d: bad parameter declaration\n" $_line
: $(($1 = -1))
: $((__tmp = $1)) $((argc = $4)) $((argv_ = $5)) $((fd = $6)) $((bt = $7)) $((ty = $8)) $((poolsz = $9)) $((idmain = ${10})) $((pc = ${11})) $((sp = ${12})) $((bp = ${13})) $((a = ${14})) $((cycle = ${15})) $((i = ${16})) $((t = ${17})) $((__t1 = ${18})) $(($1 = __tmp))
return
fi
if [ $((_$((_id + _Class)))) = $_Loc ] ; then
printf "%d: duplicate parameter definition\n" $_line
: $(($1 = -1))
: $((__tmp = $1)) $((argc = $4)) $((argv_ = $5)) $((fd = $6)) $((bt = $7)) $((ty = $8)) $((poolsz = $9)) $((idmain = ${10})) $((pc = ${11})) $((sp = ${12})) $((bp = ${13})) $((a = ${14})) $((cycle = ${15})) $((i = ${16})) $((t = ${17})) $((__t1 = ${18})) $(($1 = __tmp))
return
fi
: $((_$((_id + _HClass)) = _$((_id + _Class))))
: $((_$((_id + _Class)) = _Loc))
: $((_$((_id + _HType)) = _$((_id + _Type))))
: $((_$((_id + _Type)) = ty))
: $((_$((_id + _HVal)) = _$((_id + _Val))))
: $((_$((_id + _Val)) = (i += 1) - 1))
_next __
if [ $_tk = $__COMMA__ ] ; then
_next __
fi