-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
194 lines (171 loc) · 4.31 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#define DEBUG 0
#define debug(fmt, ...) \
do { if (DEBUG) printf(fmt, ##__VA_ARGS__); } while (0)
char *code;
char *input;
int tok_ptr = 0;
int line_counter = 1;
int line_position = 0;
int input_ptr = 0;
int bracket_counter = 0;
int *brackets;
int *test;
int puffer;
unsigned char *registers;
unsigned char *r;
void push_bracket(int position) {
*(++brackets) = position;
}
int pop_bracket() {
return *(brackets--);
}
void delete_char(int i)
{
int len = strlen(code);
for (; i < len - 1; i++)
{
code[i] = code[i + 1];
}
code[i] = '\0';
}
char getToken(int skip)
{
char c = code[tok_ptr++];
line_position++;
if (c == '\n')
{
line_counter++;
line_position = 1;
}
return c;
}
int interpret()
{
brackets = calloc(200, sizeof(int));
test = brackets;
registers = calloc(200, sizeof(char));
r = registers;
registers += 100; // Go to center of allocated memory
tok_ptr = 0;
char cur_tok;
int skip = 0;
do
{
cur_tok = getToken(skip);
for(int i = 0; i < 5; i++) {
debug("%d\t", *(test + i));
}
debug("\n");
debug("LAST_BRACKET: %d\n", *brackets);
if (skip != 0)
{
if(cur_tok == ']')
{
skip--;
}
else if (cur_tok == '[')
{
skip++;
}
}
else
{
debug("token: %c, position: %d, skip: %d\n", cur_tok, tok_ptr, skip);
switch (cur_tok)
{
case '+':
(*registers)++;
debug("Increased register at position %p to %d\n", registers, (int)*registers);
break;
case '-':
(*registers)--;
debug("Decreased register at position %p to %d\n", registers, (int)*registers);
break;
case '<':
*registers--;
debug("Switched to previous register\n");
break;
case '>':
*registers++;
debug("Switched to next register\n");
break;
case '.':
debug("Printing register at position %p. Value: %d\n", registers, (int)*registers);
printf("%c", *registers);
break;
case ',':
*registers = input[input_ptr++];
break;
case '[':
bracket_counter++;
if (*registers != 0)
{
debug("pushing bracket at position %d\n", tok_ptr);
push_bracket(tok_ptr);
}
else
{
skip = 1;
}
break;
case ']':
bracket_counter--;
puffer = pop_bracket();
if (*registers != 0)
{
debug("returning to last bracket at position %d\n", puffer);
tok_ptr = puffer;
tok_ptr--;
}
break;
case '\0':
if (bracket_counter != 0)
{
debug("ERROR: Unclosed bracket\n");
return 1;
}
printf("\n");
break;
default:
/* printf("Got different char in line %d at position %d: %c\n", line_counter, line_position, cur_tok); */
break;
}
}
} while (cur_tok != '\0');
free(r);
return 0;
}
void showHelp()
{
printf("Usage: compiler \"[brainfuck_code]\" \"[input]\"\n");
}
int main(int argc, char *argv[])
{
if (argc == 1 || strcmp(argv[1], "--help") == 0)
{
showHelp();
return 0;
}
struct stat buffer;
if (stat(argv[1], &buffer) == 0)
{
FILE *file = fopen(argv[1], "r");
fseek(file, 0, SEEK_END);
long fsize = ftell(file);
fseek(file, 0, SEEK_SET);
code = malloc(fsize + 1);
fread(code, 1, fsize, file);
fclose(file);
}
else
{
code = argv[1];
}
input = argv[2];
debug("%s\n", code);
return interpret();
}