-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsetree.c
185 lines (164 loc) · 4.73 KB
/
parsetree.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
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
#include "heap.h"
#include "parsetree.h"
extern Stack* pStack;
extern Heap* pHeap;
#define DEBUG 0
//
// Private functions
//
int twoToThe(int exponent) {
int result = 1;
int i;
for (i = 0; i < exponent; i++) {
result *= 2;
}
return result;
}
//
// Public functions
//
TreeNode* createEmptyNode(NodeType type) {
TreeNode* result = malloc(sizeof(TreeNode));
result->type = type;
result->pFirstOperand = NULL;
result->pSecondOperand = NULL;
result->digitLiteral = 0;
result->charLiteral = 0;
result->pNextNode = NULL;
return result;
}
void freeNode(TreeNode* pNode) {
if (!pNode) {
return;
}
freeNode(pNode->pFirstOperand);
freeNode(pNode->pSecondOperand);
freeNode(pNode->pNextNode);
free(pNode);
}
int digitRecursionCount;
void* execute(TreeNode* pNode) {
if (!pNode) {
return NULL;
}
#if DEBUG
printf("Executing node of type %d\n", pNode->type);
#endif
switch (pNode->type) {
// Statements - break so we can execute the next one
case NT_stack_push: {
int num = (int) execute(pNode->pFirstOperand);
push(pStack, num);
} break;
case NT_stack_dup: {
push(pStack, peek(pStack));
} break;
case NT_stack_copy: {
int howManyDown = (int) execute(pNode->pFirstOperand);
int val = (int) (get(pStack->pList, howManyDown)->data);
push(pStack, val);
} break;
case NT_stack_swap: {
int first = pop(pStack);
int second = pop(pStack);
push(pStack, first);
push(pStack, second);
} break;
case NT_stack_discard: {
pop(pStack);
} break;
case NT_stack_slide: {
// TODO: What the hell does slide mean
} break;
case NT_arithmetic_add: {
int right = pop(pStack);
int left = pop(pStack);
push(pStack, left + right);
} break;
case NT_arithmetic_subtract: {
int right = pop(pStack);
int left = pop(pStack);
push(pStack, left - right);
} break;
case NT_arithmetic_multiply: {
int right = pop(pStack);
int left = pop(pStack);
push(pStack, left * right);
} break;
case NT_arithmetic_divide: {
int right = pop(pStack);
int left = pop(pStack);
push(pStack, left / right);
} break;
case NT_arithmetic_mod: {
int right = pop(pStack);
int left = pop(pStack);
push(pStack, left % right);
} break;
case NT_heap_store: {
int value = pop(pStack);
int address = pop(pStack);
store(pHeap, address, value);
} break;
case NT_heap_retrieve: {
int address = pop(pStack);
push(pStack, read(pHeap, address));
} break;
case NT_flow_end_program: {
return NULL;
} break;
case NT_io_output_char: {
// TODO: Peek or pop? Same below
printf("%c", pop(pStack));
} break;
case NT_io_output_num: {
printf("%d", pop(pStack));
} break;
case NT_io_read_char: {
int address = pop(pStack);
char c = getchar();
store(pHeap, address, c);
} break;
case NT_io_read_num: {
int address = pop(pStack);
int num;
scanf("%d", &num);
store(pHeap, address, num);
} break;
// Expressions - return so we don't execute a next statement
case NT_number: {
digitRecursionCount = 0;
int value = (int) execute(pNode->pSecondOperand);
if (pNode->pFirstOperand->digitLiteral) {
value *= -1;
}
return (void*) value;
} return NULL;
case NT_digit: {
int placesFromLeft = digitRecursionCount;
if (pNode->pNextNode) {
int sumSoFar;
digitRecursionCount++;
sumSoFar = (int) execute(pNode->pNextNode);
if (pNode->digitLiteral) {
sumSoFar += twoToThe(digitRecursionCount - placesFromLeft);
}
return (void*) sumSoFar;
} else {
return (void*) pNode->digitLiteral;
}
} return NULL;
default: {
printf("Cannot execute node with unrecognized type %d\n", pNode->type);
} break;
}
#if DEBUG
printf("Stack: ");
printStack(pStack);
#endif
execute(pNode->pNextNode);
return NULL;
}