-
Notifications
You must be signed in to change notification settings - Fork 2
/
interpreter.c
267 lines (203 loc) · 7.02 KB
/
interpreter.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
#include "interpreter.h"
ProgramState genProgramState(
Lines* buffer_ptr, Head* r_head_ptr, Head* w_head_ptr
) {
ProgramState state;
state.buffer_ptr = buffer_ptr;
state.r_head_ptr = r_head_ptr;
state.w_head_ptr = w_head_ptr;
state.curr_head_ptr = w_head_ptr;
return state;
}
uint8_t interpret(ProgramState* state) {
bool should_end = false;
while ( !should_end ) {
should_end = execute(state);
state->r_head_ptr->pos_x++;
}
// returning 0, as in 0 errors
return 0;
}
bool execute(ProgramState* state) {
Lines* buffer_ptr = state->buffer_ptr;
Head* r_head_ptr = state->r_head_ptr;
Head* w_head_ptr = state->w_head_ptr;
Head** curr_head_ptr = &state->curr_head_ptr;
if ( !isLegalPosition(buffer_ptr, r_head_ptr) ) {
#ifdef _DEBUG
fprintf(stderr, "Read head reached line end (standard exit)\n");
#endif
return true;
}
char opcode = buffer_ptr->lines[r_head_ptr->pos_y][r_head_ptr->pos_x];
char next = '\0';
// used to make 'x' and 'y' opcodes work
int to_line = 0;
// used to make '-' opcode work
uint32_t mul = 1;
#ifdef _DEBUG
fprintf(stderr, "\nNEXT OPCODE(%i)> \"%c\"\n", r_head_ptr->pos_x, opcode);
fprintf(stderr, "R_POS (%i, %i)\n", r_head_ptr->pos_x, r_head_ptr->pos_y);
fprintf(stderr, "W_POS (%i, %i)\n", w_head_ptr->pos_x, w_head_ptr->pos_y);
fprintf(stderr, "R_MOD(%i)\n", r_head_ptr->mod);
fprintf(stderr, "W_MOD(%i)\n", w_head_ptr->mod);
#endif
switch (opcode) {
case 'w':
*curr_head_ptr = w_head_ptr;
break;
case 'r':
*curr_head_ptr = r_head_ptr;
break;
case '\\':
next = moveDown(buffer_ptr, *curr_head_ptr, (*curr_head_ptr)->mod);
if ( next == EOF ) {
return true;
}
if ( *curr_head_ptr == r_head_ptr && r_head_ptr->mod != 0 ) {
(*curr_head_ptr)->pos_x--;
}
break;
case '/':
next = moveUp(buffer_ptr, *curr_head_ptr, (*curr_head_ptr)->mod);
if ( next == EOF ) {
return true;
}
if ( *curr_head_ptr == r_head_ptr && r_head_ptr->mod != 0 ) {
(*curr_head_ptr)->pos_x--;
}
break;
case '<':
next = moveLeft(buffer_ptr, *curr_head_ptr, (*curr_head_ptr)->mod);
if ( next == EOF ) {
return true;
}
// subtract from head pos instead of executing recursively
if ( *curr_head_ptr == r_head_ptr && r_head_ptr->mod != 0 ) {
(*curr_head_ptr)->pos_x--;
}
break;
case '>':
next = moveRight(buffer_ptr, *curr_head_ptr, (*curr_head_ptr)->mod);
if ( next == EOF ) {
return true;
}
// subtract from head pos instead of executing recursively
if ( *curr_head_ptr == r_head_ptr && r_head_ptr->mod != 0 ) {
(*curr_head_ptr)->pos_x--;
}
break;
case 'y':
to_line = (*curr_head_ptr)->mod - (*curr_head_ptr)->pos_y;
if ( to_line < 0 ) {
next = moveUp(buffer_ptr, *curr_head_ptr, -to_line);
} else {
next = moveDown(buffer_ptr, *curr_head_ptr, to_line);
}
if ( next == EOF ) {
return true;
}
if ( *curr_head_ptr == r_head_ptr ) {
(*curr_head_ptr)->pos_x--;
}
break;
case 'x':
to_line = (*curr_head_ptr)->mod - (*curr_head_ptr)->pos_x;
if ( to_line < 0 ) {
next = moveLeft(buffer_ptr, *curr_head_ptr, -to_line);
} else {
next = moveRight(buffer_ptr, *curr_head_ptr, to_line);
}
if ( next == EOF ) {
return true;
}
if ( *curr_head_ptr == r_head_ptr ) {
(*curr_head_ptr)->pos_x--;
}
break;
// prints char value of mod
case '.':
writeChar(buffer_ptr, w_head_ptr);
break;
// prints numeric value of mod
case '=':
// converting presumed numeric mod to a char
w_head_ptr->mod += 0x30;
writeChar(buffer_ptr, w_head_ptr);
break;
// change mul's value so '+' will subtract
case '-':
mul = -1;
// fall through
// adds next mod's numerical value to current mod
case '+':
next = moveRight(buffer_ptr, r_head_ptr, 1);
if ( next == EOF ) {
return true;
}
(*curr_head_ptr)->mod += (next-0x30)*mul;
mul = 1;
break;
// ? allows for conditional execution
// if the field before it is equal to 0 or '0'
// it advances r_head's pos by 1
//
// if it's not - it decrements the value of the field checked and
// advances r_head's pos by 3
//
// if the positioning of ? turns out impossible to decrement the field
// before it stops the execution
case '?':
if ( r_head_ptr->pos_x == 0 ) {
return true;
}
// assigning the value of the field before
next = buffer_ptr->lines[r_head_ptr->pos_y][r_head_ptr->pos_x-1];
// the amount r_head is gonna be incremented by
int amount = 3;
// performing the check
if ( next == 0 || next == '0' ) {
amount = 1;
} else {
buffer_ptr->lines[r_head_ptr->pos_y][r_head_ptr->pos_x-1]--;
}
next = moveRight(buffer_ptr, r_head_ptr, amount);
if ( next == EOF ) {
return true;
}
// i mean i gotta do this after every move to make the opcode work
r_head_ptr->pos_x--;
break;
// escape the next character;
// also allows for chars representing numbers (48-57) to be loaded into
// mod in their char value
case ',':
next = moveRight(buffer_ptr, r_head_ptr, 1);
if ( next == EOF ) {
return true;
}
(*curr_head_ptr)->mod = next;
break;
default:
#ifdef _DEBUG
fprintf(stderr, "Load mod \"%c\"\n", opcode);
#endif
// if the char represents a number, load it's numerical value
// rather than the char itself
if ( isNumber(opcode) ) {
opcode -= 0x30;
}
#ifdef _DEBUG
fprintf(stderr, "Loaded value (%i)\n", opcode);
#endif
(*curr_head_ptr)->mod = opcode;
break;
}
return false;
}
bool isNumber(char c) {
if ( c > 47 && c < 58 ) {
return true;
}
return false;
}