comparison interps/egobf/src/bfc.c @ 996:859f9b4339e6

<Gregor> tar xf egobot.tar.xz
author HackBot
date Sun, 09 Dec 2012 19:30:08 +0000
parents
children
comparison
equal deleted inserted replaced
995:6883f5911eb7 996:859f9b4339e6
1 /*
2 * Copyright (c) 2005 Gregor Richards
3 *
4 * This file is part of egobfi.
5 *
6 * egobfi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * egobfi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with egobfi; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdio.h>
22
23 #include "bfc.h"
24 #include "egobfi.h"
25
26 void bf_compile()
27 {
28 char *memtype;
29 struct bfi *bfic;
30
31 switch (bytewidth) {
32 case 1:
33 memtype = "uint8_t";
34 break;
35
36 case 2:
37 memtype = "uint16_t";
38 break;
39
40 case 4:
41 memtype = "uint32_t";
42 break;
43
44 default:
45 memtype = "uint8_t";
46 break;
47 }
48
49 /* header */
50
51 printf("#include <stdio.h>\n#include <stdint.h>\n");
52 #ifdef HAVE_WCHAR_H
53 printf("#include <wchar.h>\n");
54 #endif
55 printf("%s *mem;\n", memtype);
56 printf("int mptr;\n"
57 "int mlen;\n"
58 "int main() {\n"
59 "int input;\n"
60 "int iseof;\n"
61 "mem = (%s *) malloc(30000 * sizeof(%s));\n"
62 "mlen = 30000;\n"
63 "mptr = 0;\n",
64 memtype, memtype);
65
66 printf("#define CHKMSZ(a) \\\n"
67 "if ((a) >= mlen) { \\\n"
68 "mlen += 3000; \\\n"
69 "mem = (%s *) realloc(mem, mlen * sizeof(%s)); \\\n"
70 "if (mem == NULL) { perror(\"realloc\"); exit(1); } \\\n"
71 "memset(mem + mlen - 3000, 0, 3000 * sizeof(%s)); \\\n"
72 "}\n", memtype, memtype, memtype);
73
74 while (prog[pptr].cmd != FIN) {
75 /*printf("%d\n", pptr);*/
76 bfic = prog + pptr;
77 switch (bfic->cmd) {
78 case ADD:
79 printf("mem[mptr] += %d;\n", bfic->arg1);
80 pptr++;
81 break;
82
83 case RHT:
84 printf("mptr += %d; CHKMSZ(mptr)\n", bfic->arg1);
85 pptr++;
86 break;
87
88 case LPO:
89 printf("while (mem[mptr]) {\n");
90 pptr++;
91 break;
92
93 case LPC:
94 printf("}\n");
95 pptr++;
96 break;
97
98 case OUT:
99 /* output a character */
100 #ifdef HAVE_WCHAR_H
101 if (unicode) {
102 printf("putwchar(mem[mptr]);\n");
103 } else {
104 printf("putchar(mem[mptr]);\n");
105 }
106 #else
107 printf("putchar(mem[mptr]);\n");
108 #endif
109
110 printf("fflush(stdout);\n");
111 pptr++;
112 break;
113
114 case INP:
115 /* input a character */
116 printf("iseof = 0;\n");
117 #ifdef HAVE_WCHAR_H
118 if (unicode) {
119 printf("input = (int) getwchar();\n"
120 "iseof = (input == (int) WEOF);\n");
121 } else {
122 printf("input = getchar();\n"
123 "iseof = (input == EOF);");
124 }
125 #else
126 printf("input = getchar();\n"
127 "iseof = (input == EOF);\n");
128 #endif
129
130 printf("if (iseof) {\n");
131 switch (eof) {
132 case 0:
133 printf("mem[mptr] = 0; }\n");
134 break;
135
136 case -1:
137 printf("mem[mptr] = -1; }\n");
138 break;
139 }
140 printf("} else {\n");
141 printf("mem[mptr] = input; }\n");
142 pptr++;
143 break;
144
145 /*case DBG:
146 pptr++;
147 if (debug) {
148 cur = mptr - 10;
149 if (cur < 0) cur = 0;
150 printf("%d:", cur);
151 for (; cur < mptr + 40; cur++) {
152 if (cur == mptr) putchar('*');
153 CHKMSZ(cur)
154 printf("%d|", mem[cur]);
155 }
156 putchar('\n');
157 }
158 break;*/
159
160 /* INTERNAL COMMANDS */
161 case ZRO:
162 printf("mem[mptr] = 0;\n");
163 pptr++;
164 break;
165
166 case ADDTO:
167 printf("CHKMSZ(mptr + %d) ", bfic->arg1);
168 printf("mem[mptr + %d] += %d * mem[mptr]; ", bfic->arg1, bfic->arg2);
169 printf("mem[mptr] = 0;\n");
170 pptr++;
171 break;
172
173 default:
174 pptr++;
175 }
176 }
177
178 printf("return 0;\n}\n");
179 }