-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.c
168 lines (145 loc) · 3.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
#include <stdio.h> // for basic I/O operation.
#include <stdlib.h> // for memory management.
typedef struct stack {
int value;
struct stack *next;
} stack_node_t;
typedef struct {
stack_node_t *top;
} stack_t;
int pop(stack_t *s);
void push(stack_t *s, int i);
char *bracemap(char *program);
void loadBFtoMemory(FILE *handle);
void process(char c);
const int memorySize = 30000;
char *cell;
int currentCell = 0;
int InstructionPointer = 0;
int i, charCount;
FILE *hInput; // Handle to File object.
char *programArray; // Cleaned up Brainfuck program.
char *braces; // Storage of braces pair location.
char *bracesStack; // temporary array for braces stack.
char _temp;
int main(int argc, char *argv[])
{
// Make sure file path always be specified.
if(argc < 2)
return 0;
cell = calloc(memorySize, sizeof(char));
hInput = fopen(argv[1], "r");
loadBFtoMemory(hInput);
braces = bracemap(programArray);
while(InstructionPointer < charCount)
{
process(programArray[InstructionPointer]);
InstructionPointer++;
}
// printf("\n");
// for(i = 0; i < 20; i++)
// printf("%d|", cell[i]);
// printf("\n");
free(programArray);
fclose(hInput);
return 0;
}
char *bracemap(char *program)
{
int i, start;
char *temp = calloc(charCount, sizeof(char));
stack_t tempStack = {NULL};
for(i = 0; i < charCount; i++)
{
if(programArray[i] == '[')
{
push(&tempStack, i);
start = i;
}
else if(programArray[i] == ']')
{
int start = pop(&tempStack);
temp[start] = i;
temp[i] = start;
}
}
return temp;
}
void process(char c)
{
switch(c)
{
case '+':
cell[currentCell]++;
break;
case '-':
cell[currentCell]--;
break;
case '>':
currentCell++;
break;
case '<':
currentCell--;
break;
case '.':
printf("%c", cell[currentCell]);
break;
case ',':
cell[currentCell] = (char)getchar();
break;
case '[':
InstructionPointer = (cell[currentCell] == 0) ? braces[InstructionPointer] : (InstructionPointer);
break;
case ']':
InstructionPointer = (cell[currentCell] == 0) ? (InstructionPointer) : braces[InstructionPointer];
break;
default: // Ignore other characters.
break;
}
}
void loadBFtoMemory(FILE *handle){
char c;
charCount = 0;
programArray = (char*) malloc((charCount + 1) * sizeof(char));
if(programArray == NULL)
printf("Memory allocation failed.");
else
{
c = fgetc(hInput);
while(c != EOF)
{
switch(c)
{
case '+':
case '-':
case '>':
case '<':
case '.':
case ',':
case '[':
case ']':
programArray[charCount++] = c;
realloc(programArray, (charCount + 1) * sizeof(char));
default:
break;
}
c = (char)fgetc(hInput);
}
}
}
int pop(stack_t *s){
stack_node_t *free_node;
int c;
free_node = s->top;
c = free_node->value;
s->top = free_node->next;
free(free_node);
return c;
}
void push(stack_t *s, int c){
stack_node_t *new_node;
new_node = (stack_node_t *) malloc(sizeof(stack_node_t));
new_node->value = c;
new_node->next = s->top;
s->top = new_node;
}