-
Notifications
You must be signed in to change notification settings - Fork 0
/
pichu.c
759 lines (679 loc) · 27.3 KB
/
pichu.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "pichu.h"
//running the program
Instruction program[] = {
M_INST_PUSH(1),
M_INST_PUSH(4),
M_INST_PUSH(6),
M_INST_PUSH(8),
};
#define PROGRAM_SIZE (sizeof(program)/sizeof(program[0]))
void push(Machine *machine, int operand){ //push value
if(machine->currentStackSize >= MAX_STACK_SIZE){
fprintf(stderr, "stack overflow\n");
exit(EXIT_FAILURE);
}
machine->stack[machine->currentStackSize] = operand;
machine->currentStackSize++;
}
int pop(Machine *machine){ //pop the top
if(machine->currentStackSize <= 0) {
fprintf(stderr, "stack underflow\n");
exit(EXIT_FAILURE);
}
machine->currentStackSize--;
return machine->stack[machine->currentStackSize];
}
void IndexSwap(Machine *machine, int index){
if(index > machine->currentStackSize || index < 0){
fprintf(stderr, "index out of bounds\n");
exit(EXIT_FAILURE);
}
int temp = machine->stack[index];
machine->stack[index] = pop(machine);
push(machine,temp);
}
void IndexDuplicate(Machine *machine, int index){
if(index > machine->currentStackSize || index < 0){
fprintf(stderr, "index out of bounds\n");
exit(EXIT_FAILURE);
}
push(machine, machine->stack[index]);
}
void printStack(Machine *machine){ //prints operands on the stack
printf("Top Of Stack:\n");
for(int i = machine->currentStackSize - 1; i>=0; i--){
printf("%d\n", machine->stack[i]);
}
printf("Bottom Of Stack:\n");
}
void writeProgramToFile(Machine *machine, char *filename){
FILE *f = fopen(filename, "wb");
if(f == NULL){
fprintf(stderr, "could not open file %s\n", filename);
exit(EXIT_FAILURE);
}
fwrite(machine->instructions, sizeof(machine->instructions[0]), PROGRAM_SIZE, f);
printf("program written to file %s\n", filename);
fclose(f);
}
Machine *readFromFile(Machine *machine, char *filename){
FILE *f = fopen(filename,"r");
if(f == NULL){
fprintf(stderr, "could not open file %s\n", filename);
exit(EXIT_FAILURE);
}
Instruction *instructions = malloc(sizeof(Instruction) * MAX_STACK_SIZE);
if (instructions == NULL) {
fprintf(stderr, "memory allocation failed\n");
exit(EXIT_FAILURE);
}
else if (machine == NULL) {
fprintf(stderr, "memory allocation failed\n");
exit(EXIT_FAILURE);
}
fseek(f, 0, SEEK_END);
int length = ftell(f);
fseek(f, 0, SEEK_SET);
fread(instructions, sizeof(instructions[0]), length/8, f);
printf("program read from file %s\n", filename);
machine->programSize = length/8;
machine->instructions = instructions;
fclose(f);
return machine;
}
void runPichu(Machine *machine){
int first, second; //first and second operands, used for division and subtraction
int gotoStore; //Used for GOTO_Z and GOTO_NZ
int stackStore[MAX_STACK_SIZE]; //used to store previous values within the stack to restore during GOTO operation failures
//run program & instructions--------------------------------------------------------
for(size_t i=0; i < machine->programSize; i++) {
switch((machine->instructions)[i].operator){
//NO OPERATION
case INST_NOP:
continue;
break;
//PUSH
case INST_PUSH:
push(machine, machine->instructions[i].operand);
break;
//POP
case INST_POP:
pop(machine);
break;
//ADD
case INST_ADD:
push(machine, pop(machine) + pop(machine));
break;
//SUBTRACT
case INST_SUB:
first = pop(machine);
second = pop(machine);
push(machine, first - second);
break;
//MULTIPLY
case INST_MUL:
push(machine ,pop(machine) * pop(machine));
break;
//DIVIDE
case INST_DIV:
first = pop(machine);
second = pop(machine);
if(second == 0){
fprintf(stderr, "cant divide by 0\n");
exit(EXIT_FAILURE);
}
push(machine ,first / second);
break;
//PRINT
case INST_PRINT:
printf("%d\n", pop(machine));
break;
//DUPLICATE
case INST_DUP:
first = pop(machine);
push(machine ,first);
push(machine ,first);
break;
//SWAP
case INST_SWAP:
first = pop(machine);
second = pop(machine);
push(machine, first);
push(machine, second);
break;
//COMPARE EQUAL
case INST_COMPE:
first = pop(machine);
second = pop(machine);
if(first == second){
push(machine, 1);
}
else{
push(machine, 0);
}
break;
//COMPARE NOT EQUAL
case INST_COMPNE:
first = pop(machine);
second = pop(machine);
if(first == second){
push(machine, 0);
}
else{
push(machine, 1);
}
break;
//COMPARE GREATER THAN
case INST_COMPG:
first = pop(machine);
second = pop(machine);
if(first > second){
push(machine, 1);
}
else{
push(machine, 0);
}
break;
//COMPARE GREATER THAN OR EQUAL TO
case INST_COMPGE:
first = pop(machine);
second = pop(machine);
if(first >= second){
push(machine, 1);
}
else{
push(machine, 0);
}
break;
//COMPARE LESS THAN
case INST_COMPL:
first = pop(machine);
second = pop(machine);
if(first < second){
push(machine, 1);
}
else{
push(machine, 0);
}
break;
//COMPARE LESS THAN OR EQUAL TO
case INST_COMPLE:
first = pop(machine);
second = pop(machine);
if(first <= second){
push(machine, 1);
}
else{
push(machine, 0);
}
break;
//POP2
case INST_POP2:
pop(machine);
pop(machine);
break;
//NEGATE
case INST_NEG:
first = pop(machine);
push(machine, -first);
break;
//READ
case INST_READ:
first = pop(machine);
printf("top of stack is %d\n", first);
push(machine, first);
break;
//STOP
case INST_STOP:
i = machine->programSize;
break;
//CLEAR
case INST_CLEAR:
while(machine->currentStackSize > 0){
pop(machine);
}
break;
//CONSTANT -1
case INST_CONST_M1:
push(machine, -1);
break;
//CONSTANT 0
case INST_CONST_0:
push(machine, 0);
break;
//CONSTANT 1
case INST_CONST_1:
push(machine, 1);
break;
//CONSTANT 2
case INST_CONST_2:
push(machine, 2);
break;
//CONSTANT 3
case INST_CONST_3:
push(machine, 3);
break;
//CONSTANT 4
case INST_CONST_4:
push(machine, 4);
break;
//CONSTANT 5
case INST_CONST_5:
push(machine, 5);
break;
/*//!-----------------------------------------------------------------
//PUSH NULL REFERENCE ONTO STACK
case INST_PUSH_NULL:
push(machine, NULL);
break;
//COMPARE NULL
case INST_COMPE_NULL:
first = pop(machine);
if(first == NULL){
push(machine, 1);
}
else{
push(machine, 0);
}
break;
//COMPARE NOT NULL
case INST_COMPNE_NULL:
first = pop(machine);
if(first == NULL){
push(machine, 0);
}
else{
push(machine, 1);
}
break;
*///!-----------------------------------------------------------------
//GOTO
//NOTE: I have commented out the printf checks for this instruction
case INST_GOTO:
first = machine->instructions[i].operand;
second = pop(machine);
int popIterator =0;
stackStore[0] = second; //stores initial popped value
//printf("stackStore[0] = %d\n", stackStore[0]);
for(int gotoIndex = 0; gotoIndex < machine->currentStackSize; popIterator++){
//printf("first = %d, second = %d\n", first, second);
if(first != second){
//printf("First != second loop\n");
second = pop(machine);
stackStore[popIterator+1] = second;
//printf("%d\n", stackStore[popIterator]);
}
else if(first == second){
//printf("First == second loop\n");
push(machine, second);
break; //want to break here so it doesn't readd the stack
}
else{
fprintf(stderr,"out of bounds\n");
//exit loop and go into the other loop outside the for loop
//exit(EXIT_FAILURE);
}
}
//this re adds the values back into the stack
if(first != second) {
//printf("popIterator = %d\n", popIterator);
for(int forLoopIterator = popIterator; forLoopIterator > -1; forLoopIterator--){
printf("for loop iterator :%d\n", forLoopIterator);
push(machine, stackStore[forLoopIterator]);
}
}
//printf("exited for loop\n");
break;
//GOTO IF ZERO
case INST_GOTO_Z:
gotoStore = pop(machine);
printf("Popped first value and stored it as %d\n", gotoStore);
if(gotoStore == 0){
//COPY GOTO
first = machine->instructions[i].operand;
second = pop(machine);
int popIterator =0;
stackStore[0] = second; //stores initial popped value
//printf("stackStore[0] = %d\n", stackStore[0]);
for(int gotoIndex = 0; gotoIndex < machine->currentStackSize; popIterator++){
//printf("first = %d, second = %d\n", first, second);
if(first != second){
//printf("First != second loop\n");
second = pop(machine);
stackStore[popIterator+1] = second;
//printf("%d\n", stackStore[popIterator]);
}
else if(first == second){
//printf("First == second loop\n");
push(machine, second);
break; //want to break here so it doesn't readd the stack
}
else{
fprintf(stderr,"out of bounds\n");
//exit loop and go into the other loop outside the for loop
//exit(EXIT_FAILURE);
}
}
//this re adds the values back into the stack
if(first != second) {
//printf("popIterator = %d\n", popIterator);
for(int forLoopIterator = popIterator; forLoopIterator > -1; forLoopIterator--){
printf("for loop iterator :%d\n", forLoopIterator);
push(machine, stackStore[forLoopIterator]);
}
push(machine, gotoStore);
}
//printf("exited for loop\n");
}
else{
//PUSH POPPED VALUE BACK IN
push(machine, gotoStore);
}
break;
//GOTO IF NOT ZERO
case INST_GOTO_NZ:
gotoStore = pop(machine);
printf("Popped first value and stored it as %d\n", gotoStore);
if(gotoStore != 0){
//COPY GOTO
first = machine->instructions[i].operand;
second = pop(machine);
int popIterator = 0;
stackStore[0] = second; //stores initial popped value
//printf("stackStore[0] = %d\n", stackStore[0]);
for(int gotoIndex = 0; gotoIndex < machine->currentStackSize; popIterator++){
//printf("first = %d, second = %d\n", first, second);
if(first != second){
//printf("First != second loop\n");
second = pop(machine);
stackStore[popIterator+1] = second;
//printf("%d\n", stackStore[popIterator]);
}
else if(first == second){
//printf("First == second loop\n");
push(machine, second);
break; //want to break here so it doesn't readd the stack
}
else{
fprintf(stderr,"out of bounds\n");
//exit loop and go into the other loop outside the for loop
//exit(EXIT_FAILURE);
}
}
//this re adds the values back into the stack
if(first != second) {
//printf("popIterator = %d\n", popIterator);
for(int forLoopIterator = popIterator; forLoopIterator > -1; forLoopIterator--){
printf("for loop iterator :%d\n", forLoopIterator);
push(machine, stackStore[forLoopIterator]);
}
push(machine, gotoStore);
}
//printf("exited for loop\n");
}
else{
//PUSH POPPED VALUE BACK IN
push(machine, gotoStore);
}
break;
//GOES TO NULL
case INST_GOTO_NULL:
//unfinished
break;
//REPLACE WITH
case INST_CHANGE_TO:
int changeTo = machine->instructions[i].operand;
printf("Change to is %d\n", changeTo);
break;
//MODIFY
case INST_MOD:
int mod = machine->instructions[i].operand;
printf("Modifying %d to %d\n", mod, changeTo);
int modifyStore[MAX_STACK_SIZE];
modifyStore[0] = pop(machine);
first = modifyStore[0];
printf("First outside pop = %d\n", first);
printf("ModifyStore %d\n", modifyStore[0]);
int stackIterator = 1;
for(int modLoop = machine->currentStackSize; modLoop != 0; modLoop--, stackIterator++){
first = pop(machine);
printf("stackIterator = %d\n", stackIterator);
modifyStore[stackIterator] = first;
printf("%d\n", first);
printf("ModifyStore %d\n", modifyStore[stackIterator]);
if(first == mod){
printf("in first == mod, %d\n", first);
push(machine, changeTo);
//push the stored stack back
for(int i = stackIterator; i != 0; i--){
printf("pushing %d at %d\n", modifyStore[i-1], i-1);
push(machine, modifyStore[i-1]);
}
break;
}
else{
//Do nothing
}
}
//read everything back into stack when nothing found
if(first != mod){
for(int forLoopIterator = stackIterator; forLoopIterator > 0; forLoopIterator--){
printf("for loop iterator :%d\n", forLoopIterator);
push(machine, modifyStore[forLoopIterator-1]);
}
}
printf("Exited loop\n");
break;
//MODIFY ALL
case INST_MOD_ALL:
int modAll = machine->instructions[i].operand;
printf("Modifying all %d to %d\n", modAll, changeTo);
int modifyAllStore[MAX_STACK_SIZE];
first = pop(machine);
modifyAllStore[0] = first;
printf("First outside pop = %d/%d\n", first, modifyAllStore[0]);
stackIterator = 1;
for(int modLoop = machine->currentStackSize; modLoop != 0; modLoop--, stackIterator++){
first = pop(machine);
printf("stackIterator = %d\n", stackIterator);
modifyAllStore[stackIterator] = first;
printf("%d\n", first);
printf("ModifyStore %d\n", modifyAllStore[stackIterator]);
if(first == modAll){
printf("first==modAll loop ,first:%d\n", first);
modifyAllStore[stackIterator] = changeTo;
printf("ModifyStore %d\n", modifyAllStore[stackIterator]);
}
else{
//Do nothing
}
}
//add everything back into the stack
for(int i = stackIterator; i > 0; i--){
printf("Entered 2nd for loop");
push(machine, modifyAllStore[i-1]);
}
printf("Exited loop\n");
break;
//FLIPS STACK
case INST_FLIP:
int flipStore[MAX_STACK_SIZE];
stackIterator = 0;
//puts stack into flipStore
for(int i = machine->currentStackSize; i != 0; i--, stackIterator++){
flipStore[stackIterator] = pop(machine);
printf("%d\n", flipStore[stackIterator]);
}
//puts flipStore back into stack
for(int i = 1; i != stackIterator+1; i++){
printf("pushing %d at %d\n", flipStore[i-1], i-1);
push(machine, flipStore[i-1]);
}
break;
//GOTO CONDITION
case INST_CONDITION:
int condition = machine->instructions[i].operand;
printf("GOTO condition is set to %d\n", condition);
break;
//GOTO IF CONDITION = TRUE
case INST_GOTO_C:
gotoStore = pop(machine);
printf("Popped first value and stored it as %d\n", gotoStore);
if(gotoStore == condition){
//COPY GOTO
first = machine->instructions[i].operand;
second = pop(machine);
int popIterator =0;
stackStore[0] = second; //stores initial popped value
//printf("stackStore[0] = %d\n", stackStore[0]);
for(int gotoIndex = 0; gotoIndex < machine->currentStackSize; popIterator++){
//printf("first = %d, second = %d\n", first, second);
if(first != second){
//printf("First != second loop\n");
second = pop(machine);
stackStore[popIterator+1] = second;
//printf("%d\n", stackStore[popIterator]);
}
else if(first == second){
//printf("First == second loop\n");
push(machine, second);
break; //want to break here so it doesn't readd the stack
}
else{
fprintf(stderr,"out of bounds\n");
//exit loop and go into the other loop outside the for loop
//exit(EXIT_FAILURE);
}
}
//this re adds the values back into the stack
if(first != second) {
//printf("popIterator = %d\n", popIterator);
for(int forLoopIterator = popIterator; forLoopIterator > -1; forLoopIterator--){
printf("for loop iterator :%d\n", forLoopIterator);
push(machine, stackStore[forLoopIterator]);
}
push(machine, gotoStore);
}
//printf("exited for loop\n");
}
else{
//PUSH POPPED VALUE BACK IN
push(machine, gotoStore);
}
break;
//GOTO IF CONDITION = FALSE
case INST_GOTO_NC:
gotoStore = pop(machine);
printf("Popped first value and stored it as %d\n", gotoStore);
if(gotoStore != condition){
//COPY GOTO
first = machine->instructions[i].operand;
second = pop(machine);
int popIterator = 0;
stackStore[0] = second; //stores initial popped value
//printf("stackStore[0] = %d\n", stackStore[0]);
for(int gotoIndex = 0; gotoIndex < machine->currentStackSize; popIterator++){
//printf("first = %d, second = %d\n", first, second);
if(first != second){
//printf("First != second loop\n");
second = pop(machine);
stackStore[popIterator+1] = second;
//printf("%d\n", stackStore[popIterator]);
}
else if(first == second){
//printf("First == second loop\n");
push(machine, second);
break; //want to break here so it doesn't readd the stack
}
else{
fprintf(stderr,"out of bounds\n");
//exit loop and go into the other loop outside the for loop
//exit(EXIT_FAILURE);
}
}
//this re adds the values back into the stack
if(first != second) {
//printf("popIterator = %d\n", popIterator);
for(int forLoopIterator = popIterator; forLoopIterator > -1; forLoopIterator--){
printf("for loop iterator :%d\n", forLoopIterator);
push(machine, stackStore[forLoopIterator]);
}
push(machine, gotoStore);
}
//printf("exited for loop\n");
}
else{
//PUSH POPPED VALUE BACK IN
push(machine, gotoStore);
}
break;
//INCREMENT TOP
case INST_INC:
first = pop(machine);
first++;
push(machine, first);
break;
//DECREMENT TOP
case INST_DEC:
first = pop(machine);
first--;
push(machine, first);
break;
//INCREMENT ALL
case INST_INC_ALL:
int incAllStore[MAX_STACK_SIZE];
stackIterator = 0;
for(int i = machine->currentStackSize; i != 0; i--, stackIterator++){
incAllStore[stackIterator] = pop(machine) + 1;
printf("at %d, = %d\n",stackIterator, incAllStore[stackIterator]);
}
for(int i = stackIterator; i != 0; i--){
//printf("i = %d\n",i);
push(machine, incAllStore[i-1]);
}
break;
//DECREMENT ALL
case INST_DEC_ALL:
int decAllStore[MAX_STACK_SIZE];
stackIterator = 0;
for(int i = machine->currentStackSize; i != 0; i--, stackIterator++){
decAllStore[stackIterator] = pop(machine) - 1;
printf("at %d, = %d\n",stackIterator, decAllStore[stackIterator]);
}
for(int i = stackIterator; i != 0; i--){
//printf("i = %d\n",i);
push(machine, decAllStore[i-1]);
}
break;
//INDEX SWAP
case INST_INDEX_SWAP:
IndexSwap(machine, machine->instructions[i].operand);
break;
//INDEX DUPLICATE
case INST_INDEX_DUP:
IndexDuplicate(machine, machine->instructions[i].operand);
break;
//DEFAULT
default: //unexpected state
assert(0);
break;
}
}
//END-OF-SWITCH-AND-FOR-LOOP--------------------------------------------------------
}
int main(){
lexer();
Machine *loadedMachine = malloc(sizeof(Machine) * MAX_STACK_SIZE);
loadedMachine->instructions = program;
writeProgramToFile(loadedMachine, "program.bin");
loadedMachine = readFromFile(loadedMachine, "program.bin");
//Goes into running the instructions
runPichu(loadedMachine);
//Prints the stack
printStack(loadedMachine);
return 0;
}