-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.c
1577 lines (1445 loc) · 39.8 KB
/
core.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
/**
* Copy Right:: Freely we received , freely do we give. By Emeka
* Print Statement, can take multiple data
* 5 PRINT "Enter your test number"
* Read data from the stdin, takes only one argument
* 10 INPUT L
* Assignment statement
* 15 LET D = 100
* Unconditional jump
* 17 GOTO 45
* 20 LET N = "Coder, you are welcome! "
* 30 LET M = "Hello Mr "
* 35 PRINT M, N
* Print statement, however when without argument prints new line
* 38 PRINT
* 40 PRINT L
* 43 PRINT
* Conditional jump
* 45 IF L > 30 THEN 130
* Subroutine jumps to the label, however when it encounters return statement
* It would return to the next statement after GoSub
* 50 GOSUB 90
* 60 PRINT "Good Bye"
* 65 PRINT
* 70 LET D = 100
* 80 PRINT "MIRACLE SEEDS"
* 83 PRINT
* 85 GOTO 140
* 90 LET N = "Satapotus"
* 100 LET M = " Ugunda "
* 110 PRINT N, M
* 115 PRINT
* 120 PRINT L
* 125 RETURN
* 130 PRINT "This is your day!"
* 135 PRINT
* 140 PRINT "GAME OVER!"
* 150 PRINT
* For Loop is just nice-to-have because goto and if statement could easily
* simulate it. However if STEP is missing then it would increment by 1
* When it encounters Next statement , it would increment the loop number and jump back to
* the beginning of the FOR statement. There is a bug which means that FOR statement should
* not be the last statement.
* 160 FOR I = 1 TO 7 STEP 3
* 170 PRINT I
* 180 PRINT
* 190 NEXT I
* 195 PRINT
* 200 PRINT "DONE"
*
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <time.h>
#include "decl.h"
static Clist* head, *tail,*stepback, *currenttoken, *doubleback;
env* henv = NULL;
env* tenv = NULL;
//This is the base for env
env** foo = NULL;
ReturnStack* rstack = NULL;
struct labelList* llst;
struct labels* hlabels = 0;
struct labels* tlabels = 0;
FuncAbstraction* funcTable[FN_SIZE];
CStatement* hstat = NULL;
CStatement* tstat = NULL;
CStatement* currentstat = NULL;
CStatement* pc = NULL;
CSave hfor[5] ;
CSFor fsfor[5];
int fc = 0;
char* reservedword[] = { "DIM", "FOR", "PRINT", "DEF", "IF","READ","INPUT","NEXT", "RETURN","STOP", "END", "GOTO", "GOSUB", "THEN", "LET", "DATA", "TO", "STEP", "REM",NULL};
char getoptype (char cop){
int i;
for(i = 0; i < aroptokenlen; i++){
if(cop == aroptoken[i].cop)
return aroptoken[i].nop;
}
return '\0';
}
void cerror (void* obj, char* message, const char* name, int line){
if(!obj){
fprintf(stderr, "%s\ninside function %s \nand line %d\n", message, name, line);
exit(0);
}
}
void insertlabel(char* label){
if(currentpos < dim){
strcpy(tknlb[currentpos++], (const char*)label);
}
return;
}
int contains(char* label){
int i = 0;
while(i < currentpos){
if(strcmp((const char*)label, (const char*)tknlb[i] ) == 0){
return 1;
}
i++;
}
return 0;
}
int findTableIndex(char* name){
int idx = fntabidx;
for(int i = 0; i < idx; i++){
if(strcmp(name, funcTable[i]->name) == 0){
return i;
}
}
return -1;
}
void addFunc(FuncAbstraction* fun){
if(fntabidx == FN_SIZE){
cerror(0,"ERROR---You have reached the maximum function size 100", __func__, __LINE__ );
}
fntabidx++;
funcTable[fntabidx] = fun;
return;
}
struct CExpression* makefunCall(char* name, LExpression* Lexp, int nsize){
FuncCall* fncall;
struct CExpression* expcall;
fncall = malloc(sizeof(FuncCall));
int idx;
if(!fncall){
cerror(0,"ERROR---Not enough space to create FuncCall Object", __func__, __LINE__ );
}
fncall->LexpList = Lexp;
fncall->name = name;
fncall->argSize = nsize;
idx = findTableIndex(name);
fncall->tableIndex = idx;
expcall = malloc(sizeof(struct CExpression*));
if(!expcall){
cerror(0,"ERROR---Not enough space to create Expression FuncCall Object" , __func__, __LINE__ );
}
expcall->extype = V_call;
expcall->funCall = fncall;
return expcall;
}
struct CExpression* makenumval(double val){
CNumberValue* dval;
CValue* result;
struct CExpression* rstexpress;
dval= malloc(sizeof(CNumberValue));
result = malloc(sizeof(CValue));
rstexpress = (struct CExpression* ) malloc(sizeof(struct CExpression*));
if(!dval || !dval || !result){
cerror(0,"ERROR---Not enough space", __func__, __LINE__ );
}
dval->value = val;
result->vtype = NumberValue;
result->value = dval;
rstexpress->extype = V_value;
rstexpress->valueexpression = result;
return rstexpress;
}
struct CExpression* makestrval(char* str){
CStringValue* sval;
CValue* result;
struct CExpression* rstexpress;
sval = malloc(sizeof(CStringValue));
result = malloc(sizeof(CValue));
rstexpress = (struct CExpression*)malloc(sizeof(struct CExpression));
if(!sval || !sval || !rstexpress){
cerror(0,"ERROR---Not enough space", __func__, __LINE__ );
}
sval->value = str;
result->value = sval;
result->vtype = StringValue;
rstexpress->extype = V_value;
rstexpress->valueexpression = result;
return rstexpress;
}
struct CExpression* makeoperatorexpress(struct CExpression* left, int op, struct CExpression* right){
struct COperatorExpression* cop;
struct CExpression* result;
cop = malloc(sizeof(struct COperatorExpression));
result = malloc(sizeof(struct CExpression));
if(!cop || !result){
cerror(0,"ERROR---Not enough space", __func__, __LINE__ );
}
cop->left = left;
cop->right = right;
cop->coperator = op;
result->extype = V_operator;
result->operatorexpression = cop;
return result;
}
struct CExpression* makevarexpression(char* text){
struct CVariableExpression* temp;
struct CExpression* result;
temp = malloc(sizeof(struct CVariableExpression));
result = malloc(sizeof(struct CExpression));
if(!temp || !result ){
cerror(0, "ERROR---Not enough space" , __func__, __LINE__);
}
temp->name = text;
result->extype = V_expression;
result->variableexpression = temp;
return result;
}
void addstat(void* statf, int stype,int ssize,char* labelnumber,int sty){
int lnum = (int)strtol(labelnumber,(char**) NULL, 10);;
Clabels* clb = malloc(sizeof(Clabels));
cerror(clb, "ERROR--not enough memory to create Clabels" , __func__, __LINE__);
if(sty){
clb->key = lnum;
clb->cindex = ssize;
clb->st = NULL;
if(llst->top - 1 < STAT_MAX){
llst->clabs[llst->top++] = clb;
}
return;
}
CStatement* ttstat = malloc(sizeof(CStatement));
cerror(ttstat, "ERROR--not enough memory" , __func__, __LINE__);
ttstat->statement = statf;
ttstat->stype = stype;
ttstat->next = NULL;
clb->key = lnum;
clb->cindex = ssize;
clb->st = ttstat;
if(llst->top - 1 < STAT_MAX){
llst->clabs[llst->top++] = clb;
}
if(!tstat){
hstat = ttstat;
tstat = ttstat;
}
else{
tstat->next = ttstat;
tstat = ttstat;
}
return;
}
void jumpto(int jmp , int test){
if (jmp < 0){
for(int i = 0; i < llst->top; i++){
if(llst->clabs[i]->cindex == test){
pc = llst->clabs[i]->st;
return;
}
}
}
else {
for(int i = 0; i < llst->top; i++){
if(llst->clabs[i]->key == jmp){
pc = test? llst->clabs[i + 1]->st :llst->clabs[i]->st ;
return;
}
}
}
cerror(0, "The label to jump to is not in your code" , __func__, __LINE__);
}
int match(int tkntype){
if(!currenttoken)
return 0;
if(currenttoken->type != tkntype)
return 0;
doubleback = stepback;
stepback = currenttoken;
currenttoken = currenttoken->next;
return 1;
}
int matchtypes(int tpa, int tpb){
Clist* temp,*ctemp;
if(!currenttoken)
return 0;
if(currenttoken->type != tpa)
return 0;
ctemp = currenttoken;
temp = currenttoken->next;
if(temp->type != tpb)
return 0;
doubleback = ctemp;
stepback = temp;
currenttoken = temp->next;
return 1;
}
int matchstring(char* str){
if(!currenttoken)
return 0;
if(currenttoken->type != TT_WORD)
return 0;
if(strcmp(currenttoken->text, str) != 0)
return 0;
doubleback = stepback;
stepback = currenttoken;
currenttoken = currenttoken->next;
return 1;
}
CIfThenStatement* makeifthenstat(struct CExpression* exp, char* label){
CIfThenStatement* ifstat = malloc(sizeof(CIfThenStatement));
cerror(ifstat,"ERROR-not enough memory to create if statement object", __func__, __LINE__ );
ifstat->condition = exp;
ifstat->label = label;
return ifstat;
}
CGotoStatement* makegotostat(char* label){
CGotoStatement* gotostat = malloc(sizeof(CGotoStatement));
cerror(gotostat, "ERROR-not enough memory to create goto object" , __func__, __LINE__);
gotostat->label = label;
gotostat->nlab = (int)strtol(label, (char**)NULL, 10);
return gotostat;
}
CGosubStatement* makegosubstat(int jmp, int lbnum){
CGosubStatement* gosubstat = malloc(sizeof(CGosubStatement));
cerror(gosubstat, "ERROR-not enough memory to create gosub object" , __func__, __LINE__);
gosubstat->label = lbnum;
gosubstat->jumpto = jmp;
return gosubstat;
}
CAssignStatement* makeassignstat(char* name, struct CExpression* value){
CAssignStatement* assignstat = malloc(sizeof(CAssignStatement));
cerror(assignstat, "ERROR-not enough memory assignment" , __func__, __LINE__);
assignstat->name = name;
assignstat->value = value;
return assignstat;
}
CArrayStatement* makearraystat(char* name, struct CExpression* value, struct CExpression* valopt){
CArrayStatement* arraystat = malloc(sizeof(CArrayStatement));
cerror(arraystat, "ERROR-not enough memory array object" , __func__, __LINE__);
arraystat->varname = name;
arraystat->exp = value;
arraystat->opt = valopt;
return arraystat;
}
CInputStatement* inputstat(char* name){
CInputStatement* instat = malloc(sizeof(CInputStatement));
cerror(instat, "ERROR-not enough memory Input Statement object" , __func__, __LINE__);
instat->name = name;
return instat;
}
CPrintStatement* printstat(struct LExpression* exp){
CPrintStatement* pstat = malloc(sizeof(CPrintStatement));
cerror(pstat, "ERROR-not enough memory Print Statement", __func__, __LINE__);
pstat->expression = exp;
return pstat;
}
//Find in a table(not done yet
CValue* lookup(char* key, env** varenv){
env* varhead = *varenv;
while(varhead){
if(strcmp(varhead->key,key) == 0){
return varhead->vval;
}
varhead = varhead->next;
}
return NULL;
}
void putvar(char* key , CValue* val){
env* lenv = malloc(sizeof(env));
cerror(lenv, "ERROR-not enough memory assignment" , __func__, __LINE__);
lenv->vval = val;
lenv->next = NULL;
lenv->key = key;
if(!tenv){
henv = lenv;
tenv = lenv;
}
else{
lenv->next = henv;
henv = lenv;
}
return;
}
int isnumber(char* str){
int sl = strlen(str);
for(int i = 1; i < sl; i++){
if(isdigit(str[i])){
continue;
}
else {
return 0;
}
}
return 1;
}
void clean(env** envl){
env* lenv = *envl;
env* lenvl;
CValue* val;
while(1){
if(strlen(lenv->key) < 7 && !isnumber(lenv->key)){
lenvl = lenv;
if(lenvl->vval){
free(lenvl->vval);
}
val = lenvl->vval;
if (val->vtype == StringValue){
CStringValue* sn = (CStringValue*)val->value;
if(sn){
free(sn);
}
}
else {
CNumberValue* nn = (CNumberValue*)val->value;
if(nn){
free(nn);
}
}
if(val){
free(val);
}
if(lenvl){
free(lenvl);
}
lenv = lenv->next;
continue;
}
else {
for(int i = 0; i < 2 ; i++){
lenvl = lenv;
if(lenvl->vval){
free(lenvl->vval);
}
val = lenvl->vval;
if (val->vtype == StringValue){
CStringValue* sn = (CStringValue*)val->value;
if(sn){
free(sn);
}
}
else {
CNumberValue* nn = (CNumberValue*)val->value;
if(nn){
free(nn);
}
}
if(val){
free(val);
}
if(lenvl){
free(lenvl);
}
lenv = lenv->next;
}
}
}
}
//Parser help functions
Clist* consume (int typ){
Clist* temp;
if(currenttoken->type != typ){
printf("ERROR---Expected type %i and text : %s", typ, currenttoken->text);
exit(0);
}
temp = currenttoken;
doubleback = stepback;
stepback = temp;
currenttoken = currenttoken->next;
return temp;
}
Clist* consumestr (char* str){
if(!matchstring(str)){
printf("ERROR--expected %s", str);
exit(0);
}
return stepback;
}
//Expression evaluation
CValue* eval(struct CExpression* express, struct env** varenv){
struct CVariableExpression* varexpress;
CValue* cval, *leftval, *rightval, *rop;
FuncCall* fnCall;
LExpression* exlst;
env* lvals;
env* local = *varenv;
int idx;
parList* plst;
CValue* rtval;
FuncAbstraction* fndecl;
CNumberValue* dlvalue,*drvalue, *detemp;
CStringValue* slvalue,*srvalue;
struct COperatorExpression* cope;
int stt = 1;
switch(express->extype){
case V_expression:
varexpress = express->variableexpression;
cval = lookup(varexpress->name, varenv);
if(cval){
switch(cval->vtype){
case NumberValue:
case StringValue:
return cval;
default:
cerror(0, "ERROR--unexpected value type", __func__, __LINE__);
}
}
else{
return NULL;
}
case V_value:
cval = express->valueexpression;
switch(cval->vtype){
case NumberValue:
case StringValue:
return cval;
default:
cerror(cval, "ERROR--unexpected value type", __func__, __LINE__);
}
case V_call:
fnCall = express->funCall;
exlst = fnCall->LexpList;
if(fnCall->tableIndex == -1){
idx = findTableIndex(fnCall->name);
} else {
cerror(0, "Calling Function that was not declared", __func__, __LINE__);
}
fnCall->tableIndex = idx;
fndecl = funcTable[idx];
plst = fndecl->plist;
if(fndecl->parSize != fnCall->argSize){
cerror(0, "Arguments mismatch", __func__, __LINE__);
}
for(int i = 0; i < fnCall->argSize; i++){
lvals = malloc(sizeof(env));
if(!lvals){
cerror(lvals, "Failed to create Cvariable Object for arguments", __func__, __LINE__);
}
lvals->vval = eval(exlst->arg, varenv);
lvals->key = plst->name->variableexpression->name;
lvals->next = local;
plst = plst->next;
local = lvals;
exlst = exlst->next;
}
rtval = eval(fndecl->body , &local);
for(int i = 0; i < fnCall->argSize; i++){
lvals = local;
local = local->next;
if(lvals->vval){
free(lvals->vval);
}
free(lvals);
}
return rtval;
case V_operator:
cope = express->operatorexpression;
leftval = eval(cope->left, varenv);
rightval = eval(cope->right, varenv);
rop = malloc(sizeof(CValue));
cerror(rop, "ERROR--not enough memory to create CValue", __func__, __LINE__);
switch(cope->coperator) {
case TT_REQUAL:
case TT_NOTEQUAL:
if(cope->coperator == TT_NOTEQUAL){
stt = 0;
}
if(leftval->vtype == rightval->vtype && rightval->vtype == NumberValue){
dlvalue = (CNumberValue*)leftval->value;
drvalue = (CNumberValue*)rightval->value;
if(dlvalue->value == drvalue->value){
rop->vtype = stt == 0? CFALSE : CTRUE;
return rop;
}
else{
rop->vtype = stt == 0? CTRUE : CFALSE;
return rop;
}
}
else if(leftval->vtype == rightval->vtype && rightval->vtype == StringValue){
slvalue = (CStringValue*)leftval->value;
srvalue = (CStringValue*)rightval->value;
if(strcmp(slvalue->value, srvalue->value) == 0){
rop->vtype = stt == 0? CFALSE : CTRUE;
return rop;
}
else{
rop->vtype = stt == 0? CTRUE : CFALSE;
return rop;
}
}
else{
cerror(0, "Type error: comparing two different type ", __func__, __LINE__);
}
case TT_GREATEROREQUAL:
if(leftval->vtype == NumberValue && rightval->vtype == NumberValue){
dlvalue = (CNumberValue*)leftval->value;
drvalue = (CNumberValue*)rightval->value;
rop->vtype = dlvalue->value >= drvalue->value? CTRUE : CFALSE;
return rop;
}
else if(leftval->vtype == StringValue && rightval->vtype == StringValue){
slvalue = (CStringValue*)leftval->value;
srvalue = (CStringValue*)rightval->value;
rop->vtype = strcmp(slvalue->value, srvalue->value) >= 0? CTRUE : CFALSE;
return rop;
}
else{
cerror(0, "Type error: comparing two different type " , __func__, __LINE__);
}
case TT_LESSOREQUAL:
if(leftval->vtype == NumberValue && rightval->vtype == NumberValue){
dlvalue = (CNumberValue*)leftval->value;
drvalue = (CNumberValue*)rightval->value;
rop->vtype = dlvalue->value <= drvalue->value? CTRUE : CFALSE;
return rop;
}
else if(leftval->vtype == StringValue && rightval->vtype == StringValue){
slvalue = (CStringValue*)leftval->value;
srvalue = (CStringValue*)rightval->value;
rop->vtype = strcmp(slvalue->value, srvalue->value) <= 0? CTRUE : CFALSE;
return rop;
}
else{
cerror(0, "Type error: comparing two different type ", __func__, __LINE__);
}
case TT_LESS:
if(leftval->vtype == NumberValue && rightval->vtype == NumberValue){
dlvalue = (CNumberValue*)leftval->value;
drvalue = (CNumberValue*)rightval->value;
rop->vtype = dlvalue->value < drvalue->value ? CTRUE : CFALSE;
return rop;
}
else if((leftval->vtype == StringValue)&& (rightval->vtype == StringValue)){
slvalue = (CStringValue*)leftval->value;
srvalue = (CStringValue*)rightval->value;
rop->vtype = strcmp(slvalue->value, srvalue->value) < 0? CTRUE: CFALSE;
return rop;
}
else {
cerror(0,"ERROR--Don't mix up types" , __func__, __LINE__);
}
case TT_GREATER:
if(leftval->vtype == NumberValue && rightval->vtype == NumberValue){
dlvalue = (CNumberValue*)leftval->value;
drvalue = (CNumberValue*)rightval->value;
rop->vtype = dlvalue->value > drvalue->value ? CTRUE : CFALSE;
return rop;
}
if(leftval->vtype == StringValue && rightval->vtype == StringValue){
slvalue = (CStringValue*)leftval->value;
srvalue = (CStringValue*)rightval->value;
rop->vtype = strcmp(slvalue->value, srvalue->value) > 0? CTRUE : CFALSE;
return rop;
}
cerror(0,"ERROR--Don't mix up types" , __func__, __LINE__);
case '+':
if(leftval->vtype == NumberValue && rightval->vtype == NumberValue){
rop->vtype = NumberValue;
dlvalue = (CNumberValue*)leftval->value;
drvalue = (CNumberValue*)rightval->value;
detemp = (CNumberValue* )malloc(sizeof(CNumberValue));
detemp->value = dlvalue->value + drvalue->value;
rop->value = detemp;
return rop;
}
else if(leftval->vtype == StringValue && rightval->vtype == StringValue){
rop->vtype = StringValue;
slvalue = (CStringValue*)leftval->value;
srvalue = (CStringValue*)rightval->value;
strcat(slvalue->value, srvalue->value);
rop->value = slvalue;
return rop;
}
else {
cerror(0,"ERROR--Don't mix up types for + operator" , __func__, __LINE__);
}
case '-':
if(leftval->vtype == NumberValue && rightval->vtype == NumberValue){
rop->vtype = NumberValue;
dlvalue = (CNumberValue*)leftval->value;
drvalue = (CNumberValue*)rightval->value;
detemp = (CNumberValue* )malloc(sizeof(CNumberValue));
detemp->value = dlvalue->value - drvalue->value;
rop->value = detemp;
return rop;
}
cerror(0,"ERROR--Don't mix up types for - operator", __func__, __LINE__ );
case '*':
if(leftval->vtype == NumberValue && rightval->vtype == NumberValue){
rop->vtype = NumberValue;
dlvalue = (CNumberValue*)leftval->value;
drvalue = (CNumberValue*)rightval->value;
detemp = (CNumberValue* )malloc(sizeof(CNumberValue));
detemp->value = dlvalue->value * drvalue->value;
rop->value = detemp;
return rop;
}
cerror(0, "ERROR--Don't mix up types" , __func__, __LINE__);
case '/':
if((leftval->vtype == NumberValue)&& (rightval->vtype == NumberValue)){
rop->vtype = NumberValue;
dlvalue = (CNumberValue*)leftval->value;
drvalue = (CNumberValue*)rightval->value;
detemp = (CNumberValue* )malloc(sizeof(CNumberValue));
if (drvalue->value == 0.0){
cerror(0,"ERROR---division by zero is not allowed", __func__, __LINE__ );
}
detemp->value = dlvalue->value / drvalue->value;
rop->value = detemp;
return rop;
}
cerror(0,"ERROR--Don't mix up types", __func__, __LINE__ );
case '%':
if(leftval->vtype == NumberValue && rightval->vtype == NumberValue){
rop->vtype = NumberValue;
dlvalue = (CNumberValue*)leftval->value;
drvalue = (CNumberValue*)rightval->value;
detemp = (CNumberValue* )malloc(sizeof(CNumberValue));
if (drvalue->value == 0.0){
cerror(0,"ERROR---Modulo by zero is not allowed", __func__, __LINE__ );
}
detemp->value = (int)dlvalue->value % (int)drvalue->value;
rop->value = detemp;
return rop;
}
cerror(0, "ERROR--Don't mix up types", __func__, __LINE__);
}
}
cerror(0,"ERROR--Don't mix up types!!" , __func__, __LINE__);
}
void exec (){
CPrintStatement* ptstat;
LExpression* largs;
CValue* rval, *dval;
CAssignStatement* astat;
CGotoStatement* gotostat;
CIfThenStatement* ifstat;
CGosubStatement*gosubstat;
CInputStatement* ip;
int jmp, slen, buffsize = 256;
char buf[buffsize];
int i = 0;
double lval;
char *tmp;
switch(currentstat->stype){
case S_print:
ptstat = (CPrintStatement*)currentstat->statement;
largs = ptstat->expression;
while(largs != NULL){
rval = eval(largs->arg,&henv);
switch(rval->vtype){
case NumberValue:
printf("%f", ((CNumberValue *)rval->value)->value);
break;
case StringValue:
printf("%s", ((CStringValue *)rval->value)->value);
break;
}
largs = largs->next;
}
if(ptstat->expression == NULL){
printf("\n");
}
return;
case S_assign:
astat = (CAssignStatement *)currentstat->statement;
if(!astat){
cerror(0, "ERROR--not enough memory" , __func__, __LINE__);
}
dval = eval(astat->value, &henv);
putvar(astat->name, dval);
return;
case S_goto:
gotostat = (CGotoStatement*)currentstat->statement;
jmp = (int)strtol(gotostat->label, (char**)NULL, 10);
if(jmp < 0){
//clean(&henv);
jumpto(jmp, -1 * jmp);
}
else {
jumpto(jmp, 0);
}
return;
case S_ifthen:
ifstat = (CIfThenStatement*)currentstat->statement;
dval = eval(ifstat->condition, &henv);
int jmp;
if(dval->vtype == CTRUE)
{
jmp = (int)strtol(ifstat->label, (char**)NULL, 10);
jumpto(jmp, 0);
}
return;
case S_return:
jmp = rstack->stack[--rstack->idx];
jumpto(jmp, 1);
return;
case S_gosub:
gosubstat = (CGosubStatement*)currentstat->statement;
jumpto(gosubstat->jumpto, 0);
rstack->stack[rstack->idx++] = gosubstat->label;
return;
case S_input:
printf("\nuser input>> ");
ip = (CInputStatement*)currentstat->statement;
fgets(buf, buffsize, stdin);
slen = strlen(buf);
while(i <= slen){
if(isdigit(buf[i])){
i++;
continue;
}
break;
}
if(slen - 1 == i){
lval = strtod(buf, (char**)NULL);
dval = makenumval(lval)->valueexpression;
putvar(ip->name, dval);
return;
}
else{
tmp = malloc(sizeof(char)*slen);
strncpy(tmp, buf, slen-1);
tmp[slen] = '\0';
dval = makestrval(tmp)->valueexpression;
putvar(ip->name, dval);
return;
}
}
}
CStatement* parser (){
char* labelnumber;
char xname[60];
char stepname[60];
char govl[20];
const int LOCAL_STAT_MAX = 100;
int llnum, jmpto,nkey, cidx, stepindex = 0;
Clist* tk;
LExpression* hd, *tl;
Clabels* clb;
FuncAbstraction* funabs;
parList* params;
CAssignStatement* astat;
CArrayStatement* arstat;
CPrintStatement* pst;
CInputStatement* in;
CIfThenStatement* ifst;
CGotoStatement* gstat;
CGosubStatement* gosub;
struct CExpression* body, *fst,*inex, *texpf, *texpss, *cond ,*valex, *valop, *value, *optional = NULL;
char* name, *wlab;
struct labelList* locallst = malloc(sizeof(struct labelList));
cerror(locallst, "ERROR- setting label inside a For loop not allowed" , __func__, __LINE__);
locallst->top = 0;
while(1){
while(match(TT_LINE)){ }
if(match(TT_NUMBER)){
if (setinjump){
cerror(0, "ERROR- setting label inside a For loop not allowed" , __func__, __LINE__);
}
labelnumber = stepback->text;
if(match(DEF)){
char* funame = consume(TT_WORD)->text;
if(!match(TT_LEFT_PAREN)){
cerror(0, "ERROR- Expected opening paren for function" , __func__, __LINE__);
}
funabs = malloc(sizeof(FuncAbstraction));
funabs->name = funame;
funabs->parSize = 0;
funabs->plist = malloc(sizeof(parList*));
params = funabs->plist;
if(!match(TT_RIGHT_PAREN)){
params->name = expression();
++funabs->parSize;
while(matchstring(",")){
params->next = malloc(sizeof(parList));
params->next->name = expression();
params = params->next;
++funabs->parSize;
if(match(TT_RIGHT_PAREN)){
break;
}
}
}
else{
params->name = NULL;
consume(TT_RIGHT_PAREN);
}
consume(TT_EQUAL);
body = expression();
funabs->body = body;
funcTable[fntabidx++] = funabs;
addstat(NULL, 0,statsize,labelnumber,1);
++statsize;
}
else if(match(LET)){
name = consume(TT_WORD)->text;
consume(TT_EQUAL);
astat = makeassignstat(name, expression());
addstat(astat, S_assign,statsize,labelnumber,0);
++statsize;
}
else if(match(DIM)){
name = consume(TT_WORD)->text;
consume(TT_LEFT_PAREN);
fst = expression();
if(matchstring(",")){
optional = expression();
}
consume(TT_RIGHT_PAREN);
arstat = makearraystat(name, fst, optional);
addstat(arstat, S_array, statsize,labelnumber,0);
++statsize;
}
else if(match(FOR)){
if(match(TT_WORD)){
if(match(TT_EQUAL)){
name = doubleback->text;
time_t seconds;
seconds = time(NULL);
astat = makeassignstat(name, expression());
addstat(astat, S_assign,statsize,labelnumber,0);
++statsize;