-
Notifications
You must be signed in to change notification settings - Fork 11
/
c8c.c
1206 lines (1104 loc) · 26.1 KB
/
c8c.c
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
// ____ ___ ____
// / __) ( _ ) / __)
// ( (__ / _ \ ( (__
// \____) \___/ \____)
//
// C8C is a highly portable C-like compiler for the chip8 platform.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <ctype.h>
#include <stdbool.h>
static char* term();
static void expression(char* overrider, const bool shortable);
static void dblock();
// Variable names.
static int v;
static char* vars[16];
// Array names and function names.
static int l;
static struct label
{
char* name;
int args;
int height;
}
labels[128];
// Counter for branches.
static int branch;
// The current input file character.
static int now;
// The line number.
static int nline = 1;
// Line max length.
static const int lmax = 512;
// Input file (c8).
static FILE* fi;
// Output file (asm).
static FILE* fo;
// Input file name.
static char* c8src;
// Output file name.
static char* assem;
// Line buffer.
static char* line;
// Failure signaler raised by bomb(). Used to remove .c8 file at cleanup.
static bool failure;
// Writes to the output file. A new line is included.
static void print(const char* msg, ...)
{
va_list args;
va_start(args, msg);
vfprintf(fo, msg, args);
fprintf(fo, "\n");
va_end(args);
}
// Writes to standard error. A newline is included.
static void bomb(const char* msg, ...)
{
va_list args;
va_start(args, msg);
fprintf(stderr, "error: line %d: ", nline);
vfprintf(stderr, msg, args);
fprintf(stderr, "\n");
va_end(args);
failure = true;
exit(1);
}
// Buffers a new character from the input file.
static void buffer()
{
static int reads;
if(reads == lmax - 1)
bomb("line too long");
line[reads++] = now == '\n' ? '\0' : now;
if(now == '\n')
{
nline++;
reads = 0;
print(";%s", line);
}
}
// Gets a new charcter from the input file. Does not ignore comments.
static void step()
{
now = fgetc(fi);
buffer();
}
// Removes all variable names.
static void reset()
{
for(int i = 0; i < v; i++)
{
free(vars[i]);
vars[i] = NULL;
}
v = 0;
}
// Removes all label names.
static void kill()
{
for(int i = 0; i < l; i++)
{
free(labels[i].name);
labels[i].name = NULL;
}
l = 0;
}
// Gets a new charcter from the input file. Ignores (//) style comments.
static void next()
{
step();
if(now == '/')
{
step();
if(now != '/')
bomb("expected '/'");
while(now != '\n')
step();
}
}
// Skips sequential white space.
static void skip()
{
while(isspace(now)) next();
}
// Duplicates a string.
static char* dup(const char* s)
{
return strcpy((char*) malloc(strlen(s) + 1), s);
}
// Shuts down everything. Removes assembly file if something went wrong.
static void shutdown()
{
reset();
kill();
free(line);
if(fi) fclose(fi);
if(fo) fclose(fo);
if(failure)
remove(assem);
}
// Initializes everything.
static void init(char* argv[])
{
struct label baked[] = {
{ dup("draw" ), 3, 0 },
{ dup("putchar"), 3, 0 },
{ dup("rand" ), 0, 0 },
{ dup("getchar"), 0, 0 },
{ dup("cls" ), 0, 0 },
{ dup("sizeof" ), 1, 0 },
};
for(unsigned i = 0; i < sizeof(baked) / sizeof(*baked); i++)
labels[l++] = baked[i];
c8src = argv[1];
assem = argv[2];
// Input file.
fi = fopen(argv[1], "r");
if(fi == NULL)
{
fprintf(stderr, "error: %s does not exist\n", c8src);
exit(1);
}
// Output file.
fo = fopen(argv[2], "w");
if(fo == NULL)
{
fprintf(stderr, "error: %s cannot be made\n", assem);
exit(1);
}
line = (char*) malloc(lmax * sizeof(char));
next();
skip();
atexit(shutdown);
}
// Returns true if two strings match, else false.
static bool eql(const char* a, const char* b)
{
if(a == NULL || b == NULL)
return 0;
return strcmp(a, b) == 0;
}
// Retrusn a string of the 'now' character.
static char* peeks()
{
char n[] = { (char) now, '\0' };
return dup(n);
}
// Returns true if 'now' at the end of an expression.
static bool isendexpr()
{
return now == ';' || now == ')' || now == ']' || now == ',';
}
// Returns true if 'now' at end of operator.
static bool isendop()
{
return isalnum(now) || isspace(now) || isendexpr();
}
// Skips all white space and comments, then checks if 'now' matches what is expected.
// Feed is advanced.
static void match(const int c)
{
skip();
if(now != c)
bomb("expected '%c'", c);
next();
}
// Returns a string name.
static char* name()
{
skip();
// First char must be alpha or underscore
if(!(now == '_' || isalpha(now)))
bomb("names must start with underscores or alpha chars");
const int size = 128;
char* name = (char*) malloc(size * sizeof(char));
int i = 0;
// But name can contain alpha an numeric chars.
while(isalnum(now) || now == '_')
{
if(i == size - 1)
bomb("name too long");
name[i++] = now;
next();
}
name[i] = '\0';
return name;
}
// Returns a string digit.
static char* dig()
{
skip();
if(!isdigit(now))
bomb("expected value");
const int size = 128;
char* d = (char*) malloc(size * sizeof(char));
int i = 0;
while(isdigit(now) || isxdigit(now) || tolower(now) == 'x')
{
if(i == size - 1)
bomb("digit too long");
d[i++] = now;
next();
}
d[i] = '\0';
return d;
}
// Returns a string operator.
static char* op()
{
skip();
const int size = 128;
char* o = (char*) malloc(size * sizeof(char));
int i = 0;
while(!isendop())
{
if(i == size - 1)
bomb("operator too long");
o[i++] = now;
next();
}
o[i] = '\0';
return o;
}
// Increments the v register.
static void incv()
{
if(++v == 0xE) bomb("register stack overflow");
}
// Decrements the v register.
static void decv()
{
if(--v < 0x00) bomb("register stack underflow");
}
// Removes 'n' variable names.
static void pop(const int n)
{
for(int i = 0; i < n; i++)
{
decv();
free(vars[v]);
vars[v] = NULL;
}
}
// Returns a positive integer if a label name was found.
static int find(const char* name)
{
for(int i = 0; i < l; i++)
if(eql(name, labels[i].name))
return i;
return -1;
}
// Returns the v-register number of a declared variable.
// Exits if not found.
static int var(char* name)
{
for(int i = 0; i < v; i++)
if(eql(name, vars[i]))
return i;
bomb("variable name '%s' not defined", name);
return 0; /* Keep compiler quiet. */
}
// Returns true if a name is already declared, else exits.
// Note that this checks both automatic variables and label names.
static bool isndef(char* name)
{
// Checks vars.
for(int i = 0; i < v; i++)
if(eql(name, vars[i]))
bomb("name '%s' already defined", name);
// Checks labels.
for(int i = 0; i < l; i++)
if(eql(name, labels[i].name))
bomb("label '%s' already defined", name);
return true;
}
// Turns a string digit into a long digit.
// All integers are unsigned byte sized and thus clamped 0-255 inclusive.
static uint8_t tobyte(const char* value)
{
return strtoul(value, NULL, 0) % 256;
}
// Generate negation (-).
static void gneg()
{
print("\tLD VF,0xFF");
print("\tXOR V%1X,VF", v);
print("\tADD V%1X,0x01", v);
}
// Generate bitwise inversion (~).
static void ginv()
{
print("\tLD VF,0xFF");
print("\tXOR V%1X,VF", v);
}
// Generate logical not (!).
static void gnotl()
{
// Converts a positive number into logical 1.
print("\tSE V%1X,0x00", v);
print("\tLD V%1X,0x01", v);
// Xor the number with logical 1.
print("\tLD VF,0x01");
print("\tXOR V%1X,VF", v);
}
// Generate addition (+).
static void gadd()
{
print("\tADD V%1X,V%1X", v - 1, v);
}
// Generate subtraction (-).
static void gsub()
{
print("\tSUB V%1X,V%1X", v - 1, v);
}
// Generate bitwise and (&).
static void gand()
{
print("\tAND V%1X,V%1X", v - 1, v);
}
// Generate bitwise or (|).
static void gor()
{
print("\tOR V%1X,V%1X", v - 1, v);
}
// Generate bitwise xor (^).
static void gxor()
{
print("\tXOR V%1X,V%1X", v - 1, v);
}
// Generate move.
static void gmove()
{
print("\tLD V%1X,V%1X", v - 1, v);
}
// Generate not equal to (!=).
static void gneqlto()
{
print("\tLD VF,0x00");
print("\tSE V%1X,V%1X", v - 1, v);
print("\tLD VF,0x01");
print("\tLD V%1X,VF", v - 1);
}
// Generate equal to (==).
static void geqlto()
{
print("\tLD VF,0x01");
print("\tSE V%1X,V%1X", v - 1, v);
print("\tLD VF,0x00");
print("\tLD V%1X,VF", v - 1);
}
// Generate less than or equal to (<=).
static void glteqlto()
{
print("\tSUBN V%1X,V%1X", v - 1, v);
print("\tLD V%1X,VF", v - 1);
}
// Generate greater than or equal to (>=).
static void ggteqlto()
{
print("\tSUB V%1X,V%1X", v - 1, v);
print("\tLD V%1X,VF", v - 1);
}
// Generate less than (<).
static void glt()
{
ggteqlto();
print("\tLD VF,0x01");
print("\tXOR V%1X,VF", v - 1);
}
// Generate greater than (>).
static void ggt()
{
glteqlto();
print("\tLD VF,0x01");
print("\tXOR V%1X,VF", v - 1);
}
// Generate copy.
static void gcp(char* ta)
{
print("\tLD V%1X,V%1X", var(ta), v - 1);
}
// Generate equal (=).
static void geql(char* ta)
{
gmove();
gcp(ta);
}
// Generate add equal (+=).
static void gaddeql(char* ta)
{
gadd();
gcp(ta);
}
// Generate subtract equal (-=)
static void gsubeql(char* ta)
{
gsub();
gcp(ta);
}
// Generate xor equal (^=)
static void gxoreql(char* ta)
{
gxor();
gcp(ta);
}
// Generate and equal (&=)
static void gandeql(char* ta)
{
gand();
gcp(ta);
}
// Generate or equal (|=)
static void goreql(char* ta)
{
gor();
gcp(ta);
}
// Generate load name.
static void glname(char* ta)
{
print("\tLD V%1X,V%1X", v, var(ta));
}
// Generate load digit.
static void gldig(char* tb)
{
print("\tLD V%1X,0x%02X", v, tobyte(tb));
}
// Generate frame push.
static void gfpush()
{
print("\tLD F,VE");
print("\tLD [I],VE");
print("\tLD VF,0x03");
print("\tADD VE,VF");
}
// Generate frame pop.
static void gfpop()
{
print("\tLD VF,0x03");
print("\tSUB VE,VF");
print("\tLD VF,V%1X", v);
print("\tLD F,VE");
print("\tLD VE,[I]");
print("\tRET");
}
// Unary not.
static char* notl()
{
match('!');
char* ta = term();
gnotl();
return ta;
}
// Unary positive.
static char* pos()
{
match('+');
if(now == '+')
bomb("operator '++' not supported");
return term();
}
// Unary bitwise invert.
static char* inv()
{
match('~');
char* ta = term();
ginv();
return ta;
}
// Unary negate.
static char* neg()
{
match('-');
if(now == '-')
bomb("operator '--' not supported");
char* ta = term();
gneg();
return ta;
}
// Force an expression.
static char* fexp()
{
match('(');
expression(NULL, true);
match(')');
return dup(")");
}
// Load digit.
static char* ldig()
{
char* tb = dig();
gldig(tb);
return tb;
}
// Saves a copy of this frame then moves this frame to next.
static void move(const int args)
{
gfpush();
for(int i = 0; i < args; i++)
print("\tLD V%1X,V%1X", i, v + i);
}
// Return.
static void sret()
{
expression(NULL, true);
match(';');
gfpop();
}
// Get character.
static void getchr()
{
print("getchar:");
print("\tLD V0,0xFF"); // Return value.
print("\tLD V1,0x00\n\tSKNP V1\n\tLD V0,0x00");
print("\tLD V1,0x01\n\tSKNP V1\n\tLD V0,0x01");
print("\tLD V1,0x02\n\tSKNP V1\n\tLD V0,0x02");
print("\tLD V1,0x03\n\tSKNP V1\n\tLD V0,0x03");
print("\tLD V1,0x04\n\tSKNP V1\n\tLD V0,0x04");
print("\tLD V1,0x05\n\tSKNP V1\n\tLD V0,0x05");
print("\tLD V1,0x06\n\tSKNP V1\n\tLD V0,0x06");
print("\tLD V1,0x07\n\tSKNP V1\n\tLD V0,0x07");
print("\tLD V1,0x08\n\tSKNP V1\n\tLD V0,0x08");
print("\tLD V1,0x09\n\tSKNP V1\n\tLD V0,0x09");
print("\tLD V1,0x0A\n\tSKNP V1\n\tLD V0,0x0A");
print("\tLD V1,0x0B\n\tSKNP V1\n\tLD V0,0x0B");
print("\tLD V1,0x0C\n\tSKNP V1\n\tLD V0,0x0C");
print("\tLD V1,0x0D\n\tSKNP V1\n\tLD V0,0x0D");
print("\tLD V1,0x0E\n\tSKNP V1\n\tLD V0,0x0E");
print("\tLD V1,0x0F\n\tSKNP V1\n\tLD V0,0x0F");
// Done, restore return value.
gfpop();
}
// Put charater.
static void putchr()
{
const int spacing = 1;
const int width = 4;
print("putchar:");
// Shift up: V0, V1, V2 will be populated by I, I+1, I+2.
print("\tLD V5,V2"); // N
print("\tLD V4,V1"); // Y
print("\tLD V3,V0"); // X
// V6 will serve as a collision flag.
print("\tLD V6,0x00");
print("\tLD B,V5");
print("\tLD V2,[I]");
// First.
print("\tLD F,V0");
print("\tDRW V3,V4,0x5");
print("\tOR V6,VF");
// Second.
print("\tLD F,V1");
print("\tADD V3,0x%02X", width + spacing);
print("\tDRW V3,V4,0x5");
print("\tOR V6,VF");
// Third.
print("\tLD F,V2");
print("\tADD V3,0x%02X", width + spacing);
print("\tDRW V3,V4,0x5");
print("\tOR V6,VF");
// Done. Restore return value.
v = 6;
gfpop();
}
// Draw. Hardcoded inline for performance.
static void draw()
{
const int args = 2;
// The first two arguments are expressions for x, y.
for(int i = 0; i < args; i++)
{
expression(NULL, true);
incv();
match(',');
}
// The third argument is a label name for the sprite to draw.
// Draw is the only function that can take a label as an argument.
char* label = name();
const int index = find(label);
if(index == -1)
bomb("label '%s' not defined", label);
print("\tLD I,%s", labels[index].name);
// Optionally, appending square brackets to the label will index the sprite.
skip();
if(now == '[')
{
const int b = branch++;
match('[');
expression(NULL, true);
match(']');
print("WHILE%d:", b);
print("\tSNE V%X,0x00", v);
print("\tJP OUT%d", b);
print("\tLD VF,0x%02X", labels[index].args);
print("\tADD I,VF");
print("\tLD VF,0x01");
print("\tSUB V%X,VF", v);
print("\tJP WHILE%d", b);
print("OUT%d:", b);
}
print("\tDRW V%1X,V%1X,0x%1X", v - 2, v - 1, labels[index].args);
free(label);
v -= args;
}
// Clear screen. Inlined for performance.
static void clear()
{
print("\tCLS");
}
// Random number. Inlined for performance.
static void rnd()
{
print("\tRND VF,0xFF");
}
// Generate function call.
static void gfcall(const char* name)
{
const int i = find(name);
if(i == -1)
bomb("function '%s' not defined", name);
int args = 0;
while(now != ')')
{
expression(NULL, true);
incv();
args++;
skip();
if(now == ',')
match(',');
else
if(now == ')')
break;
else bomb("unknown symbol in argument list");
}
if(labels[i].args != args)
bomb("argument mismatch when calling '%s'", name);
v -= args;
move(args);
print("\tCALL %s", name);
}
// Generate size.
static void szof()
{
char* n = name();
const int index = find(n);
if(index == -1)
bomb("expected label but got %s", n);
free(n);
skip();
if(now == '[')
{
match('[');
match(']');
print("\tLD VF,0x%02X", labels[index].height);
}
else
print("\tLD VF,0x%02X", labels[index].args);
}
// Function call.
static void fcall(const char* name)
{
match('(');
// These built in functions that will inline.
eql(name, "draw") ? draw() :
eql(name, "sizeof") ? szof() :
eql(name, "rand") ? rnd() :
eql(name, "clear") ? clear() : gfcall(name);
match(')');
// Load return value.
print("\tLD V%1X,VF", v);
}
// Returns true if the string is a token.
static bool istoken(char* s)
{
return eql(s, "while") || eql(s, "if") || eql(s, "auto") || eql(s, "return");
}
// Load name.
static char* lname()
{
char* ta = name();
if(istoken(ta))
return ta;
skip();
now == '(' ? fcall(ta) : glname(ta);
return ta;
}
// Terms may start with a prefix modifier, an alpha for a name load
// or a digit for a digit load. A term may be forced into an expression
// with surrounding round brackets.
static char* term()
{
skip();
return
// Prefix modifiers.
now == '~' ? inv () :
now == '+' ? pos () :
now == '!' ? notl () :
now == '-' ? neg () :
// Name load.
isalpha(now) ? lname() :
// Digit load.
isdigit(now) ? ldig () :
// Term is expression (enclosed in brackets).
now == '(' ? fexp() : peeks();
/* Returns the string in all cases, even if nothing was done. */
}
// Operate.
// Operators work on chained variables from V0 to VD (inclusive).
static void operate(char* o, char* t)
{
eql(o, "=" ) ? geql (t) : // Assignment.
eql(o, "+=") ? gaddeql (t) : // Assignment.
eql(o, "-=") ? gsubeql (t) : // Assignment.
eql(o, "^=") ? gxoreql (t) : // Assignment.
eql(o, "&=") ? gandeql (t) : // Assignment.
eql(o, "|=") ? goreql (t) : // Assignment.
eql(o, "+" ) ? gadd () : // Chain.
eql(o, "-" ) ? gsub () : // Chain.
eql(o, "&" ) ? gand () : // Chain.
eql(o, "^" ) ? gxor () : // Chain.
eql(o, "|" ) ? gor () : // Chain.
eql(o, "<" ) ? glt () : // Comparison.
eql(o, "<=") ? glteqlto () : // Comparison.
eql(o, ">=") ? ggteqlto () : // Comparison.
eql(o, ">" ) ? ggt () : // Comparison.
eql(o, "!=") ? gneqlto () : // Comparison.
eql(o, "==") ? geqlto () : // Comparison.
eql(o, "||") ? gmove () : // Logical.
eql(o, "&&") ? gmove () : // Logical.
eql(o, "!" ) ? gnotl () : // Unary.
bomb("unknown operator '%s'", o);
/* Exits if operator is not found. */
}
// Names start with alpha characters or underscores.
static bool isname(const char* s)
{
if(s == NULL)
bomb("derefereced a null pointer.");
return isalpha(s[0]) || s[0] == '_';
}
// TA O TB O TA ...
static void expression(char* overrider, const bool shortable)
{
// Assume term is not an L-value.
bool lvalue = false;
// Goes high when using logical operators.
bool shorting = false;
const int b = branch++;
char* ta = overrider ? overrider : term();
// If term is a name then it is an L-value.
if(isname(ta))
lvalue = true;
// If the name is a label then it is not an L-value.
if(find(ta) != -1)
lvalue = false;
skip();
while(!isendexpr())
{
incv();
char* o = op();
// If the operator is an assignment operator, then
// a new expression is computed. lvalue is first checked.
if(eql(o, "=" )
|| eql(o, "+=")
|| eql(o, "-=")
|| eql(o, "^=")
|| eql(o, "&=")
|| eql(o, "|="))
{
if(lvalue == false)
bomb("expected lvalue to the left of operator '%s'", o);
expression(NULL, true);
}
// If the operator is a comparison operator then a new expression is computed.
else if(
eql(o, "<" )
|| eql(o, "<=")
|| eql(o, ">=")
|| eql(o, ">" )
|| eql(o, "!=")
|| eql(o, "=="))
expression(NULL, true);
// If the operator is a short circuit operator a new
// expression is computed. Given this, the expression will turn
// logical at the end of each subsequent expression.
else if(
eql(o, "||")
|| eql(o, "&&"))
{
shorting = true;
print("\t%s V%1X,0x00", eql(o, "||") ? "SE" : "SNE", v - 1);
print("\tJP END%d", b);
expression(NULL, false);
}
char* tb = term();
// If this is the second term of the expression, then there is
// no way that this expression is l-value correct in case a future
// assignment operator is encountered.
lvalue = false;
// Operate on the two terms and let the magic happen.
operate(o, ta);
decv();
free(o);
free(ta);
ta = tb;
}
if(shorting)
{
print("END%d:", b);
if(shortable)
{
print("\tSE V%1X,0x00", v);
print("\tLD V%1X,0x01", v);
}
}
free(ta);
}
// Generate array.
// Returns the number of elements in the array.
static int garr()
{
match('{');
int size = 0;
skip();
while(now != '}')
{
char* d = dig();
print("\tDB 0x%02X", tobyte(d));
size++;
free(d);
skip();
// Bytes are separated by commas.
// The last byte can be comma but does not have to be.
if(now == ',')
{
match(',');
skip();
if(now == '}') break;
}
}
if(size > 0xF)
bomb("too many elements in sprite");
match('}');
return size;
}
// Declaring an array (sprite).
static void arr(char* name)
{
isndef(name);
print("%s:", name);
skip();
int size = 0;
int height = 0;
// Array of sprite arrays.
// eg. sprite[] = { { 0x01, 0x02 ,0x03 }, { 0x01, 0x02 ,0x03 } };
// In this case, height = 2, and size = 3.
if(now == '[')
{
match('[');
match(']');
match('=');
match('{');
size = garr();