-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.c
305 lines (236 loc) · 8.53 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
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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include "interpreter.h"
#include "interface.h"
#define KEY_CHARACTERS "#[]+-<>.,"
// I don't feel like doing this dynamically.
#define MAX_LOOP_DEPTH 256
#define INITIAL_MEMORY_SIZE 256
static uint16_t memorySize = INITIAL_MEMORY_SIZE;
static uint8_t* memory;
static uint16_t memoryIndex;
static char* fullCode;
static char* strippedCode;
static uint16_t* strippedIndeces;
static uint16_t strippedLength = 0;
static uint16_t strippedIndex = 0;
static void (*OutFunction)(char out);
static int16_t (*RequestInput)(void);
static char *FileToBuffer(const char *fileName);
static uint16_t StripCode(char* code);
static uint16_t loopDepth = 0;
char* InitInterpreter(const char* inFileName, void (*Output)(char), int16_t(*Input)(void)) {
fullCode = FileToBuffer(inFileName);
if(fullCode == NULL) return NULL;
strippedLength = StripCode(fullCode);
OutFunction = Output;
RequestInput = Input;
memory = (uint8_t*)malloc(INITIAL_MEMORY_SIZE);
memset(memory, 0, memorySize);
return fullCode;
}
// Interprets the next char, and returns it.
// `nextCharPtr` is set to the next char that will be interpreted.
char InterpretNextChar(char* nextCharPtr) {
// Store the char because the index might change.
char codeChar = strippedCode[strippedIndex];
switch(codeChar & NO_BREAKPOINT_bm) {
case '+':
memory[memoryIndex]++;
break;
case '-':
memory[memoryIndex]--;
break;
case '>':
memoryIndex++;
// Allocate more memory when needed.
if(memoryIndex >= memorySize) {
memorySize += INITIAL_MEMORY_SIZE;
memory = (uint8_t*)realloc(memory, memorySize);
if(memory == NULL) {
fprintf(stderr, "Not enough memory :(\n");
return INTERPRETER_OUT_OF_MEMORY;
}
// Fill it with 0's.
memset(memory + memorySize - INITIAL_MEMORY_SIZE, 0x00, INITIAL_MEMORY_SIZE);
}
break;
case '<':
if(memoryIndex == 0) {
return INTERPRETER_MEMORY_OUT_OF_BOUNDS;
}
memoryIndex--;
break;
case '[':
// Set the index to the stored index of the corresponding bracket.
if(memory[memoryIndex] == 0)
memcpy(&strippedIndex, strippedCode + strippedIndex + 1, sizeof(strippedIndex));
else loopDepth++;
strippedIndex += 2;
break;
case ']':
// Set the index to the stored index of the corresponding bracket.
if(memory[memoryIndex] != 0)
memcpy(&strippedIndex, strippedCode + strippedIndex + 1, sizeof(strippedIndex));
else loopDepth--;
strippedIndex += 2;
break;
case '.':
OutFunction(memory[memoryIndex]);
break;
case ',': {
// The calling function should now consider giving an input using ProvideInput().
// Execution will not continue before an input is given.
int16_t input = RequestInput();
if(input < 0) {
if(nextCharPtr) *nextCharPtr = strippedCode[strippedIndex + 1];
fprintf(stderr, "No input???\n");
return ',';
}
memory[memoryIndex] = (uint8_t)input;
break;
}
case '#':
break;
default:
return INTERPRETER_LEAK;
}
strippedIndex++;
if(strippedIndex == strippedLength) {
return INTERPRETER_EOF;
}
if(nextCharPtr) *nextCharPtr = strippedCode[strippedIndex];
return codeChar;
}
void ToggleBreakPoint(void) {
// Toggle the breakpoint bit in the code as well as the stripped code.
strippedCode[strippedIndex] ^= BREAKPOINT_bm;
fullCode[strippedIndeces[strippedIndex]] ^= BREAKPOINT_bm;
}
void ToggleBreakPointAtCodeIndex(uint16_t codeIndex) {
uint16_t stripIndex = 0;
fprintf(stderr, "codeIndex %d ", codeIndex);
// Find the character in the stripped code.
while(strippedIndeces[stripIndex] < codeIndex) stripIndex++;
// Make sure it's a brainfuck character and not a comment.
if(strippedCode[stripIndex] != fullCode[codeIndex]) return;
fprintf(stderr, "found a %c at %d\n", strippedCode[stripIndex], stripIndex);
strippedCode[stripIndex] ^= BREAKPOINT_bm;
fullCode[codeIndex] ^= BREAKPOINT_bm;
}
// The stripper strips code that looks like this:
// ```brainfuck
// +++ Set @0 to 3
// [- > +++ <] Set @1 to 3 * 3 = 9
// ```
// To this: (every character is a byte)
// ```md
// Index: 0123456789abcd
// Code: +++[II->+++]II
// ```
// Here, II contain the index of the corresponding bracket, so it would be:
// `+++[0b->+++]03`
//
// TODO: Count the maximum depth of [loops] and malloc the stack using that number.
// TODO: It's a bit more elegant that way, and you can nest more than 256 times.
uint16_t StripCode(char* code) {
uint16_t strippedSize = 0;
char* c = code;
// Go through the entire code to measure how long our stripped code array needs to be.
while(*c) {
if(strchr(KEY_CHARACTERS, *c)) {
strippedSize++;
// I do the bracket jumping by storing the corresponding bracket for each bracket in the code,
// so we need to make space for that.
if(*c == '[' || *c == ']') strippedSize += 2;
}
c++;
}
// Room for a terminating \0.
strippedSize++;
// Allocate the arrays.
strippedCode = (char *) malloc(strippedSize * sizeof(char));
strippedIndeces = (uint16_t *) calloc(strippedSize, sizeof(uint16_t));
// Go through the code and actually strip it.
uint16_t strippedIndex = 0;
uint16_t codeIndex = 0;
uint16_t bracketStack[MAX_LOOP_DEPTH];
uint16_t *stackPtr = bracketStack;
while(code[codeIndex]) {
if(strchr(KEY_CHARACTERS, code[codeIndex])) {
strippedCode[strippedIndex] = code[codeIndex];
strippedIndeces[strippedIndex] = codeIndex;
// Lots of indentation happening here.
switch(code[codeIndex]) {
case '[':
// Push the index onto the stack.
*stackPtr++ = strippedIndex;
// Continue to next character.
strippedIndex += 2;
break;
case ']':
stackPtr--;
// Set the index of this bracket to the corresponding index from the stack.
// Memcpy copies both bytes of the index (which is 16 bit) in the stack into the code array.
memcpy(strippedCode + strippedIndex + 1, stackPtr, sizeof(*stackPtr));
// Set the index of the opposite bracket to this index.
memcpy(strippedCode + *stackPtr + 1, &strippedIndex, sizeof(*stackPtr));
// Continue to the next character.
strippedIndex += sizeof(*stackPtr);
break;
case '#':
// Set the breakpoint bit in both the stripped and normal code.
strippedCode[strippedIndex] = '#' | BREAKPOINT_bm;
code[codeIndex] = '#' | BREAKPOINT_bm;
break;
}
strippedIndex++;
}
codeIndex++;
}
strippedCode[strippedIndex] = '\0';
return strippedSize;
}
uint16_t GetCodeIndex(void) {
return strippedIndeces[strippedIndex];
}
uint16_t GetMemIndex(void) {
return memoryIndex;
}
uint16_t GetLoopDepth(void) {
return loopDepth;
}
uint8_t* GetMemory(void) {
return memory;
}
// Thanks Michael on SO. I could write this myself but I truly cannot be bothered.
// https://stackoverflow.com/questions/2029103/correct-way-to-read-a-text-file-into-a-buffer-in-c
char *FileToBuffer(const char *fileName) {
FILE *fp = fopen(fileName, "r");
if(fp == NULL) return NULL;
char* buffer = NULL;
if(fp != NULL) {
// Go to the end of the file.
if(fseek(fp, 0L, SEEK_END) == 0) {
// Get the size of the file.
long bufsize = ftell(fp);
if(bufsize == -1) return NULL;
// Allocate our buffer to that size.
buffer = malloc(sizeof(char) * (bufsize + 1));
// Go back to the start of the file.
if(fseek(fp, 0L, SEEK_SET) != 0) return NULL;
// Read the entire file into memory.
size_t newLen = fread(buffer, sizeof(char), bufsize, fp);
if(ferror(fp) != 0) {
fputs("Error reading file", stderr);
return NULL;
}
else (buffer)[newLen++] = '\0'; // Just to be safe.
}
fclose(fp);
}
return buffer;
}