-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
3096 lines (3075 loc) · 95.9 KB
/
parser.cpp
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
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef UNDER_CE
#include <assert.h>
#else
#include <windows.h>
#define assert(x) {if (!(x)) MessageBox(NULL,L"error",L"error",MB_OK);}
#define system(x) 0
#define stricmp _stricmp
#define itoa _itoa
#endif
#include <math.h>
#ifdef WIN32
#define strcasecmp stricmp
#define strncasecmp strnicmp
#define strcasestr strstr
#endif
#if !defined(_MTYPE_H)
#define _MTYPE_H
#include <stdio.h>
#define VAL_LIMIT 128
typedef char Name[37]; //35 limit + @ + NULL
typedef struct _Context {
int nNames;
int lineno;
char **intfs;
int intf;
int cons;
int type;
int task;
unsigned char subs[512];
char val[VAL_LIMIT];
Name names[512];
Name lasmName[512];
} Context;
#define C_VOID 0
#define C_STRING 1
#define C_INTEGER 2
#define C_ARRAY 3
#define C_VARIABLE 4
#define C_FLOAT 5
#define C_BOOL 6
#define C_RESULT 7
#define POWER 'M'
#define MULTIPLY 'N'
#define DIVIDE 'O'
#define MOD 'P'
#define INVERT 'R'
#define ADD 'S'
#define SUBTRACT 'T'
#define AND 'U'
#define ANDAND 'V'
#define OR 'W'
#define OROR 'X'
#define EQL 'Y'
#define NEQL 'Z'
#define GTR 'a'
#define GEQ 'b'
#define LSS 'c'
#define LEQ 'd'
#define INTDIV 'e'
#define SHIFTR '0'
#define SHIFTL '1'
#define UNARYPLUS '2'
#define FLIP '3'
#define XOR '4'
#define NOT '5'
typedef struct _Output {
int nArgs;
char opcode[12];
union {
struct {char arg1[12],arg2[12],arg3[12],arg4[12],arg5[12],arg6[12];};
char line[72];
};
} Output;
typedef struct _Outputs {
int count;
Output code[6000];
} Outputs;
typedef char SCons[128];
SCons arrays[16];
int arrayg[16];
int arrayn[16];
int arrayt[16];
int arraycnt=0;
bool needMask=false;
Outputs code;
static int error(int code);
static int error2(int code, char s[]);
static int error3(int code, int arg);
static void emit(char opcode[], char arg1[],char arg2[],char arg3[],char arg4[],char arg5[],char arg6[]) {
code.code[code.count].nArgs=0;
strcpy(&code.code[code.count].opcode[0],opcode);
strcpy(&code.code[code.count].arg1[0],&arg1[0]);
strcpy(&code.code[code.count].arg2[0],&arg2[0]);
strcpy(&code.code[code.count].arg3[0],&arg3[0]);
strcpy(&code.code[code.count].arg4[0],&arg4[0]);
strcpy(&code.code[code.count].arg5[0],&arg5[0]);
strcpy(&code.code[code.count].arg6[0],&arg6[0]);
if (arg1[0]) code.code[code.count].nArgs++;
if (arg2[0]) code.code[code.count].nArgs++;
if (arg3[0]) code.code[code.count].nArgs++;
if (arg4[0]) code.code[code.count].nArgs++;
if (arg5[0]) code.code[code.count].nArgs++;
if (arg6[0]) code.code[code.count].nArgs++;
code.count++;
}
static void EMITline(char *s) {
code.code[code.count].nArgs=-1;
strcpy(code.code[code.count].line,s);
code.count++;
}
#define UNEMIT() {code.count--;}
#define EMIT(op) emit(op,"","","","","","");
#define EMIT1(op,arg1) emit(op,arg1,"","","","","")
#define EMIT2(op,arg1,arg2) emit(op,arg1,arg2,"","","","")
#define EMIT3(op,arg1,arg2,arg3) emit(op,arg1,arg2,arg3,"","","")
#define EMIT4(op,arg1,arg2,arg3,arg4) emit(op,arg1,arg2,arg3,arg4,"","")
#define EMIT6(op,arg1,arg2,arg3,arg4,arg5,arg6) emit(op,arg1,arg2,arg3,arg4,arg5,arg6)
#endif //_MTYPE_H
#define M_FAIL -1
#define M_NOERR 0
typedef char NM[4];
static NM tasks[10]={"Z0","Z1","Z2","Z3","Z4","Z5","Z6","Z7","Z8","Z9"};
static NM subs[8]={"P0","P1","P2","P3","P4","P5","P6","P7"};
static NM globals[32]={"G0","G1","G2","G3","G4","G5","G6","G7","G8",
"G9","G10","G11","G12","G13","G14","G15","G16","G17","G18","G19","G20","G21","G22","G23","G24",
"G25","G26","G27","G28","G29","G30","G31"};
static NM temps[16]={"$0","$1","$2","$3","$4","$5","$6","$7","$8",
"$9","$10","$11","$12","$13","$14","$15"};
static int tempcnt=1,tempmask=1;
static int globalcnt=1,globalmask=1;
static int labelCnt=1;
static int subCnt=0;
static int taskCnt=0;
static int maxLocal=0;
static int maxTask=0;
static int gbool=0,lbool=0;
static int unioning=-1;
static char tref[8],smax[8]={0};
static int getLabel() {return labelCnt++;}
static char *makeLabel(int i) {
char *s=new char[8];
assert(s!=NULL);
sprintf(s,"s%d",i);
return s;
}
static char *findName(Context *c, char *n) {
int i,j=c->nNames; Name s;
strcpy(s,n);
if (c->task>=0) strcat(s,"@");
for (i=0; i<j; i++) if (strcasecmp(s,c->names[i])==0) return c->lasmName[i]; //local
if (c->task<0) return NULL;
for (i=0; i<j; i++) if (strcasecmp(n,c->names[i])==0) return c->lasmName[i]; //global
return NULL;
}
static int defineName(Context *c, char *n, int type);
static char * getName(Context *c, char *n) {
int i,j=c->nNames;
for (i=0; i<j; i++) if (strcasecmp(n,c->names[i])==0) return c->lasmName[i];
i=defineName(c,n,0);
return c->lasmName[i];
}
static char *getTempN(Context *c, int n=1) {
int i,j,k;
if (unioning>0) i=unioning;
else {
k=(1<<n)-1;
for (i=0; i<tempcnt; i++)
if (((k<<i)&tempmask)==0) {
break;
}
tempmask|=(k<<i);
if (i>=tempcnt) {
tempcnt+=n;
if (tempcnt>=16) {error(106); return NULL;}
}
}
j=c->nNames++;
if ((n>1)&&(c->task==0)) i+=n-1;
strcpy(c->names[j],temps[i]);
strcpy(c->lasmName[j],c->names[j]);
c->lasmName[j][0]=(c->task==0)?'T':'L';
EMIT3(";define",c->names[j],"as",c->lasmName[j]);
assert(temps[i][0]=='$');
if (unioning==0) unioning=i;
return temps[i];
}
#define getTemp(x) getTempN(x)
static void freeTemp(Context *c, char *name) {
int i=atoi(name+1);
assert(name[0]=='$');
assert(i<tempcnt);
assert(tempmask&(1<<i));
tempmask &= ~(1<<i);
}
static int isTemp(char *name) { return name[0]=='$';}
static char *getGlobal(Context *c, int n) {
int i,j;
j=(1<<n)-1;
if (globalmask!=((1<<globalcnt)-1)) {
assert(n==1);
for (i=0; i<globalcnt; i++)
if (((1<<i)&globalmask)==0) {
return globals[i];
}
assert(0);
}
globalmask|=(j<<globalcnt); globalcnt+=n;
if (globalcnt>=32) {error(107); return NULL;}
return globals[globalcnt-n];
}
static int freeName(Context *c, char *name) {
int i,j=c->nNames;
for (i=0; i<j; i++) if (strcasecmp(name,c->names[i])==0) {
c->names[i][0]=0;
return 0;
}
return 1;
}
static void freeLocals(Context *c) {
int i,j=c->nNames;
for (i=0; i<j; i++)
if ((c->lasmName[i][0]=='T')||(c->lasmName[i][0]=='L')) c->names[i][0]=0;
}
static int isArray(char *name) {return name[1]=='A';}
static int isMatrix(char *name) {return (name[1]=='M')||(name[1]=='Y');}
static int isSpecial(char *name) {return (name[0]=='R')||(name[0]=='#');}
static int isParameterMatrix(char *name) {return name[1]=='Y';}
static int isGlobal(char *name) { return name[0]=='G';}
static int isLocal(char *name) { return (name[0]=='L')||(name[0]=='T');}
static int isWritable(char *name) {return (!isArray(name))&&(isGlobal(name)||isLocal(name));}
static int isProcedure(char *name) { return name[0]=='P';}
static int isTask(char *name) { return name[0]=='Z';}
static int isBool(char *name) {return (name[1]=='B')||(name[1]=='X');}
static int isBoolParameter(char *name) {return name[1]=='X';}
static int isFloat(char *name) {return name[1]=='F';}
static int isInteger(char *name) {return !isBool(name)&&!isFloat(name);}
static int defineName(Context *c, char *n, int type) {
int g,i,j,k; char *p,temp[8];
g=(c->task<0)&&(n[0]!='$');
i=c->nNames++;
strcpy(c->names[i],n);
if (c->task>=0) strcat(c->names[i],"@");
if (type>99) {
j=type%100;
type=type/100;
k=type==99999;
if ((j==1)||(type==99999)) type=1;
if (g) {
p=getGlobal(c,type);
if (p==NULL) return 0;
strcpy(temp,p);
} else {
p=getTempN(c,type);
if (p==NULL) return 0;
strcpy(temp,p);
temp[0]=(c->task==0)?'T':'L';
UNEMIT();
}
temp[5]=0; temp[4]=temp[2]; temp[3]=temp[1];
temp[1]=k?'Y':'M';
c->cons=C_ARRAY;
c->type=(j==0)?C_INTEGER:(j==1)?C_BOOL:C_FLOAT;
temp[2]=(j==0)?'I':(j==1)?'B':'F';
strcpy(c->lasmName[i],temp);
EMIT3(";define",n,"as",c->lasmName[i]);
return 1;
}
if ((type==4)||(type==5)||(type==6)) {
if (g) temp[0]='G';
else temp[0]=(c->task==0)?'T':'L';
temp[1]='A';
itoa(arraycnt-1,temp+2,10);
strcpy(c->lasmName[i],temp);
c->cons=C_ARRAY;
c->type=(type==4)?C_INTEGER:(type==5)?C_FLOAT:C_BOOL;;
arrayg[arraycnt-1]=0;
arrayt[arraycnt-1]=c->type;
EMIT3(";define",n,"as",c->lasmName[i]);
return 1;
}
if (type==1) {
temp[1]='B';
if (g) {temp[0]='G'; j=gbool++;}
else {temp[0]=(c->task==0)?'T':'L'; j=lbool++;}
itoa(j,temp+2,10);
assert(j<=14);
strcpy(c->lasmName[i],temp);
} else if (g) {
p=getGlobal(c,1);
if (p==NULL) return 0;
} else {
p=getTemp(c);
if (p==NULL) return 0;
strcpy(temp,p);
p=&temp[0];
p[0]=(c->task==0)?'T':'L';
UNEMIT();
}
if (type==0) strcpy(c->lasmName[i],p);
c->cons=C_VARIABLE;
c->type=C_INTEGER;
if (type==1) c->type=C_BOOL;
if (type==2) {
c->type=C_FLOAT;
c->lasmName[i][0]=p[0]; c->lasmName[i][1]='F';
strcpy(&c->lasmName[i][2],p+1);
}
if (type==3) {
c->type=C_BOOL;
c->lasmName[i][0]=p[0]; c->lasmName[i][1]='X';
strcpy(&c->lasmName[i][2],p+1);
}
EMIT3(";define",n,"as",c->lasmName[i]);
return 1;
}
static void setBooleanBase(char name[], char out[], int *bit) {
int i;
i=atoi(name+2);
*bit=i%100;
if (isGlobal(name)) strcpy(out,"G");
else strcpy(out,"L");
itoa(i/100,out+1,10);
}
#ifndef max
#define max(x,y) ((x)>(y)?(x):(y))
#endif
static int makeTask(Context *c, char *n, int index) {
int i;
for (i=0;i<8;i++) tref[i]=0;
i=c->nNames++;
strcpy(c->names[i],n);
if (index>=10) {error(108); return 0;}
if (lbool>=15) {error(109); return 0;}
strcpy(c->lasmName[i],tasks[index]);
EMIT3(";define",n,"as",c->lasmName[i]);
EMIT1("task",c->lasmName[i]);
return 1;
}
static int makeSub(Context *c, char *n) {
int i;
i=c->nNames++;
strcpy(c->names[i],n);
if (lbool>=15) {error(110); return 0;}
if (subCnt++>=7) {error(111); return 0;}
strcpy(c->lasmName[i],subs[subCnt-1]);
EMIT3(";define",n,"as",c->lasmName[i]);
EMIT1("sub",c->lasmName[i]);
return 1;
}
static void setSub(Context *c, int g, int n, int r, int isFunc) {
c->subs[subCnt*5-5]=isFunc; c->subs[subCnt*5-4]=r; c->subs[subCnt*5-3]=g&0xff; c->subs[subCnt*5-2]=g>>8; c->subs[subCnt*5-1]=n;
}
static int getSub(Context *c, int subIndex, int &g, int &r, int &n) {
r=c->subs[subIndex*5+1]; g=c->subs[subIndex*5+2]|(c->subs[subIndex*5+3]<<8); n=c->subs[subIndex*5+4];
return c->subs[subIndex*5];
}
static int fromString(char *s, int *ii, Context *c) {
int i,j,k,l,line=c->lineno; char ss[26],*ps;
i=strspn(s+*ii," \n\r\t ");
if (i>0) *ii+=i;
if ((s[*ii]=='"')||(s[*ii]=='\''))
{
i=s[*ii];
*ii+=1; l=1; c->val[0]='C';
for (j=0;s[*ii];j++) {
if (s[*ii]==i) {
*ii+=1; c->val[l]=0; c->cons=C_STRING; c->type=C_STRING;
if (l==2) {c->cons=C_INTEGER; c->type=C_INTEGER; itoa(c->val[1],c->val+1,10); c->val[0]='N'; }
return M_NOERR;
} else if (s[*ii]=='\\') {
*ii+=1;
if (s[*ii]=='n') k='\n';
else if (s[*ii]=='r') k='\r';
else if (s[*ii]=='t') k='\t';
else if (s[*ii]=='\\') k='\\';
else if (s[*ii]=='\'') k='\'';
else if (s[*ii]=='"') k='"';
else if (s[*ii]=='0') {
k=strspn(s+*ii,"01234567");
strncpy(ss,s+*ii,k);
ss[k]=0; *ii+=k-1;
sscanf(ss,"%lo",&k);
if (k==0) break;
}
else break;
c->val[l]=k;
} else {
c->val[l]=s[*ii];
if (s[*ii]=='\n') (c->lineno)++;
}
*ii+=1; l++;
}
return error(112);
}
// Number
k=0;
if ((s[*ii]=='0')&&(toupper(s[*ii+1])=='X')) i=strspn(s+*ii,"0123456789abcdefABCDEFxX");
else {
i=0;
if (s[*ii]=='+') *ii+=1;
else if (s[*ii]=='-') i=1;
i+=strspn(s+*ii+i,"0123456789.");
}
*ii+=i;
if (i==0) return error(113);
j=0;
*ii=*ii-i-j-k;
strncpy(ss,s+*ii,i+j+k);
ss[i+j+k]=0;
*ii=*ii+i+j+k;
if ((ss[0]=='0')&&(ss[1]!='.')) {
if (toupper(ss[1])!='X') {
i=strspn(ss,"01234567");
if (i!=(int)strlen(ss)) return error(114);
sscanf(ss,"%lo",&k);
} else sscanf(ss,"%lx",&k);
zz: //if ((s[*ii]=='l')||(s[*ii]=='L')) *ii+=1;
c->cons=C_INTEGER; c->type=C_INTEGER; c->val[0]='N'; strcpy(&c->val[1],itoa(k,ss,10));
} else if ((ps=strchr(ss,'.'))==NULL) {
sscanf(ss, "%ld", &k); goto zz;
} else {
if (strchr(ps+1,'.')) return error(115);
if ((s[*ii]=='e')||(s[*ii]=='E')) {
i=*ii; *ii+=1;
if ((s[*ii]=='+')||(s[*ii]=='-')) *ii+=1;
j=strspn(s+*ii,"0123456789"); *ii+=j;
if (j==0) return error(116);
strncat(ss,s+i,*ii-i);
}
if ((s[*ii]=='f')||(s[*ii]=='F')) *ii+=1;
c->cons=C_FLOAT; c->type=C_FLOAT; c->val[0]='F'; strcpy(&c->val[1],ss);
}
return M_NOERR;
}
static int expr(char *s, int *ii, int ls, Context *c);
static int state(char *s, int *ii, int ls, Context *lineno);
static int linestart=0;
static void L(char *s, int end) {
char line[512];
if (end<=linestart) return;
line[0]=';';
strncpy(line+1,s+linestart,end-linestart); line[end-linestart+1]=0;
if ((end<=(linestart+2))&&(strspn(line+1," \t\n\r")==strlen(line+1)))
return;
EMITline(line);
}
static int W(char *s, int *ii, int ls, int *lineno) {
for (;*ii<ls;) {
//Ignore all of the folowing "\n\r\t " and space and move to next i
while ((*ii<ls)&&((s[*ii]==' ')||(s[*ii]=='\t')||(s[*ii]=='\r')||(s[*ii]=='\n'))) {
if (s[*ii]=='\n') {(*lineno)++; L(s,*ii); linestart=*ii+1;}
*ii+=1;
}
//ignore comments starting with '//'
if ((*ii<ls)&&(s[*ii]=='/')&&(s[*ii+1]=='/'))
{
while ((*ii<ls)&&(s[*ii]!='\n')&&(s[*ii]!='\r')) *ii+=1;
continue;
}
if ((*ii<ls)&&(s[*ii]=='/')&&(s[*ii+1]=='*')) {
*ii+=2;
for (;;) {
while ((*ii<ls)&&(s[*ii]!='\n')&&(s[*ii]!='*')) *ii+=1;
if (s[*ii]=='\n') {(*lineno)++; L(s,*ii); linestart=*ii+1;}
else if (s[*ii]!='*') return error(7);
else if (s[(*ii)+1]=='/') {*ii+=2; break;}
*ii+=1;
}
} else break;
}
return 0;
}
static char *ftoa(float z,char *s,int x) {
sprintf(s,"%g",z);
return s;
}
static int pow2(char *s) {
int i=atoi(s+1),j,k=1;
assert(i>=0);
for (j=0; j<=15; j++,k+=k)
if (k==i) return j;
return 0;
}
static int isNumber(int cons) {
return (cons==C_INTEGER)||(cons==C_FLOAT);
}
static int isFloat(int cons) {
return (cons==C_FLOAT);
}
static int isInteger(int cons) {
return (cons==C_INTEGER);
}
static int isBool(int cons) {
return (cons==C_BOOL);
}
static int iscConstant(Context *c) {
return isNumber(c->cons)||isBool(c->cons);
}
static bool iscConstantTrue(Context *c) {
float f=(float)atof(c->val+1);
return f!=0.0f;
}
static int isVariable(int cons) {
return (cons==C_VARIABLE);
}
static int isReadable(Context *c) {
if (c->cons!=C_VOID) return true;
return false;
}
static int iscBool(Context *c) {
if (c->cons==C_BOOL) return true;
if (( (c->cons==C_VARIABLE)||(c->cons==C_RESULT))&&(c->type==C_BOOL)) return true;
return false;
}
static int iscBoolParameter(Context *c) {
if (c->cons==C_BOOL) return false;
if ((c->cons==C_VARIABLE)&&(c->type==C_BOOL)&&(c->val[1]=='X')) return true;
return false;
}
static int iscInteger(Context *c) {
if (c->cons==C_INTEGER) return true;
if (( (c->cons==C_VARIABLE)||(c->cons==C_RESULT))&&(c->type==C_INTEGER)) return true;
return false;
}
static int iscFloat(Context *c) {
if (c->cons==C_FLOAT) return true;
if (( (c->cons==C_VARIABLE)||(c->cons==C_RESULT))&&(c->type==C_FLOAT)) return true;
return false;
}
static void incs(char *s, int n) {
float i=(float)atof(s+1);
if (isInteger(s)) ftoa(i+1.0f*(float)n,s+1,10);
else ftoa(i+0.1f*(float)n,s+1,10);
}
static void inc(Context *s, int n) { //inc to next higher/lower value to switch <= to <
float i=(float)atof(s->val+1);
if (isInteger(s->cons)) ftoa(i+1.0f*(float)n,s->val+1,10);
else ftoa(i+0.1f*(float)n,s->val+1,10);
}
static int factorx(char *s, int *ii, int ls, Context *c,int *n);
static int factor(char *s, int *ii, int ls, Context *c) {
int i,n;
i=factorx(s,ii,ls,c,&n);
//if ((n<-2)||(n==-1)) return M_FAIL;
if (n<=0) return i;
assert(0);//outint(out,index,n-1);
//out[(*index)++]=ARRAY;
return i;
}
static void getArray(int ai, int sub, char out[]) {
char *s=arrays[ai],*t;
for (;;) {
t=strchr(s,',');
sub--;
if (sub<0) break;
s=t+1;
}
if (t==NULL) strcpy(out,s);
else {
strncpy(out,s,t-s);
out[t-s]=0;
}
}
static int doProcedure(Context *c, char *s, int &i,int ls, int &f, char *js) {int j,k,m,r;
char name[8];
if (c->task!=0) return error(117);
if (W(s,&i,ls,&c->lineno)) return M_FAIL;
if (s[i++]!='(') return error(118);
j=atoi(js+1);
f=getSub(c,j,j,r,k);
for (m=0;;m++) {int n;
if (W(s,&i,ls,&c->lineno)) return M_FAIL;
if (s[i]==')') {i++; break;}
if (s[i]==',') {
if (m==0) return error(119);
i++;
}
n=expr(s,&i,ls,c);
if (n!=M_NOERR) return n;
name[0]='L';
itoa(m+1,name+1,10);
n=j&3; j=j>>2;
if (m>k) return error3(120,m+1);
if (r&(1<<m)) {
if (!isMatrix(c->val)) return error3(121,m+1);
if ((n==0)&&(c->type!=C_INTEGER)) return error3(122,m+1);
else if ((n==2)&&(c->type!=C_FLOAT)) return error3(123,m+1);
strcpy(c->val+1,c->val+3);
EMIT2("lea",name,c->val);
continue;
}
if ((n==0)&&!iscInteger(c)) return error3(124,m+1);
if ((n==2)&&!iscFloat(c)) return error3(125,m+1);
if ((n==3)&&!iscBool(c)) return error3(126,m+1);
if (isTemp(c->val)) {
freeTemp(c,c->val); strcpy(c->val,getName(c,c->val));
EMIT2("setv",name,c->val);
} else {
if (iscBool(c)&&!iscConstant(c)) {char temp4[8],temp5[8]; int mm;
setBooleanBase(c->val,temp5,&mm);
temp4[0]='N'; itoa((1<<mm),temp4+1,10);
EMIT2("setv",name,temp5);
EMIT2("andv",name,temp4);
EMIT2("divv",name,temp4);
} else EMIT2("setv",name,c->val);
}
}
if (m!=k) return error3(127,k+1);
tref[atoi(js+1)]++;
EMIT1("calls",js);
return M_NOERR;
}
static int factorx(char *s, int *ii, int ls, Context *c,int *n)
{
int failure=M_FAIL; char *js;
int k,m,i,f,modifier=0;
*n=0;
if (W(s,ii,ls,&c->lineno)) return M_FAIL;
i=*ii;
if ((s[i]=='+')&&(s[i+1]=='+')) {modifier=1; i+=2; goto zz;}
if ((s[i]=='-')&&(s[i+1]=='-')) {modifier=2; i+=2; goto zz;}
//test the following and send to fromstring to interpret
if ( (s[i]=='+')||(s[i]=='-')||(s[i]=='\'')||
(s[i]=='"')||((s[i]>='0') && (s[i] <= '9')) )
{
failure=fromString(s,ii,c);
return failure;
}
zz:
if (((s[i]>='a') && (s[i] <= 'z'))||((s[i]>='A') && (s[i] <= 'Z'))) { char name[36]; char *ps,*pps;
m=strspn(s+i+1,"abcdefg_hijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
strncpy(name,s+i++,m+1); name[m+1]=0;
if (m>35) return error2(128,name);
if (m>0) i+=m;
*ii=i;
if (strcmp(name,"true")==0) {
if (modifier) return error(129);
c->cons=C_BOOL; c->type=C_BOOL;
strcpy(c->val,"N1");
return M_NOERR;
}
if (strcmp(name,"false")==0) {
if (modifier) return error(130);
c->cons=C_BOOL; c->type=C_BOOL;
strcpy(c->val,"N0");
return M_NOERR;
}
if (strcmp(name,"rand")==0) {
if (modifier) return error(131);
if (W(s,ii,ls,&c->lineno)) return M_FAIL;
if (s[*ii]!='(') return error(132);
*ii+=1;
if (W(s,ii,ls,&c->lineno)) return M_FAIL;
if (s[*ii]!=')') return error(133);
*ii+=1;
c->cons=C_RESULT; c->type=C_INTEGER;
strcpy(c->val,"R0");
return M_NOERR;
}
if (strcmp(name,"event")==0) {
if (modifier) return M_FAIL;
if (W(s,ii,ls,&c->lineno)) return M_FAIL;
if (s[*ii]!='(') return error(134);
*ii+=1;
failure = expr (s,ii,ls,c);
if ( failure!=M_NOERR ) return failure;
if (c->cons!=C_INTEGER) return error(135);
if (W(s,ii,ls,&c->lineno)) return M_FAIL;
if (s[*ii]!=',') return error(136);
*ii+=1;
m=atoi(c->val+1);
if ((m<0)||(m>5)) return error3(137,m);
failure = expr (s,ii,ls,c);
if ( failure!=M_NOERR ) return failure;
if (c->cons!=C_INTEGER) return error(138);
f=atoi(c->val+1);
if ((f<0)||(f>15)) return error3(139,f);
pps=getTemp(c);
if (pps==NULL) return M_FAIL;
strcpy(c->val,pps);
pps=getName(c,c->val);
if (s[*ii]!=')') return error(140);
*ii+=1;
sprintf(name,"E%2d,%2d",(m==0)?25:(m+26),f);
EMIT2("setv",pps,name);
c->cons=C_RESULT; c->type=C_INTEGER;
return M_NOERR;
}
if ((strcmp(name,"abs")==0)||(strcmp(name,"sign")==0)||(strcmp(name,"int")==0)||(strcmp(name,"bool")==0)||
(strcmp(name,"float")==0)||(strcmp(name,"castf")==0)||(strcmp(name,"casti")==0)) {
if (modifier) return error2(141,name);
if (W(s,ii,ls,&c->lineno)) return M_FAIL;
if (s[*ii]!='(') return error2(142,name);
failure = factorx (s,ii,ls,c,n);
if ( failure!=M_NOERR ) return failure;
if (isNumber(c->cons)) {float z;
z=(float)atof(c->val+1);
if (name[0]=='a') z=(z>0)?z:-z;
if (name[0]=='b') {z=(z!=0)?1.0f:0.0f; c->val[0]='N'; c->type=C_BOOL; c->cons=C_BOOL;}
if (name[0]=='i') {z=(float)(int)z; c->val[0]='N'; c->type=C_INTEGER; c->cons=C_INTEGER;}
if (name[0]=='f') {c->val[0]='F'; c->type=C_FLOAT; c->cons=C_FLOAT;}
if (name[0]=='s') z=(float)((z==0)?0: ((z>0)?1:-1));
if (name[0]=='c') {
return error2(143,name);
}
ftoa(z,c->val+1,10);
return M_NOERR;
}
if (isTemp(c->val)) {
ps=c->val;
pps=getName(c,c->val);
k=c->type;
if (name[0]=='a') EMIT2("absv",pps,pps);
else if (name[0]=='b') {
c->type=C_BOOL; goto bo;
} else if (name[0]=='i') {
c->type=C_INTEGER;
bo: if (isInteger(k)||isBool(k)) return M_NOERR;
if (!isFloat(k)) return error(144);
EMIT2("divv",pps,"N10");
} else if (name[0]=='f') {
if (iscFloat(c)) return M_NOERR;
if (!iscInteger(c)) return error(145);
EMIT2("mulv",pps,"N10");
c->type=C_FLOAT;
} else if (name[0]=='c') {
//m=(name[4]=='i')?C_FLOAT:C_INTEGER;
//if (c->type!=m) return M_FAIL;
c->type=(name[4]=='i')?C_INTEGER:C_FLOAT;
return M_NOERR;
} else EMIT2("sgnv",pps,pps);
} else {
ps=getTemp(c);
if (ps==NULL) return M_FAIL;
pps=getName(c,ps);
k=c->type;
if (name[0]=='a') EMIT2("absv",pps,c->val);
else if (name[0]=='b') {
c->type=C_BOOL; goto boo; //TODO absv( sgnv() )
} else if (name[0]=='i') {
c->type=C_INTEGER;
boo: if (isInteger(k)||isBool(k)) return M_NOERR;
if (!isFloat(k)) return error(144);
EMIT2("setv",pps,c->val);
EMIT2("divv",pps,"N10");
} else if (name[0]=='c') {
EMIT2("setv",pps,c->val);
m=(name[4]=='i')?C_FLOAT:C_INTEGER;
if (c->type!=m) return error(146);
c->type=(name[4]=='i')?C_INTEGER:C_FLOAT;
} else if (name[0]=='f') {
if (iscFloat(c)) return M_NOERR;
if (!iscInteger(c)) return error(145);
EMIT2("setv",pps,c->val);
EMIT2("mulv",pps,"N10");
c->type=C_FLOAT;
} else {
EMIT2("sgnv",pps,c->val);
if (iscFloat(c)) EMIT2("mulv",pps,"N10");
}
}
c->cons=C_RESULT;
strcpy(c->val,ps);
return M_NOERR;
}
if ((js=findName(c,name))==NULL) return error2(147,name);
if (isProcedure(js)) {
if (modifier) return error2(148,name);
i=*ii;
failure=doProcedure(c,s,i,ls,f,js);
*ii=i;
if (failure!=M_NOERR) return failure;
if (f==0) return error2(149,name);
arr: js=getTemp(c);
if (js==NULL) return M_FAIL;
strcpy(name,getName(c,js));
strcpy(c->val,js);
c->cons=C_RESULT;
if (f==1) {EMIT2("setv",name,"L1"); c->type=C_INTEGER;}
if (f==2) {EMIT2("setv",name,"L1"); c->type=C_BOOL;}
if (f==3) {EMIT2("setv",name,"LF1"); c->type=C_FLOAT;}
return M_NOERR;
} else {
if (c->cons==C_VOID) {
if (isMatrix(js)) {char *p,*q; int z;
if (s[*ii]!='[') {
if (modifier!=0) return error2(150,name);
if (isParameterMatrix(js)) return error2(151,name);
if (js[2]=='B') return error2(152,name);
c->cons=C_ARRAY;
c->type=(js[2]=='I')?C_INTEGER:C_FLOAT;
strcpy(c->val,js);
return M_NOERR;
}
*ii+=1;
f=atoi(js+3); m=js[2]; z=js[1];
name[0]=js[0]; name[1]=0; strcat(name,js+3);
k=expr(s, ii, ls, c);
if (k!=M_NOERR) return M_FAIL;
if (s[*ii]!=']') return error(153);
*ii+=1;
if (!isInteger(c->val)) return error(154);
if (c->cons!=C_INTEGER) {
if (isTemp(c->val)) {
p=getName(c,c->val);
} else {
js=getTemp(c);
if (js==NULL) return M_FAIL;
p=getName(c,js);
EMIT2("setv",p,c->val);
strcpy(c->val,js);
}
if (z=='Y') goto moo;
if (m=='B') {
if (modifier!=0) return error(155);
needMask=true;
if (c->task!=0) return error(156);
EMIT2("setv","L1","N-1");
EMIT2("setv","L2",p);
EMIT1("calls"," 7");
EMIT2("setv",p,name);
EMIT2("andv",p,"L1");
EMIT2("divv",p,"L1");
} else {
EMIT2("addav",p,name);
EMIT1("indv",p);
}
mo: c->cons=C_RESULT;
c->type=(m=='I')?C_INTEGER:(m=='F')?C_FLOAT:C_BOOL;
if (modifier) {char *j;
mooo: UNEMIT();
j=getTemp(c);
if (j==NULL) return M_FAIL;
q=getName(c,j);
EMIT3("set",q,"36",p);
EMIT2( (modifier&1)?"sumv":"subv",q,(m=='F')?"F1":"N1");
EMIT2("sindv",p,q);
if (modifier>2) EMIT2( (modifier==3)?"subv":"sumv",q,(m=='F')?"F1":"N1");
freeTemp(c,js);
strcpy(c->val,j);
} else {
if ((s[*ii]=='+')&&(s[*ii+1]=='+')) {*ii+=2; modifier=3; goto mooo;}
if ((s[*ii]=='-')&&(s[*ii+1]=='-')) {*ii+=2; modifier=4; goto mooo;}
}
return M_NOERR;
}
k=atoi(c->val+1);
if (k<0) return error3(157,k);
if (isParameterMatrix(js)) {
js=getTemp(c);
if (js==NULL) return M_FAIL;
p=getName(c,js);
if (k==0) {
if (modifier) {
EMIT2("setv",p,name);
EMIT1("indv",p);
} else EMIT3("set",p,"36",name);
} else {
EMIT2("setv",p,c->val);
moo: EMIT2("sumv",p,name);
EMIT1("indv",p);
}
strcpy(c->val,js);
goto mo;
}
if (m!='B') {
f+=(name[0]!='T')?k:-k;
itoa(f,name+1+(m=='F'),10);
} else {
if (k>=15) return error3(158,k);
itoa(f*100+k,name+2,10);
}
if (m!='I') name[1]=m;
js=&name[0];
}
c->cons=C_VARIABLE;
c->type=C_INTEGER;
if (isBool(js)) c->type=C_BOOL;
if (isFloat(js)) c->type=C_FLOAT;
if (isArray(js)) {
if (modifier) return error(159);
m=atoi(js+2);
if (s[*ii]!='[') return error(160);
*ii+=1;
failure=expr(s,ii,ls,c);
if (failure!=M_NOERR) return failure;
if (s[*ii]!=']') return error(153);
*ii+=1;
if (!isInteger(c->cons)) {
if (!iscInteger(c)) return error(154);
if (isTemp(c->val)) {
freeTemp(c,c->val); strcpy(c->val,getName(c,c->val));
}
if (c->task!=0) return error(161);
EMIT3("setv","L1","2",js+2);
EMIT2("setv","L2",c->val);
EMIT1("calls","7");
arrayg[m]=1;
f=(arrayt[m]==C_INTEGER)?1:(arrayt[m]==C_FLOAT)?3:2;
goto arr;
}
k=atoi(c->val+1);
if ((k<0)||(k>=arrayn[m])) return error3(162,arrayn[m]);
c->cons=arrayt[m];
c->type=arrayt[m];
getArray(m,k,c->val);
return M_NOERR;
}
if ((s[*ii]=='+')&&(s[*ii+1]=='+')) {*ii+=2; modifier=3;}
else if ((s[*ii]=='-')&&(s[*ii+1]=='-')) {*ii+=2; modifier=4;}
if (modifier&&(!isWritable(js)||isBool(js))) return error(163);
if (modifier==1) EMIT2("sumv",js,isFloat(js)?"F1":"N1");
if (modifier==2) EMIT2(
"sumv",js,isFloat(js)?"F-1":"N-1");
if (modifier==3) {strcpy(c->val,js);strcat(c->val,"+");}
else if (modifier==4) {
strcpy(c->val,js);strcat(c->val,"-");}
else strcpy(c->val,js);
} else return error(164);
}
return M_NOERR;
} else if (s[i]!='(') return error(165);
if (modifier) return error(166);
*ii=i+1;
failure = expr (s,ii,ls,c);
if ( failure!=M_NOERR ) return failure;
if (W(s,ii,ls,&c->lineno)) return M_FAIL;
if (s[*ii]!=')') return error(167);
*ii+=1;
if (W(s,ii,ls,&c->lineno)) return M_FAIL;
if (s[*ii]=='?') {int j; char temp4[4],*ps,*pps,temp5[8];
*ii+=1;
if (isTemp(c->val)) {freeTemp(c,c->val); strcpy(c->val,getName(c,c->val));}
else if (iscBool(c)&&!iscBoolParameter(c)) {
setBooleanBase(c->val,temp4,&m);
ps=getTemp(c);
if (ps==NULL) return M_FAIL;
strcpy(c->val,pps=getName(c,ps));
EMIT2("setv",pps,temp4);
temp5[0]='N'; itoa((1<<m),temp5+1,10);
EMIT2("andv",pps,temp5);
freeTemp(c,ps);
}
j=getLabel()*10000+getLabel();
EMIT4("chkl",c->val,"3",iscFloat(c)?"F0":"N0",makeLabel(j/10000));
failure = expr (s,ii,ls,c);
if ( failure!=M_NOERR ) return failure;
if (W(s,ii,ls,&c->lineno)) return M_FAIL;
if (s[*ii]!=':') return error(168);
*ii+=1;
i=c->type;
if (isTemp(c->val)) {ps=c->val; strcpy(temp5,getName(c,c->val));}
else {
ps=getTemp(c);
if (ps==NULL) return M_FAIL;
strcpy(temp5,getName(c,ps));
if (iscBool(c)&&!iscBoolParameter(c)) {
setBooleanBase(c->val,temp4,&m);
EMIT2("setv",temp5,temp4);
temp4[0]='N'; itoa((1<<m),temp4+1,10);
EMIT2("andv",temp5,temp4);
EMIT2("divv",temp5,temp4);
} else EMIT2("setv",temp5,c->val);
}
EMIT1("jmpl",makeLabel(j%10000));
EMIT(strcat(makeLabel(j/10000),":"));
failure = expr (s,ii,ls,c);
if ( failure!=M_NOERR ) return failure;
if (i!=c->type) return error(169);
if (W(s,ii,ls,&c->lineno)) return M_FAIL;
if (isTemp(c->val)) {freeTemp(c,c->val); strcpy(c->val,getName(c,c->val));}
if (iscBool(c)&&!iscBoolParameter(c)) {
setBooleanBase(c->val,temp4,&m);
EMIT2("setv",temp5,temp4);
temp4[0]='N'; itoa((1<<m),temp4+1,10);
EMIT2("andv",temp5,temp4);
EMIT2("divv",temp5,temp4);
} else EMIT2("setv",temp5,c->val);
EMIT(strcat(makeLabel(j%10000),":"));
c->cons=C_RESULT;
strcpy(c->val,ps);
}
return M_NOERR;