-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbf.c
More file actions
234 lines (211 loc) · 3.58 KB
/
bf.c
File metadata and controls
234 lines (211 loc) · 3.58 KB
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
// By Pytel
/*
Based on code by:
@maxcountryman
@himehowareu
@D0tty
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define OK 0
#define NOK -1
// help
void help(char *path_to_interpreter)
{
printf("Usage: %s file.bf\n", path_to_interpreter);
}
/*
* Returns the size of the file in bytes
*/
long int file_size(FILE *file)
{
fseek(file, 0L, SEEK_END);
long int file_len = ftell(file);
rewind(file);
return file_len;
}
/*
* Reads the file and returns the size of the file in bytes
* Returns -1 on error
*/
int read_file(char *path, char **buffer)
{
FILE *file = fopen(path, "r");
if (file == NULL)
{
return NOK;
}
long file_len = file_size(file);
char *file_buf = malloc(file_len * sizeof(char) + 1);
file_buf[file_len] = '\0';
fread(file_buf, file_len, sizeof(char), file);
fclose(file);
*buffer = file_buf;
return file_len;
}
char *add_cell(char *tape, size_t *tape_size)
{
char *tmp = (char *)realloc(tape, (++*tape_size) * sizeof(char));
if (tmp == NULL)
{
printf("FATAL: Could not reallocate tape memory!");
exit(1);
}
tmp[*tape_size - 1] = 0;
return tmp;
}
char *remove_cell(char *tape, size_t *tape_size, char *ptr)
{
char* tail = &tape[*tape_size - 1];
// tape is empty
if (*tape_size < 1)
{
return tape;
}
// if the last cell is not zeroed
else if (*tail != 0)
{
return tape;
}
// if the cell is pointed to currently
else if (ptr == tail)
{
return tape;
}
// remove last cell
char *tmp = (char *)realloc(tape, (--*tape_size) * sizeof(char));
if (tmp == NULL)
{
printf("FATAL: Could not reallocate tape memory!");
exit(1);
}
return tmp;
}
void interpret(char *input)
{
size_t tape_size = 1;
char *tape;
char *ptr;
long int index = 0;
char current_char;
size_t i;
size_t loop;
tape = (char *)calloc(tape_size, sizeof(char));
ptr = tape;
for (i = 0; input[i] != 0; i++)
{
current_char = input[i];
if (current_char == '>')
{
++index;
if (index >= tape_size)
{
tape = add_cell(tape, &tape_size);
}
++ptr;
}
else if (current_char == '<')
{
--index;
if (index < 0)
{
printf("FATAL: Tape index out of bounds!");
free(tape);
exit(1);
}
--ptr;
}
else if (current_char == '+')
{
++*ptr;
}
else if (current_char == '-')
{
--*ptr;
}
else if (current_char == '.')
{
putchar(*ptr);
}
else if (current_char == ',')
{
*ptr = getchar();
}
else if (current_char == '[' && *ptr == 0)
{ // jump behind loop
loop = 1;
while (loop > 0)
{
current_char = input[++i];
if (current_char == '[')
{
loop++;
}
else if (current_char == ']')
{
loop--;
}
}
}
else if (current_char == '[')
{ // do loop
continue;
}
else if (current_char == ']' && *ptr)
{ // repeat loop
loop = 1;
while (loop > 0)
{
current_char = input[--i];
if (current_char == '[')
{
loop--;
}
else if (current_char == ']')
{
loop++;
}
}
}
else
{ // random chars and comments
continue;
}
// garbage collect
tape = remove_cell(tape, &tape_size, ptr);
}
free(tape);
}
int main(int argc, char *argv[])
{
// check args
if (argc != 2)
{
help(argv[0]);
exit(2);
}
// load file
char *file_buf = NULL;
long file_len = read_file(argv[1], &file_buf);
// check if file is empty or not found
if (file_len <= 0)
{
printf("Error: fopen failed!\n");
exit(1);
}
// check if buffer is empty
if (file_buf == NULL)
{
printf("Error: file reading failed!\n");
exit(3);
}
// interpret
interpret(file_buf); // outputs input
// free memory
free(file_buf);
return 0;
}
/*
END
*/