bf.c (1477B)
1 #include <stdio.h> 2 #include <string.h> 3 4 int 5 main() { 6 FILE *in = stdin, *out = stdout; 7 int c, cdepth = 0, hidepth = 0; 8 9 fprintf(out, "(unix core) |0x0 @stdin |0x1 @stdout |0x2 @stderr |0x3 @syscall |0x4 @arg1 |0x8 @arg2 |0xc @arg3 |0x10 @arg4 |0x14 @result |0x18 @fd |0x1c @read |0x1d @write\n"); 10 fprintf(out, "|0x30 @ptr |0x34\n"); 11 fprintf(out, "@arr\n"); 12 fprintf(out, "|0x1000 @main\n&arr &ptr storei\n"); 13 14 for (c; c != EOF; c = getc(in)) { 15 switch (c) { 16 case '+': 17 fprintf(out, "&ptr loadi loadb b2i\n"); 18 fprintf(out, "#1 add i2b\n"); 19 fprintf(out, "&ptr loadi storeb\n"); 20 break; 21 case '-': 22 fprintf(out, "&ptr loadi loadb b2i\n"); 23 fprintf(out, "#1 sub i2b\n"); 24 fprintf(out, "&ptr loadi storeb\n"); 25 break; 26 case '>': 27 fprintf(out, "&ptr loadi #1 add\n"); 28 fprintf(out, "&ptr storei\n"); 29 break; 30 case '<': 31 fprintf(out, "&ptr loadi #1 sub\n"); 32 fprintf(out, "&ptr storei\n"); 33 break; 34 case '.': 35 fprintf(out, "&ptr loadi loadb &stdout storeb\n"); 36 break; 37 case ',': 38 fprintf(out, "&stdin loadb b2i\n"); 39 fprintf(out, "&ptr loadi storei\n"); 40 break; 41 42 case '[': 43 fprintf(out, "@start%d\n", hidepth); 44 fprintf(out, "&ptr loadi loadb b2i #0 equ\n"); 45 fprintf(out, "&end%d jnz\n", cdepth); 46 hidepth++; 47 cdepth++; 48 break; 49 case ']': 50 cdepth--; 51 fprintf(out, "&start%d jmp\n", cdepth); 52 fprintf(out, "@end%d\n", cdepth); 53 break; 54 } 55 } 56 fprintf(out, "halt\n"); 57 }