-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrainfuck.c
142 lines (126 loc) · 2.67 KB
/
brainfuck.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#define MemoryCell struct _memorycell
#define S_MEMORYCELL sizeof(MemoryCell)
#define S_CHAR sizeof(char)
MemoryCell {
MemoryCell *next;
MemoryCell *previous;
unsigned char value;
};
MemoryCell *nextCell(MemoryCell *current) {
if (current->next == NULL) {
current->next = (MemoryCell*)malloc(S_MEMORYCELL);
current->next->previous = current;
current->next->next = NULL;
current->next->value = 0;
}
return current->next;
}
MemoryCell *previousCell(MemoryCell *current) {
if (current->previous == NULL) {
current->previous = (MemoryCell*)malloc(S_MEMORYCELL);
current->previous->next = current;
current->previous->previous = NULL;
current->previous->value = 0;
}
return current->previous;
}
void freeMemory(MemoryCell *current) {
while (current->next != NULL)
current = current->next;
while (current->previous != NULL) {
current = current->previous;
free(current->next);
}
free(current);
}
int usage(char *self) {
printf("Usage: %s program.bf\n", self);
return 1;
}
char *extendString(char *string, char new) {
int l = strlen(string);
string = realloc(string, l + 2);
string[l] = new;
string[l + 1] = 0;
return string;
}
int forward(char *source, int i) {
while (source[i] != ']')
if (source[i] == '[')
i = forward(source, i + 1) + 1;
else
i++;
return i;
}
int backward(char *source, int i) {
while (source[i] != '[')
if (source[i] == ']')
i = backward(source, i - 1) - 1;
else
i--;
return i;
}
int main(int argc, char **argv) {
if (argc != 2)
return usage(argv[0]);
char c = 0, *source = malloc(S_CHAR);
source[0] = 0;
int i = 0, sourceLength = 0;
MemoryCell *memory = (MemoryCell*)malloc(S_MEMORYCELL);
memory->next = NULL;
memory->previous = NULL;
memory->value = 0;
FILE *f = fopen(argv[1], "r");
while ( (c = fgetc(f)) != EOF )
source = extendString(source, c);
fclose(f);
sourceLength = strlen(source);
c = 0;
for (i = 0; i < sourceLength; i++) {
switch (source[i]) {
case '>':
memory = nextCell(memory);
break;
case '<':
memory = previousCell(memory);
break;
case '+':
memory->value++;
break;
case '-':
memory->value--;
break;
case '.':
fputc(memory->value, stdout);
break;
case ',':
c = fgetc(stdin);
if (c != EOF) {
memory->value = c;
c = 0;
}
break;
case '[':
if (memory->value == 0)
i = forward(source, ++i);
break;
case ']':
if (memory->value != 0)
i = backward(source, --i);
break;
default:
break;
}
if (c == EOF)
break;
}
freeMemory(memory);
free(source);
return 0;
}