-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.c
798 lines (740 loc) · 17.6 KB
/
func.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
#include "nlab.h"
void* nfopen(char* fname, char* mode)
{
FILE* fp;
fp = fopen(fname, mode);
if(!fp){
fprintf(stderr, "Cannot read %s ?\n", fname);
exit(EXIT_FAILURE);
}
return fp;
}
void* ncalloc(int n, size_t size)
{
void* v = calloc(n, size);
if(v==NULL){
ERROR("Cannot calloc() space");
}
return v;
}
void Read_File(FILE* fp, Program* p)
{
char str_tmp[BIGSTR];
int i = 0;
while(fscanf(fp, "%s", str_tmp) == 1 && i < MAXNUMTOKENS){
if(strsame(str_tmp, "#")){
assert(fgets(str_tmp, BIGSTR, fp));
}
else{
strcpy(p->wds[i], str_tmp);
i += 1;
}
}
assert(i <= MAXNUMTOKENS);
p->size = i;
}
// Used for test
void Print_Prog(Program* p)
{
for(int i = 0; i < p->size; i++){
printf("%s ", p->wds[i]);
if(strsame(p->wds[i], ";")){
printf("\n");
}
}
printf("\n");
}
void PROG(Program* p)
{
if(! strsame(p->wds[p->cw], "BEGIN")){
ERROR("No BEGIN statement ?\n");
}
p->cw += 1;
if(! strsame(p->wds[p->cw], "{")){
ERROR("No { after BEGIN ?\n");
}
p->cw += 1;
INSTRCLIST(p);
}
void INSTRCLIST(Program *p)
{
if(strsame(p->wds[p->cw], "}")){
return;
}
INSTRC(p);
#ifdef INTERP
// Check loop ?
if(p->loop_stk->size != 0 && strsame(p->wds[p->cw+1], "}")){
Var v_loop, v_idx, v_lp_start;
assert(stack_pop(p->loop_stk, &v_loop));
assert(stack_pop(p->loop_stk, &v_idx));
assert(stack_peek(p->loop_stk, &v_lp_start));
int loop = v_loop.arr[0][0];
int idx = v_idx.arr[0][0];
int lp_start = v_lp_start.arr[0][0];
// Check counter is still scalar ?
if(p->vtb[idx].row != SCALAR || p->vtb[idx].col != SCALAR){
ERROR("Changing the LOOP counter to vector ?\n");
}
// Counter + 1
p->vtb[idx].arr[0][0] += 1;
// Counter <= loop ?
if(p->vtb[idx].arr[0][0] <= loop){
p->cw = lp_start;
stack_push(p->loop_stk, v_idx);
stack_push(p->loop_stk, v_loop);
}
else{
assert(stack_pop(p->loop_stk, &v_lp_start));
free_Var(&v_loop);
free_Var(&v_idx);
free_Var(&v_lp_start);
}
}
#endif
p->cw += 1;
INSTRCLIST(p);
}
void INSTRC(Program* p)
{
if(strsame(p->wds[p->cw], "PRINT")){
PRINT(p);
}
else if(strsame(p->wds[p->cw], "SET")){
SET(p);
}
else if(strsame(p->wds[p->cw], "LOOP")){
LOOP(p);
}
else if(strsame(p->wds[p->cw], "ONES") || strsame(p->wds[p->cw], "READ")){
CREATE(p);
}
else{
ERROR("Expecting an instruction ?\n");
}
}
void PRINT(Program* p)
{
assert(strsame(p->wds[p->cw], "PRINT"));
p->cw += 1;
if(is_VARNAME(p)){
#ifdef INTERP
int idx = p->wds[p->cw][1] - 'A';
if(p->vtb[idx].row == 0){
ERROR("Using a variable before definition ?\n");
}
Print_Var(p->vtb[idx]);
#endif
return;
}
else if(is_STRING(p)){
#ifdef INTERP
char tmp[MAXTOKENSIZE];
int l = strlen(p->wds[p->cw]);
// Discard the " "
memcpy(tmp, p->wds[p->cw]+(1*sizeof(char)), (l-2)*sizeof(char));
tmp[l-2] = '\0';
printf("%s\n", tmp);
#endif
return;
}
ERROR("Expecting a VARNAME or a STRING after PRINT ?\n");
}
void SET(Program* p)
{
assert(strsame(p->wds[p->cw], "SET"));
p->cw += 1;
if(! is_VARNAME(p)){
ERROR("Invalid parameters for SET ?\n");
}
#ifdef INTERP
int idx = p->wds[p->cw][1] - 'A';
Var* v = Var_init(SCALAR, SCALAR, idx);
stack_push(p->stk, *v);
free(v);
#endif
p->cw += 1;
if(! strsame(p->wds[p->cw], ":=")){
ERROR("Invalid parameters for SET ?\n");
}
p->cw += 1;
POLISHLIST(p);
}
void LOOP(Program* p)
{
assert(strsame(p->wds[p->cw], "LOOP"));
#ifdef INTERP
int lp_start = p->cw + LOOPSTART;
Var* v_tmp1 = Var_init(SCALAR, SCALAR, lp_start);
stack_push(p->loop_stk, *v_tmp1);
free(v_tmp1);
#endif
p->cw += 1;
if(! is_VARNAME(p)){
ERROR("Invalid LOOP statement ?\n");
}
#ifdef INTERP
int idx = p->wds[p->cw][1] - 'A';
Var* v_idx1 = Var_init(SCALAR, SCALAR, idx);
Var* v_val1 = Var_init(SCALAR, SCALAR, ONES);
Var_assign(v_idx1, v_val1, p->vtb);
stack_push(p->loop_stk, *v_idx1);
free(v_idx1);
free(v_val1);
#endif
p->cw += 1;
if(! is_INTEGER(p)){
ERROR("Invalid LOOP statement ?\n");
}
#ifdef INTERP
int loop;
assert(sscanf(p->wds[p->cw], "%d", &loop));
Var* v_tmp2 = Var_init(SCALAR, SCALAR, loop);
stack_push(p->loop_stk, *v_tmp2);
free(v_tmp2);
#endif
p->cw += 1;
if(! strsame(p->wds[p->cw], "{")){
ERROR("Invalid LOOP statement ?\n");
}
p->cw += 1;
INSTRCLIST(p);
}
void CREATE(Program* p)
{
if(strsame(p->wds[p->cw], "ONES")){
p->cw += 1;
if(! is_INTEGER(p)){
ERROR("Invalid parameters for ONES ?\n");
}
#ifdef INTERP
int row, col, idx;
assert(sscanf(p->wds[p->cw], "%d", &row));
#endif
p->cw += 1;
if(! is_INTEGER(p)){
ERROR("Invalid parameters for ONES ?\n");
}
#ifdef INTERP
assert(sscanf(p->wds[p->cw], "%d", &col));
#endif
p->cw += 1;
if(! is_VARNAME(p)){
ERROR("Invalid parameters for ONES ?\n");
}
#ifdef INTERP
idx = p->wds[p->cw][1] - 'A';
// An existing variable ?
if(p->vtb[idx].row != 0){
// Avoid memory leak by multiple assignments
free_Var(&p->vtb[idx]);
}
Var* tmp = Var_init(row, col, ONES);
memcpy(&p->vtb[idx], tmp, sizeof(*tmp));
free(tmp);
#endif
}
else if(strsame(p->wds[p->cw], "READ")){
p->cw += 1;
if(! is_STRING(p)){
ERROR("Invalid parameters for READ ?\n");
}
#ifdef INTERP
char fname[MAXTOKENSIZE];
int l = strlen(p->wds[p->cw]);
// Discard the " "
memcpy(fname, p->wds[p->cw]+(1*sizeof(char)), (l-2)*sizeof(char));
fname[l-2] = '\0';
#endif
p->cw += 1;
if(! is_VARNAME(p)){
ERROR("Invalid parameters for READ ?\n");
}
#ifdef INTERP
int idx = p->wds[p->cw][1] - 'A';
// An existing variable ?
if(p->vtb[idx].row != 0){
// Avoid memory leak by multiple assignments
free_Var(&p->vtb[idx]);
}
Var_read(fname, p->vtb, idx);
#endif
}
}
void POLISHLIST(Program* p)
{
if(strsame(p->wds[p->cw], ";")){
#ifdef INTERP
// Only one or no variable in the stack
if(p->stk->size < VARANDVAL){
ERROR("No enough variable for SET statement ?\n");
}
// One variable and one value in the stack
else if(p->stk->size == VARANDVAL){
Var v_val;
Var v_idx;
assert(stack_pop(p->stk, &v_val));
assert(stack_pop(p->stk, &v_idx));
Var_assign(&v_idx, &v_val, p->vtb);
free_Var(&v_idx);
}
else{
ERROR("Too many variables for SET statement ?\n");
}
#endif
return;
}
POLISH(p);
p->cw += 1;
POLISHLIST(p);
}
void POLISH(Program* p)
{
if(is_PUSHDOWN(p)){
return;
}
else if(is_UNARYOP(p)){
return;
}
else if(is_BINARYOP(p)){
return;
}
ERROR("Invalid parameters for SET ?\n");
}
bool is_VARNAME(Program* p)
{
if ( strlen(p->wds[p->cw]) != VARNAMELEN ||
p->wds[p->cw][0] != '$' ||
! isupper(p->wds[p->cw][VARNAMELEN-1]) ){
return false;
}
return true;
}
bool is_STRING(Program* p)
{
int l = strlen(p->wds[p->cw]);
if( p->wds[p->cw][0] != '"' || p->wds[p->cw][l-1] != '"' ){
return false;
}
return true;
}
bool is_INTEGER(Program* p)
{
int d;
if(sscanf(p->wds[p->cw], "%d", &d) != 1){
return false;
}
if(d < 0){
return false;
}
return true;
}
bool is_PUSHDOWN(Program* p)
{
if(is_VARNAME(p)){
#ifdef INTERP
int idx, row, col;
idx = p->wds[p->cw][1] - 'A';
if(p->vtb[idx].row == 0){
ERROR("Using a variable before definition ?\n");
}
row = p->vtb[idx].row;
col = p->vtb[idx].col;
Var* v = Var_init(row, col, ONES);
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
v->arr[i][j] = p->vtb[idx].arr[i][j];
}
}
stack_push(p->stk, *v);
free(v);
#endif
return true;
}
else if(is_INTEGER(p)){
#ifdef INTERP
int num;
assert(sscanf(p->wds[p->cw], "%d", &num));
Var* v = Var_init(SCALAR, SCALAR, num);
stack_push(p->stk, *v);
free(v);
#endif
return true;
}
return false;
}
bool is_UNARYOP(Program* p)
{
if(strsame(p->wds[p->cw], "U-NOT")){
#ifdef INTERP
if(p->stk->size < VARANDVAL){
ERROR("No parameter for U-NOT ?\n");
}
Var v_val;
int row, col;
assert(stack_pop(p->stk, &v_val));
row = v_val.row;
col = v_val.col;
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(v_val.arr[i][j] == 0){
v_val.arr[i][j] = 1;
}
else if(v_val.arr[i][j] == 1){
v_val.arr[i][j] = 0;
}
}
}
stack_push(p->stk, v_val);
#endif
return true;
}
else if(strsame(p->wds[p->cw], "U-EIGHTCOUNT")){
#ifdef INTERP
if(p->stk->size < VARANDVAL){
ERROR("No parameter for U-EIGHTCOUNT ?\n");
}
Var v_val;
int row, col;
assert(stack_pop(p->stk, &v_val));
row = v_val.row;
col = v_val.col;
Var* tmp = Var_init(row, col, ONES);
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
tmp->arr[i][j] = Ueightcount(v_val, i, j);
}
}
stack_push(p->stk, *tmp);
free_Var(&v_val);
free(tmp);
#endif
return true;
}
return false;
}
bool is_BINARYOP(Program* p)
{
if(strsame(p->wds[p->cw], "B-AND")){
#ifdef INTERP
if(p->stk->size < VARANDTWOVAL){
ERROR("No enough variable for B-AND ?\n");
}
B_binaryop(p->stk, "AND");
#endif
return true;
}
else if(strsame(p->wds[p->cw], "B-OR")){
#ifdef INTERP
if(p->stk->size < VARANDTWOVAL){
ERROR("No enough variable for B-OR ?\n");
}
B_binaryop(p->stk, "OR");
#endif
return true;
}
else if(strsame(p->wds[p->cw], "B-GREATER")){
#ifdef INTERP
if(p->stk->size < VARANDTWOVAL){
ERROR("No enough variable for B-GREATER ?\n");
}
B_binaryop(p->stk, "GREATER");
#endif
return true;
}
else if(strsame(p->wds[p->cw], "B-LESS")){
#ifdef INTERP
if(p->stk->size < VARANDTWOVAL){
ERROR("No enough variable for B-LESS ?\n");
}
B_binaryop(p->stk, "LESS");
#endif
return true;
}
else if(strsame(p->wds[p->cw], "B-ADD")){
#ifdef INTERP
if(p->stk->size < VARANDTWOVAL){
ERROR("No enough variable for B-ADD ?\n");
}
B_binaryop(p->stk, "ADD");
#endif
return true;
}
else if(strsame(p->wds[p->cw], "B-TIMES")){
#ifdef INTERP
if(p->stk->size < VARANDTWOVAL){
ERROR("No enough variable for B-TIMES ?\n");
}
B_binaryop(p->stk, "TIMES");
#endif
return true;
}
else if(strsame(p->wds[p->cw], "B-EQUALS")){
#ifdef INTERP
if(p->stk->size < VARANDTWOVAL){
ERROR("No enough variable for B-EQUALS ?\n");
}
B_binaryop(p->stk, "EQUALS");
#endif
return true;
}
return false;
}
#ifdef INTERP
void** n2dcalloc(int h, int w, size_t szelem)
{
int i;
void** p;
p = calloc(h, sizeof(void*));
if(p == NULL){
ERROR("Cannot calloc() space");
}
for(i = 0; i < h; i++){
p[i] = calloc(w, szelem);
if(p[i] == NULL){
ERROR("Cannot calloc() space");
}
}
return p;
}
void n2dfree(void**p, int h)
{
if(p){
int i;
for(i = 0; i < h; i++){
free(p[i]);
}
free(p);
}
}
Var* Var_init(int row, int col, int n)
{
Var* v = (Var*) ncalloc(1, sizeof(Var));
v->row = row;
v->col = col;
v->arr = (int**) n2dcalloc(row, col, sizeof(int));
for(int i = 0; i < v->row; i++){
for(int j = 0; j < v->col; j++){
v->arr[i][j] = n;
}
}
return v;
}
void Var_read(char* fname, Var* vtb, int idx)
{
FILE* fp = nfopen(fname, "rt");
int row, col, d;
if(fscanf(fp, "%d %d", &row, &col) != 2 || row < 0 || col < 0){
ERROR("Invalid .arr file ?\n");
}
Var* v = Var_init(row, col, ONES);
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(fscanf(fp, "%d", &v->arr[i][j]) != 1 ||
v->arr[i][j] < 0){
ERROR("Invalid .arr file ?\n");
}
}
}
if(fscanf(fp, "%d", &d) == 1){
ERROR("Invalid .arr file ?\n");
}
fclose(fp);
memcpy(&vtb[idx], v, sizeof(*v));
free(v);
}
void Var_assign(Var* v_idx, Var* v_val, Var* vtb)
{
int idx = v_idx->arr[0][0];
// An existing variable ?
if(vtb[idx].row != 0){
// Avoid memory leak by multiple assignments
free_Var(&vtb[idx]);
}
vtb[idx] = *v_val;
}
// Used for test
void Print_Var(const Var v)
{
for(int i = 0; i < v.row; i++){
for(int j = 0; j < v.col; j++){
printf("%d ", v.arr[i][j]);
}
printf("\n");
}
}
bool free_Var(Var* v)
{
if(v){
n2dfree((void**)v->arr, v->row);
}
return true;
}
bool free_Vtb(Var* vtb)
{
if(vtb){
int i;
for(i = 0; i < NUMVARIABLE; i++){
if(vtb[i].row != 0){
n2dfree((void**) vtb[i].arr, vtb[i].row);
}
}
free(vtb);
}
return true;
}
int Ueightcount(Var v, int i, int j)
{
int cnt = 0;
for(int y = i-1; y <= i+1; y++){
for(int x = j-1; x <= j+1; x++){
if(y >= 0 && y < v.row && x >= 0 && x < v.col){
if(v.arr[y][x] > 0){
cnt += 1;
}
}
}
}
if(v.arr[i][j] > 0){
cnt -= 1;
}
return cnt;
}
void B_binaryop(stack* s, char* func)
{
Var val1, val2;
assert(stack_pop(s, &val2));
assert(stack_pop(s, &val1));
if(val2.row == 1 && val2.col == 1){
B_scalar(val2, val1, s, func);
}
else if(val1.row == 1 && val1.col ==1){
B_scalar_2(val1, val2, s, func);
}
else if(val1.row == val2.row && val1.col == val2.col){
B_matrix(val1, val2, s, func);
}
else{
ERROR("Operating two matrix of different size ?\n");
}
}
void B_scalar(Var v_sca, Var v_mat, stack* s, char* func)
{
int sca = v_sca.arr[0][0];
for(int i = 0; i < v_mat.row; i++){
for(int j = 0; j < v_mat.col; j++){
v_mat.arr[i][j] = B_operating(v_mat.arr[i][j], sca, func);
}
}
stack_push(s, v_mat);
free_Var(&v_sca);
}
void B_scalar_2(Var v_sca, Var v_mat, stack* s, char* func)
{
int sca = v_sca.arr[0][0];
for(int i = 0; i < v_mat.row; i++){
for(int j = 0; j < v_mat.col; j++){
v_mat.arr[i][j] = B_operating(sca, v_mat.arr[i][j], func);
}
}
stack_push(s, v_mat);
free_Var(&v_sca);
}
void B_matrix(Var val1, Var val2, stack* s, char* func)
{
for(int i = 0; i < val2.row; i++){
for(int j = 0; j < val2.col; j++){
val2.arr[i][j] = B_operating(val1.arr[i][j], val2.arr[i][j], func);
}
}
stack_push(s, val2);
free_Var(&val1);
}
int B_operating(int v1, int v2, char* func)
{
int result = NORESULT;
if(strsame(func, "AND")){
bool b = v1 && v2;
result = (int) b;
}
else if(strsame(func, "OR")){
bool b = v1 || v2;
result = (int) b;
}
else if(strsame(func, "GREATER")){
bool b = v1 > v2;
result = (int) b;
}
else if(strsame(func, "LESS")){
bool b = v1 < v2;
result = (int) b;
}
else if(strsame(func, "ADD")){
result = v1 + v2;
}
else if(strsame(func, "TIMES")){
result = v1 * v2;
}
else if(strsame(func, "EQUALS")){
bool b = v1 == v2;
result = (int) b;
}
return result;
}
#endif
#ifdef INTERP
// Modified from Neil's ADT-Stack!!!
/* Create an empty stack */
stack* stack_init(void)
{
stack* s = (stack*) ncalloc(1, sizeof(stack));
return s;
}
/* Add element to top */
void stack_push(stack* s, stacktype d)
{
if(s){
dataframe* f = ncalloc(1, sizeof(dataframe));
f->i = d;
f->next = s->start;
s->start = f;
s->size = s->size + 1;
}
}
/* Take element from top */
bool stack_pop(stack* s, stacktype* d)
{
if((s == NULL) || (s->start == NULL)){
return false;
}
dataframe* f = s->start->next;
*d = s->start->i;
free(s->start);
s->start = f;
s->size = s->size - 1;
return true;
}
/* Copy top element into d (but don't pop it) */
bool stack_peek(stack*s, stacktype* d)
{
if((s == NULL) || (s->start == NULL)){
return false;
}
*d = s->start->i;
return true;
}
/* Clears all space used */
bool stack_free(stack* s)
{
if(s){
dataframe* p = s->start;
while(p != NULL){
dataframe* tmp = p->next;
free(p);
p = tmp;
}
free(s);
}
return true;
}
#endif