-
Notifications
You must be signed in to change notification settings - Fork 0
/
bran.c
223 lines (198 loc) · 5.85 KB
/
bran.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned char INPUTMODE;
#define DEFAULT_MODE (INPUTMODE)0
#define SPECIAL_MODE (INPUTMODE)1
#define CHAR_MODE (INPUTMODE)2
#define MARKER_DEFINE_MODE (INPUTMODE)3
#define MARKER_JUMP_MODE (INPUTMODE)4
static void close_input(void);
static void output_indents(int);
static void output(const char* template, ...);
static void outputboilerplate_before(void);
static void outputboilerplate_after(void);
static int compile(FILE* input);
static INPUTMODE process_normal(char);
static void process_special(char);
static void process_char(char);
static void process_markerdefine(char, char* marker_information);
static void process_markerjump(char, char* marker_information);
bool errorhandler_defined;
int currentindent = 0;
FILE* input;
static void close_input(void)
{
fclose(input);
}
static void output_indents(int amount)
{
int current;
for (current = 0; current < amount; current++) {
printf("\t");
}
}
static void output(const char* template, ...)
{
va_list list;
output_indents(currentindent);
va_start(list, template);
vprintf(template, list);
va_end(list);
printf("\n");
}
static void outputboilerplate_before(void)
{
output("/* generated by bran */");
output("#include <stdio.h>");
output("");
output("char buffer[1024];");
output("");
output("int main(void)");
output("{");
currentindent += 1;
output("register unsigned int offset = 0;");
output("unsigned int iregisters[8]; /* integer registers */");
output("char* sregisters[8]; /* string registers (not null-terminated) */");
output("unsigned char lregisters[8]; /* string length registers */");
}
static void outputboilerplate_after(void)
{
output("\treturn 0;");
output("}");
output("/* eof */");
}
int main(int argc, char** argv)
{
FILE* input = stdin;
if (argc > 1)
input = fopen(argv[1], "r");
if (input == NULL) {
perror(argv[0]);
return 10 + errno;
}
atexit(close_input);
return compile(input);
}
int compile(FILE* input)
{
char current; /* stores the current character */
INPUTMODE mode = DEFAULT_MODE; /* stores the current parser state */
int marker[255]; /* stores the current version of each marker, to allow for duplicates */
outputboilerplate_before();
while ((current = fgetc(input)) != EOF) {
switch (mode) {
case DEFAULT_MODE:
mode = process_normal(current);
break;
case SPECIAL_MODE:
process_special(current);
mode = DEFAULT_MODE;
break;
case CHAR_MODE:
process_char(current);
mode = DEFAULT_MODE;
break;
case MARKER_DEFINE_MODE:
process_markerdefine(current, (char*)&marker);
mode = DEFAULT_MODE;
break;
case MARKER_JUMP_MODE:
process_markerjump(current, (char*)&marker);
mode = DEFAULT_MODE;
break;
}
}
outputboilerplate_after();
return EXIT_SUCCESS;
}
static INPUTMODE process_normal(char current)
{
switch (current) {
case '>':
output("offset++;");
break;
case '<':
output("offset--;");
break;
case '+':
output("buffer[offset]++;");
break;
case '-':
output("buffer[offset]--;");
break;
case '.':
output("printf(\"%%c\", buffer[offset]);");
break;
case ',':
output("buffer[offset] = getchar();");
if (errorhandler_defined) {
output("if (buffer[offset] == EOF) {");
currentindent += 1;
output("\tbuffer[offset] = errno;");
output("\tgoto marker_E;");
currentindent -= 1;
output("}");
} else {
output("if (buffer[offset] == EOF) buffer[offset] = 0;");
}
break;
case '[':
output("while (buffer[offset] != 0)");
output("{");
currentindent += 1;
break;
case ']': {
currentindent -= 1;
output("} /* end while */");
break;
}
case '$':
return SPECIAL_MODE;
case '%':
output("printf(\"%%.*s\", lregisters['Z' - 'S'], sregisters['Z' - 'S']);");
break;
case '^':
return MARKER_JUMP_MODE;
case '*':
return MARKER_DEFINE_MODE;
case '\'':
return CHAR_MODE;
}
return DEFAULT_MODE;
}
static void process_special(char current)
{
if (current >= 'a' && current <= 'f') {
output("iregisters['%c' - 'a'] = buffer[offset];", current);
} else if (current >= 'A' && current <= 'F') {
output("buffer[offset] = iregisters['%c' - 'A'];", current);
} else if (current >= 's' && current <= 'z') {
output("lregisters['%c' - 's'] = buffer[offset];", current);
output("sregisters['%c' - 's'] = (&buffer[offset]) - buffer[offset];", current);
} else if (current >= 'S' && current <= 'Z') {
output("if (1) {");
currentindent += 1;
output("unsigned char len = lregisters['%c' - 'S'];", current);
output("register unsigned char position;");
output("for (position = 0; position < len; position++) buffer[offset - len + position] = sregisters['%c' - 'S'][position];", current - 'S');
currentindent -= 1;
output("} /* end scope */");
}
}
static void process_char(char current)
{
output("buffer[offset] = %d;", current);
}
static void process_markerjump(char current, char* marker_information)
{
output("goto marker_%c_%d;", current, marker_information[(unsigned char)current]);
}
static void process_markerdefine(char current, char* marker_information)
{
errorhandler_defined |= current == 'E';
output("marker_%c_%d:", current, ++marker_information[(unsigned char)current]);
}