-
Notifications
You must be signed in to change notification settings - Fork 0
/
bf3.c
44 lines (38 loc) · 1.09 KB
/
bf3.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
#include <stdio.h>
#include <string.h>
#include <libtcc.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
return 1;
}
FILE *f = fopen(argv[1], "r");
if (!f) {
perror("fopen");
return 1;
}
TCCState *tcc = tcc_new();
tcc_set_options(tcc, "-nostdlib");
tcc_add_symbol(tcc, "p", putchar);
tcc_add_symbol(tcc, "g", getchar);
int c;
char bfcode[100000] = "int g(); int p(int); void bf(unsigned char *m) {";
while ((c = fgetc(f)) != EOF) {
switch (c) {
case '+': strcat(bfcode, "++*m;"); break;
case '-': strcat(bfcode, "--*m;"); break;
case '<': strcat(bfcode, "--m;"); break;
case '>': strcat(bfcode, "++m;"); break;
case '.': strcat(bfcode, "p(*m);"); break;
case ',': strcat(bfcode, "*m=g();"); break;
case '[': strcat(bfcode, "while(*m){"); break;
case ']': strcat(bfcode, "}"); break;
}
}
strcat(bfcode, "}");
tcc_compile_string(tcc, bfcode);
tcc_relocate(tcc, TCC_RELOCATE_AUTO);
void (*bf)(unsigned char *) = tcc_get_symbol(tcc, "bf");
unsigned char mem[30000] = {0};
bf(mem);
tcc_delete(tcc);
}