-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.c
208 lines (192 loc) · 3.27 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
// Brainfuck Interpreter
// by Lucas Menezes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "stack.h"
#define DEFAULT_SIZE 100000000 // less than 100 MB
//#define DEBUG
#ifdef DEBUG
# define DEBUG_PRINT(x) printf x
#else
# define DEBUG_PRINT(x) do {} while (0)
#endif
void printMemory(char *memory,char mode)
{
int i;
printf("Memory\n");
printf("[");
for (i =0 ; i < DEFAULT_SIZE ; i++)
{
if(mode == 'c')
printf("%c ", memory[i]);
else
printf("%d ",memory[i]);
}
printf("]\n");
}
void printAreaNearPtr(char* ptr,char* memory,int offset)
{
int x = ptr - memory;
int i;
if(x > 0 && x < offset)
{
for( i = 0; i < 2* offset; i++)
{
printf("|%d",*(ptr+i));
if(i==0)
printf("*");
}
}
else if (x < 0 && x*x < offset*offset)
{
for( i = -2 * offset; i < 0; i++)
{
printf("|%d",*(ptr+i));
if(i==0)
printf("*");
}
}
else
{
for( i = -offset; i < offset; i++ )
{
printf("|%d",*(ptr+i));
if(i==0)
printf("*");
}
}
printf("|\r");
}
void execute(char* program)
{
Stack* loopStack;
char instructions[] = {'>','<','+','-','.',',','[',']'};
char* ptr;
char c;
char *loopStart = NULL;
int internalLoopCount = 0;
char* memory = (char*) calloc(sizeof(char),DEFAULT_SIZE); // memory of program must be zeroed.
ptr = memory;
Stack_Init(&loopStack);
while(*program)
{
c = *program;
DEBUG_PRINT(("%x\t%c\tptr:\t%x\t{%d}\n",program,c,ptr,*ptr));
//DEBUG_PRINT(("current instruction \'%c\'\t%d\n",c,c));
if (c == instructions[0])
{
++ptr;
}
else if (c == instructions[1])
{
--ptr;
}
else if (c == instructions[2])
{
++*ptr;
}
else if (c == instructions[3])
{
--*ptr;
}
else if (c == instructions[4])
{
putchar(*ptr);
}
else if (c == instructions[5])
{
*ptr=getchar();
}
else if (c == instructions[6])
{
// will enter while
if(*ptr)
{
loopStart = program;
Stack_Push(loopStack,loopStart);
}
else
{
// skip while sequence
program++;
internalLoopCount = 0;
while(!(*program == ']' && internalLoopCount == 0)) // while not match stop requirements
{
if(*program == '[')
{
internalLoopCount++;
}
else if(*program == ']')
{
internalLoopCount--;
}
program++;
}
}
}
else if (c ==instructions[7])
{
// repeats loop
if(*ptr)
{
loopStart = Stack_Top(loopStack);
program = loopStart;
}
else
{
Stack_Pop(loopStack); // out of this loop
}
}
else
{
}
program++;
}
free(memory);
free(loopStack);
}
int errorMessage(int argc, char** argv)
{
printf("Error!\nProgram usage is: \nbfi sourcecode\n");
return -1;
}
char * readFile(char* path)
{
int size;
FILE * input;
char * fileString;
char c;
int i;
input = fopen(path,"rt");
fseek(input, 0, SEEK_END);
size = ftell(input);
rewind(input);
fileString = (char*)malloc(size+1);
i = 0;
while(fscanf(input,"%c",&c) != EOF)
{
fileString[i] = c;
i++;
}
fileString[size] = '\0';
fclose(input);
DEBUG_PRINT(("%s\n",fileString));
return fileString;
}
int main (int argc, char** argv)
{
char * program;
//expects bfi filepath
if(argc == 2)
{
program = readFile(argv[1]);
execute(program);
free(program);
return 0;
}
else
{
return errorMessage(argc, argv);
}
}