-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
57 lines (48 loc) · 1.33 KB
/
stack.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
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
/// Stack for variables and numbers
char *stack[1000];
int stackCounter = 0;
/// Function for pushing a variable or a number into the stack
void push(char *str) {
stack[stackCounter] = str;
stackCounter++;
}
/// Function for popping a variable or a number into the stack
char *pop() {
stackCounter--;
return stack[stackCounter];
}
//////////////////////////// IR STACK ////////////////////////////
/// Stack of instructions, but reversed
char *irstack[1000];
int irstackCounter = 0;
/// Function for pushing an instruction into the irstack
void irpush(char *str) {
irstack[irstackCounter] = str;
irstackCounter++;
}
/// Function for popping an instruction into the irstack
char *irpop() {
irstackCounter--;
return irstack[irstackCounter];
}
/// Emptying the reversed instructions stack
void drain() {
while (irstackCounter > 0) {
char *temp = irpop();
free(temp);
}
}
/// Writing all instructions in proper order
void writeAllInstructions(FILE *file) {
for (int i = 0; i < irstackCounter; i++) {
fprintf(file, "%s\n", irstack[i]);
if (irstack[i] != NULL) {
free(irstack[i]);
irstack[i] = NULL;
}
}
irstackCounter = 0;
}