-
Notifications
You must be signed in to change notification settings - Fork 0
/
COMPILE.C
1136 lines (991 loc) · 32.7 KB
/
COMPILE.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
// ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
// ³ ³
// ³ The Verge-C Compiler v.0.10 ³
// ³ Copyright (C)1997 BJ Eirich ³
// ³ ³
// ³ Module: COMPILE.C ³
// ³ ³
// ³ Description: The main lexical parser and code generator. ³
// ³ ³
// ³ Portability: ANSI C - should compile on any 32 bit compiler. ³
// ³ ³
// ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
#include <stdio.h>
#include "code.h"
#include "libfuncs.h"
#include "vcc.h"
// ============================== Constants ===============================
// Character types
#define LETTER 1
#define DIGIT 2
#define SPECIAL 3
// Token types
#define IDENTIFIER 1
#define DIGIT 2
#define CONTROL 3
#define RESERVED 4
#define FUNCTION 5
#define VAR0 5
#define VAR1 6
#define VAR2 7
// ============================== Variables ==============================
struct label // goto labels
{
char ident[40];
char *pos;
};
struct label labels[200]; // goto labels
struct label gotos[200]; // goto occurence records
char token[2048]; // current token buffer
char lasttoken[2048]; // restorebuf for NextIs
unsigned int token_nvalue; // int value of token if it's type DIGIT
char token_type; // type of current token.
char token_subtype; // This is just crap.
char *src; // ptr -> current location in sourcefile
char *code; // ptr -> generated output code buffer
char *cpos; // ptr -> current code buffer location
char chr_table[256]; // character-types lookup table
char *numargsptr; // number of arguements to IF ptr
unsigned int scriptofstbl[1024]; // script offset table
int numscripts=0; // number of scripts in the VC file
int lines=1;
// Compilation-state flags
char inevent=0,iex=0;
char *scripttoken;
int funcidx;
char numlabels=0,numgotos=0;
// ================================ Code =================================
err(char *str)
{ FILE *f;
if (!quiet) printf("%s (%d) \n",str,lines);
if (quiet)
{ f=fopen("ERROR.TXT","w");
fprintf(f,"%s (%d)\n",str,lines);
fclose(f);
}
remove("$$TMEP$$.MA_");
exit(-1);
}
char TokenIs(char *str)
{
if (!strcmp(str,token)) return 1;
else return 0;
}
ParseWhitespace ()
{ char c;
// ParseWhitespace() does what you'd expect - sifts through any white
// space and advances the file pointer accordingly. Additionally, it
// handles // style comments as well as /* and */ comments.
while (1)
{ while (*src <= ' ')
{ if (!*src) return; // EOF reached
if (*src == '\n') lines++;
src++;
}
if (src[0] == '/' && src[1] == '/') // Skip // comments
{ while (*src && (*src != '\n'))
src++; // Skip to next line
continue;
}
if (src[0] == '/' && src[1] == '*') // Skip /* until */
{ while (!(src[0] == '*' && src[1] == '/'))
{ if (*src == '\n') lines++;
src++;
if (!*src) return;
}
src += 2;
continue;
}
break; // end of whitespace
}
}
CheckLibFunc()
{ int i;
// If the current token is a recognized library function, sets
// token_type to FUNCTION and funcidx to the appropriate function code.
// Todo: Maybe replace this with a binary tree instead of a linear
// search. It hasn't gotten slow enough yet tho. :)
token_nvalue = 0;
if (strlen(token) == 1 && token[0]>64 && token[0]<91)
{ token_type = IDENTIFIER;
token_nvalue = token[0]-64;
return; }
if (TokenIs("FLAGS") || TokenIs("IF") || TokenIs("FOR") ||
TokenIs("WHILE") || TokenIs("SWITCH") || TokenIs("CASE") ||
TokenIs("GOTO"))
{ token_type = RESERVED;
return;
}
if (TokenIs("AND"))
{ token_type = CONTROL;
token[0] = '&'; token[1] = '&'; token[2] = 0;
return;
}
i = 0;
while (i < numfuncs)
{ if (!strcmp(funcs[i], token)) break;
i++;
}
if (i != numfuncs) token_type = FUNCTION;
funcidx = i;
}
unsigned char SearchVarList ()
{ unsigned char i;
i = 0;
while (i < numvars0)
{ if (!strcmp(vars0[i], token)) break;
i++;
}
if (i != numvars0)
{
token_type = RESERVED;
token_subtype = VAR0;
return i;
}
i = 0;
while (i < numvars1)
{ if (!strcmp(vars1[i], token)) break;
i++;
}
if (i != numvars1)
{
token_type = RESERVED;
token_subtype = VAR1;
return i;
}
i = 0;
while (i < numvars2)
{ if (!strcmp(vars2[i], token)) break;
i++;
}
if (i != numvars2)
{
token_type = RESERVED;
token_subtype = VAR2;
return i;
}
return i;
}
GetIdentifier ()
{ int i;
// Retrieves an identifier from the source buffer. Before calling this,
// it should be guaranteed that the first character is a letter. A check
// will need to be made afterward to see if it's a reserved word.
i = 0;
while ((chr_table[*src] == LETTER) || (chr_table[*src] == DIGIT))
{ token[i] = *src;
src++;
i++;
}
token[i] = 0;
strupr(&token);
CheckLibFunc();
SearchVarList();
}
GetNumber()
{ int i;
// Grabs the next number. String version remains in token[], numerical
// version is placed in token_nvalue.
i = 0;
while (chr_table[*src] == DIGIT)
{ token[i] = *src;
src++;
i++;
}
token[i] = 0;
token_nvalue = atoi (&token);
}
GetPunctuation()
{ char c;
// Grabs the next recognized punctuation type. If a double-char punctuation
// type is recognized, it will be returned. Ie, differentiate b/w = and ==.
c = *src;
switch (c)
{ case '(': token[0] = '('; token[1] = 0; src ++; break;
case ')': token[0] = ')'; token[1] = 0; src ++; break;
case '{': token[0] = '{'; token[1] = 0; src ++; break;
case '}': token[0] = '}'; token[1] = 0; src ++; break;
case '[': token[0] = '('; token[1] = 0; src ++; break;
case ']': token[0] = ')'; token[1] = 0; src ++; break;
case ',': token[0] = ','; token[1] = 0; src ++; break;
case ':': token[0] = ':'; token[1] = 0; src ++; break;
case ';': token[0] = ';'; token[1] = 0; src ++; break;
case '/': token[0] = '/'; token[1] = 0; src ++; break;
case '*': token[0] = '*'; token[1] = 0; src ++; break;
case '%': token[0] = '%'; token[1] = 0; src ++; break;
case '\"': token[0] = '\"'; token[1] = 0; src ++; break;
case '\'': token[0] = '\''; token[1] = 0; src ++; break;
case '+' : token[0] = '+'; // Is it ++ or += or +?
src++;
if (*src == '+')
{ token[1] = '+';
src++; }
else if (*src == '=')
{ token[1] = '=';
src++; }
else token[1] = 0;
token[2] = 0;
break;
case '-' : token[0] = '-'; // Is it -- or -= or -?
src++;
if (*src == '-')
{ token[1] = '-';
src++; }
else if (*src == '=')
{ token[1] = '=';
src++; }
else token[1] = 0;
token[2] = 0;
break;
case '>': token[0] = '>'; // Is it > or >=?
src++;
if (*src == '=') // It's >=
{ token[1] = '=';
token[2] = 0;
src++; break;
}
token[1] = 0; // It's >
break;
case '<': token[0] = '<'; // Is it < or <=?
src++;
if (*src == '=') // It's <=
{ token[1] = '=';
token[2] = 0;
src++; break;
}
token[1] = 0; // It's <
break;
case '!': token[0] = '!';
src++; // Is it just ! or is it != ?
if (*src == '=') // It's !=
{ token[1] = '=';
token[2] = 0;
src++; break;
}
token[1] = 0; // It's just !
break;
case '=': token[0] = '=';
src++; // is = or == ?
if (*src == '=') // Doesn't really matter,
{ token[1] = 0; // just skip the last byte
src ++;
}
else token[1] = 0;
break;
case '&': token[0] = '&'; token[1] = '&'; token[2] = 0;
src += 2; break;
default: src ++; // This should be an error.
}
}
GetString ()
{ int i;
// Expects a "quoted" string. Places the contents of the string in
// token[] but does not include the quotes.
Expect("\"");
i = 0;
while (*src != '\"')
{ token[i] = *src;
src ++;
i ++;
}
src ++;
token[i] = 0;
}
GetToken ()
{ int i;
char c;
// simply reads in the next statement and places it in the
// token buffer.
ParseWhitespace();
i = 0;
switch (chr_table[*src])
{ case LETTER: { token_type = IDENTIFIER; GetIdentifier(); break; }
case DIGIT: { token_type = DIGIT; GetNumber(); break; }
case SPECIAL: { token_type = CONTROL; GetPunctuation(); break; }
}
if (!*src && inevent) err("Unexpected end of file");
}
char NextIs(char *str)
{ char *ptr,tt,tst,i;
int olines,nv;
ptr = src;
olines = lines;
tt = token_type;
tst = token_subtype;
nv = token_nvalue;
memcpy(&lasttoken, &token, 2048);
GetToken();
src = ptr;
lines = olines;
token_nvalue = nv;
tst = token_subtype;
tt = token_type;
if (!strcmp(str,token)) i=1; else i=0;
memcpy(&token, &lasttoken, 2048);
return i;
}
Expect(char *str)
{ FILE *f;
GetToken ();
if (!strcmp(str,token)) return;
if (!quiet) printf ("error: %s expected, %s got (%d)", str, &token, lines);
if (quiet)
{ f=fopen("ERROR.TXT","w");
fprintf(f,"error: %s expected, %s got (%d) \n", str, &token, lines);
fclose(f);
}
exit (-1);
}
int ExpectNumber ()
{
GetToken();
if (token_type != DIGIT) err ("error: Numerical value expected");
return token_nvalue;
}
EmitC (char c)
{
*cpos = c;
cpos ++;
}
EmitW (short int w)
{ char *ptr;
ptr = (char *)&w;
*cpos = *ptr;
cpos ++;
ptr ++;
*cpos = *ptr;
cpos ++;
}
EmitD (int w)
{ char *ptr;
ptr = (char *)&w;
*cpos = *ptr;
cpos ++;
ptr ++;
*cpos = *ptr;
cpos ++;
ptr ++;
*cpos = *ptr;
cpos ++;
ptr ++;
*cpos = *ptr;
cpos ++;
}
EmitString (char *str)
{ int i;
i = 0;
while (str[i])
{
*cpos = str[i];
cpos ++;
i ++;
}
*cpos = 0;
cpos ++;
}
HandleOperand ()
{ unsigned char varidx;
GetToken ();
if (token_type == DIGIT)
{
EmitC (OP_IMMEDIATE);
EmitD (token_nvalue);
} else
if (token_subtype == VAR0)
{
EmitC (OP_VAR0);
varidx = SearchVarList ();
if (varidx == numvars0) err ("error: Unknown identifier.");
EmitC (varidx);
} else
if (token_subtype == VAR1)
{
EmitC (OP_VAR1);
varidx = SearchVarList ();
if (varidx == numvars1) err ("error: Unknown identifier.");
EmitC (varidx);
Expect ("(");
EmitOperand ();
Expect (")");
} else
if (token_subtype == VAR2)
{
EmitC (OP_VAR2);
varidx = SearchVarList ();
if (varidx == numvars2) err ("error: Unknown identifier.");
EmitC (varidx);
Expect ("(");
EmitOperand ();
Expect (",");
EmitOperand ();
Expect (")");
}
}
EmitOperand ()
{
while (1) // Modifier-process loop.
{
if (NextIs("("))
{
EmitC (OP_GROUP);
GetToken();
EmitOperand();
Expect(")");
}
else HandleOperand ();
if (NextIs("+"))
{
EmitC (ADD);
GetToken ();
continue;
}
else if (NextIs("-"))
{
EmitC (SUB);
GetToken ();
continue;
}
else if (NextIs("/"))
{
EmitC (DIV);
GetToken ();
continue;
}
else if (NextIs("*"))
{
EmitC (MULT);
GetToken ();
continue;
}
else if (NextIs("%"))
{
EmitC (MOD);
GetToken ();
continue;
}
else
{
EmitC (OP_END);
break;
}
}
}
char HandleExpression()
{
// Parses one single "expression". Can be anything short of a new script.
GetToken ();
if (token_type == FUNCTION)
{ OutputCode (funcidx);
return 1;
}
if (token_type == RESERVED && TokenIs("IF"))
{ ProcessIf ();
return 1;
}
if (token_type == RESERVED && TokenIs("FOR"))
{ ProcessFor ();
return 1;
}
if (token_type == RESERVED && TokenIs("WHILE"))
{ ProcessWhile();
return 1;
}
if (token_type == RESERVED && TokenIs("SWITCH"))
{ ProcessSwitch();
return 1;
}
if (token_type == RESERVED && TokenIs("GOTO"))
{ ProcessGoto();
return 1;
}
if (token_type == RESERVED && token_subtype == VAR0)
{ ProcessVar0Assign ();
return 1;
}
if (token_type == RESERVED && token_subtype == VAR1)
{ ProcessVar1Assign ();
return 1;
}
if (token_type == RESERVED && token_subtype == VAR2)
{ ProcessVar2Assign ();
return 1;
}
if (NextIs(":"))
{
memcpy(labels[numlabels].ident,lasttoken,40);
(int) labels[numlabels].pos=(int) cpos-(int) code;
if (verbose) printf("label %s found on line %d, cpos: %d. \n",&lasttoken, lines, cpos-code);
numlabels++;
Expect (":");
return 1;
}
return 0; // Not a valid command;
}
ProcessVar0Assign ()
{ int a;
EmitC (VAR0_ASSIGN);
a=SearchVarList();
EmitC (a);
if (!write0[a]) err("error: Variable is read-only.");
GetToken (); // Find relational operator
if (TokenIs("="))
EmitC (SET);
else if (TokenIs("+="))
EmitC (INCSET);
else if (TokenIs("-="))
EmitC (DECSET);
else if (TokenIs("++"))
{ EmitC (INCREMENT);
Expect (";");
return; }
else if (TokenIs("--"))
{ EmitC (DECREMENT);
Expect (";");
return; }
EmitOperand ();
Expect (";");
}
ProcessVar1Assign ()
{ int a;
EmitC (VAR1_ASSIGN);
a=SearchVarList();
EmitC (a);
if (!write1[a]) err("error: Variable is read-only.");
Expect ("(");
EmitOperand ();
Expect (")");
GetToken (); // Find relational operator
if (TokenIs("="))
EmitC (SET);
else if (TokenIs("+="))
EmitC (INCSET);
else if (TokenIs("-="))
EmitC (DECSET);
else if (TokenIs("++"))
{ EmitC (INCREMENT);
Expect (";");
return; }
else if (TokenIs("--"))
{ EmitC (DECREMENT);
Expect (";");
return; }
EmitOperand ();
Expect (";");
}
ProcessVar2Assign ()
{ int a;
EmitC (VAR2_ASSIGN);
a=SearchVarList();
EmitC (a);
if (!write2[a]) err("error: Variable is read-only.");
Expect ("(");
EmitOperand ();
Expect (",");
EmitOperand ();
Expect (")");
GetToken (); // Find relational operator
if (TokenIs("="))
EmitC (SET);
else if (TokenIs("+="))
EmitC (INCSET);
else if (TokenIs("-="))
EmitC (DECSET);
else if (TokenIs("++"))
{ EmitC (INCREMENT);
Expect (";");
return; }
else if (TokenIs("--"))
{ EmitC (DECREMENT);
Expect (";");
return; }
EmitOperand ();
Expect (";");
}
ProcessIf ()
{ unsigned char numargs=0, excl=0, varidx;
char *returnptr, *buf;
// The general opcode form of an IF is:
// <BYTE: GENERAL_IF>
// <BYTE: number of arguements to IF>
// <DWORD: ptr -> execution branch of IF evaluates false>
// <1 .. number of arguements: Argument descriptions>
EmitC (GENERAL_IF);
Expect ("(");
numargsptr = cpos;
EmitC (0); // We'll come back and fix this. <numargs>
returnptr = cpos;
EmitD (0); // This too. <elseofs>
// Here we begin the loop to write out IF arguements.
while (1)
{
numargs ++;
excl = 0;
if (NextIs("!"))
{
excl = 1;
GetToken ();
}
EmitOperand (); // First Operand
// Now we need to output the conditional operator, which is a bit more
// complicated by the possibility of zero/nonzero styles.
if (excl)
EmitC (ZERO);
GetToken ();
if (TokenIs("&&") || TokenIs(")"))
{
if (!excl) EmitC (NONZERO);
if (TokenIs("&&")) continue;
break;
}
if (TokenIs("="))
EmitC (EQUALTO);
else if (TokenIs("!="))
EmitC (NOTEQUAL);
else if (TokenIs(">"))
EmitC (GREATERTHAN);
else if (TokenIs(">="))
EmitC (GREATERTHANOREQUAL);
else if (TokenIs("<"))
EmitC (LESSTHAN);
else if (TokenIs("<="))
EmitC (LESSTHANOREQUAL);
else err ("error: Unknown IF relational operator");
EmitOperand (); // Emit the second operand if applicable
GetToken (); // See if there are more arguements
if (TokenIs("&&")) continue;
if (TokenIs(")")) break;
err ("error: &&, AND, or ) expected");
}
// Now that we've parsed the conditional arguements of the IF, go back to
// the IF header and set the correct number of arguements.
buf = cpos;
cpos = numargsptr;
EmitC (numargs);
cpos = buf;
if (NextIs("{")) // It's a compound statement.
{
Expect ("{");
while (HandleExpression());
if (!TokenIs("}")) err ("error: } expected, or unknown identifier");
buf = cpos;
cpos = returnptr;
EmitD (buf - code);
cpos = buf;
return;
}
else // Just a single statement.
{
HandleExpression();
buf = cpos;
cpos = returnptr;
EmitD (buf - code);
cpos = buf;
}
}
ProcessFor0 ()
{
EmitC (FOR_LOOP0);
EmitC (SearchVarList());
Expect (",");
EmitOperand ();
Expect (",");
EmitOperand ();
Expect (",");
if (NextIs("-"))
{
EmitC (0);
GetToken ();
}
else EmitC (1);
EmitOperand ();
Expect (")");
Expect ("{");
while (HandleExpression());
if (!TokenIs("}")) err ("error: } expected, or unknown identifier");
EmitC (ENDSCRIPT);
}
ProcessFor1 ()
{
EmitC (FOR_LOOP1);
EmitC (SearchVarList());
Expect("(");
EmitOperand ();
Expect(")");
Expect (",");
EmitOperand ();
Expect (",");
EmitOperand ();
Expect (",");
if (NextIs("-"))
{
EmitC (0);
GetToken ();
}
else EmitC (1);
EmitOperand ();
Expect (")");
Expect ("{");
while (HandleExpression());
if (!TokenIs("}")) err ("error: } expected, or unknown identifier");
EmitC (ENDSCRIPT);
}
ProcessFor ()
{
Expect ("(");
GetToken ();
if (token_subtype==VAR0) ProcessFor0();
else if (token_subtype==VAR1) ProcessFor1();
else err ("Parse error in FOR loop.");
}
ProcessWhile ()
{ unsigned char numargs=0, excl=0, varidx;
char *buf, *start, *returnptr;
// The WHILE statement is actually pretty easy to do. It basically has an IF
// header, and then at the bottom of the IF processing, is a GOTO back to the
// top. So essentially it will continuously loop until the IF is false.
// It in fact uses an IF opcode.
start=cpos;
EmitC (GENERAL_IF);
Expect ("(");
numargsptr = cpos;
EmitC (0); // We'll come back and fix this. <numargs>
returnptr = cpos;
EmitD (0); // This too. <elseofs>
// Here we begin the loop to write out WHILE arguements.
while (1)
{
numargs ++;
excl = 0;
if (NextIs("!"))
{
excl = 1;
GetToken ();
}
EmitOperand (); // First Operand
// Now we need to output the conditional operator, which is a bit more
// complicated by the possibility of zero/nonzero styles.
if (excl)
EmitC (ZERO);
GetToken ();
if (TokenIs("&&") || TokenIs(")"))
{
if (!excl) EmitC (NONZERO);
if (TokenIs("&&")) continue;
break;
}
if (TokenIs("="))
EmitC (EQUALTO);
else if (TokenIs("!="))
EmitC (NOTEQUAL);
else if (TokenIs(">"))
EmitC (GREATERTHAN);
else if (TokenIs(">="))
EmitC (GREATERTHANOREQUAL);
else if (TokenIs("<"))
EmitC (LESSTHAN);
else if (TokenIs("<="))
EmitC (LESSTHANOREQUAL);
else err ("error: Unknown IF relational operator");
EmitOperand (); // Emit the second operand if applicable
GetToken (); // See if there are more arguements
if (TokenIs("&&")) continue;
if (TokenIs(")")) break;
err ("error: &&, AND, or ) expected");
}
// Now that we've parsed the conditional arguements of the WHILE, go back to
// the WHILE header and set the correct number of arguements.
buf = cpos;
cpos = numargsptr;
EmitC (numargs);
cpos = buf;
if (NextIs("{")) // It's a compound statement.
{
Expect ("{");
while (HandleExpression());
if (!TokenIs("}")) err ("error: } expected, or unknown identifier");
EmitC (GOTO);
EmitD (start - code);
buf = cpos;
cpos = returnptr;
EmitD (buf - code);
cpos = buf;
return;
}
else // Just a single statement.
{
HandleExpression();
EmitC (GOTO);
EmitD (start - code);
buf = cpos;
cpos = returnptr;
EmitD (buf - code);
cpos = buf;
}
}
ProcessSwitch ()
{ char *buf,*retrptr;
// Special thanks for Zeromus for giving me a good idea on how to implement
// this... Even tho I changed his idea around a bit :)
EmitC (SWITCH);
Expect ("(");
EmitOperand ();
Expect (")");
Expect ("{");
// case .. option loop
while (!NextIs("}"))
{
Expect ("CASE");
EmitC (CASE);
EmitOperand ();
Expect (":");
retrptr=cpos;
EmitD (0);
while (!NextIs("CASE") && !NextIs("}")) HandleExpression ();
EmitC (ENDSCRIPT);
buf=cpos;
cpos=retrptr;
EmitD ((int) buf - (int) code);
cpos=buf;
}
Expect ("}");
EmitC (ENDSCRIPT);
}
ProcessGoto ()
{
EmitC (GOTO);
GetToken ();
memcpy (&gotos[numgotos].ident, &token, 40);
if (verbose) printf("GOTO tagged on line %d at cpos %d, label %s. \n", lines, cpos-code, &token);
gotos[numgotos].pos=cpos;
EmitD (0);
numgotos++;
Expect (";");
}
ProcessEvent ()
{
Expect ("{");
inevent = 1;
scriptofstbl[numscripts] = (cpos-code);
numscripts++;
while (1)
{
GetToken ();
if (token_type == CONTROL)