996
|
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 "../config.h"
|
|
22
|
|
23 #include <stdio.h>
|
|
24 #include <stdlib.h>
|
|
25 #include <string.h>
|
|
26
|
|
27 #ifdef HAVE_WCHAR_H
|
|
28 #include <wchar.h>
|
|
29 #endif
|
|
30
|
|
31 #ifndef EGOBFC
|
|
32 #include "bfi.h"
|
|
33 #else
|
|
34 #include "bfc.h"
|
|
35 #endif
|
|
36 #include "egobfi.h"
|
|
37 #ifndef EGOBFC2M
|
|
38 #include "optimize.h"
|
|
39 #else
|
|
40 #include "c2m.h"
|
|
41 #endif
|
|
42 /*#include "optim2.h"*/
|
|
43
|
|
44 int wrapping = 1;
|
|
45 int debug = 0;
|
|
46 int eof = 0;
|
|
47 int compile = 0;
|
|
48 int unicode = 0;
|
|
49
|
|
50 #ifdef EGOBFC
|
|
51 int bytewidth = 1;
|
|
52 #else
|
|
53 USEINT bwmax;
|
|
54 USEINT *mem;
|
|
55 int mlen;
|
|
56 USEINT *mend;
|
|
57 USEINT *mptr;
|
|
58 #endif
|
|
59
|
|
60 char *iprog;
|
|
61 int iplen;
|
|
62 int ipptr;
|
|
63
|
|
64 #ifndef EGOBFC2M
|
|
65 struct bfi *prog;
|
|
66 int plen;
|
|
67 int pptr;
|
|
68 #else
|
|
69 void (*prog)();
|
|
70 void (*tprog)();
|
|
71 int plen;
|
|
72 int pptr;
|
|
73 #endif
|
|
74
|
|
75 int main(int argc, char **argv)
|
|
76 {
|
|
77 int i;
|
|
78 FILE *bfin = stdin;
|
|
79
|
|
80 for (i = 1; i < argc; i++) {
|
|
81 if (argv[i][0] == '-') {
|
|
82 if (!strcmp(argv[i], "-wrap")) {
|
|
83 /* set wrapping on or off */
|
|
84 i++;
|
|
85 if (!strcmp(argv[i], "off"))
|
|
86 wrapping = 0;
|
|
87 } else if (!strcmp(argv[i], "-debug")) {
|
|
88 debug = 1;
|
|
89 } else if (!strcmp(argv[i], "-eof")) {
|
|
90 /* choose eof mode */
|
|
91 i++;
|
|
92 switch (argv[i][0]) {
|
|
93 case '-':
|
|
94 eof = -1;
|
|
95 break;
|
|
96
|
|
97 case 'n':
|
|
98 case 'N':
|
|
99 eof = 1; /* 1 means no change */
|
|
100 break;
|
|
101 }
|
|
102 #ifdef HAVE_WCHAR_H
|
|
103 } else if (!strcmp(argv[i], "-unicode")) {
|
|
104 i++;
|
|
105 if (!strcmp(argv[i], "on")) {
|
|
106 /* make sure it's supported at this level */
|
|
107 int chk;
|
|
108 #ifndef EGOBFC
|
|
109 chk = sizeof(USEINT);
|
|
110 #else
|
|
111 chk = bytewidth;
|
|
112 #endif
|
|
113 if(sizeof(wint_t) > chk) {
|
|
114 fprintf(stderr, "Sorry, I can't support unicode with this bitwidth!\n");
|
|
115 exit(1);
|
|
116 }
|
|
117 unicode = 1;
|
|
118 }
|
|
119 #endif
|
|
120 #ifdef EGOBFC
|
|
121 } else if (!strcmp(argv[i], "-width")) {
|
|
122 i++;
|
|
123 bytewidth = atoi(argv[i]) >> 3;
|
|
124 #endif
|
|
125 } else if(!strcmp(argv[i], "-h") ||
|
|
126 !strcmp(argv[i], "-help") ||
|
|
127 !strcmp(argv[i], "--help")) {
|
|
128 /* display help */
|
|
129 fprintf(stderr, "Use: %s [options] [file]\n"
|
|
130 "Options:\n"
|
|
131 " -eof {0|-|n}\n"
|
|
132 " set EOF mode: 0, -1 or no-change (respectively)\n"
|
|
133 " [default: 0]\n"
|
|
134 " -debug\n"
|
|
135 " activate the # command [default off]\n"
|
|
136 #ifndef EGOBFC2M
|
|
137 " -unicode {on|off}\n"
|
|
138 " set unicode mode on or off [default off]\n"
|
|
139 #ifndef EGOBFC
|
|
140 " -wrap {on|off}\n"
|
|
141 " set wrappong on or off [default on]\n"
|
|
142 #endif
|
|
143 #endif
|
|
144
|
|
145 #ifdef EGOBFC
|
|
146 " -width {8|16|32}\n"
|
|
147 " set the bitwidth [default 8]\n"
|
|
148 #endif
|
|
149 ,
|
|
150 #ifndef EGOBFC
|
|
151 #ifndef EGOBFC2M
|
|
152 /* egobfi */ "egobfi{width}"
|
|
153 #else
|
|
154 /* egobfc2m */ "egobfc2m"
|
|
155 #endif
|
|
156 #else
|
|
157 /* egobfc */ "egobfc"
|
|
158 #endif
|
|
159 );
|
|
160 exit(0);
|
|
161 } else {
|
|
162 fprintf(stderr, "Argument not recognized: %s\n", argv[i]);
|
|
163 exit(1);
|
|
164 }
|
|
165 } else {
|
|
166 /* this is our input file */
|
|
167 bfin = fopen(argv[i], "r");
|
|
168 if (bfin == NULL) {
|
|
169 perror(argv[i]);
|
|
170 exit(1);
|
|
171 }
|
|
172 }
|
|
173 }
|
|
174
|
|
175 /* get our max */
|
|
176 #ifndef EGOBFC
|
|
177 bwmax = (USEINT) -1;
|
|
178 #endif
|
|
179
|
|
180 /* initialize our memory */
|
|
181 #ifndef EGOBFC
|
|
182 mem = (USEINT *) malloc(30000 * sizeof(USEINT));
|
|
183 mlen = 30000;
|
|
184 mend = mem + mlen;
|
|
185 mptr = mem;
|
|
186 #endif
|
|
187 iprog = (char *) malloc(30000);
|
|
188 iplen = 30000;
|
|
189 ipptr = 0;
|
|
190 #ifndef EGOBFC2M
|
|
191 prog = (struct bfi *) malloc(30000 * sizeof(struct bfi));
|
|
192 plen = 30000;
|
|
193 pptr = 0;
|
|
194 #else
|
|
195 prog = (void *) malloc(250000);
|
|
196 tprog = prog;
|
|
197 plen = 250000;
|
|
198 pptr = 0;
|
|
199 #endif
|
|
200
|
|
201 /* 0 it all */
|
|
202 #ifndef EGOBFC
|
|
203 memset(mem, 0, mlen * sizeof(USEINT));
|
|
204 #endif
|
|
205 memset(iprog, 0, iplen);
|
|
206 #ifndef EGOBFC2M
|
|
207 memset(prog, 0, plen * sizeof(struct bfi));
|
|
208 #endif
|
|
209
|
|
210 /* read in our BF */
|
|
211 while (!feof(bfin) && !ferror(bfin)) {
|
|
212 iprog[ipptr] = getc(bfin);
|
|
213 if (ISBF(iprog[ipptr])) {
|
|
214 ipptr++;
|
|
215 CHKIPSZ(ipptr)
|
|
216 }
|
|
217 }
|
|
218 ipptr = 0;
|
|
219
|
|
220 #ifndef EGOBFC2M
|
|
221 /* optimize */
|
|
222 optimize();
|
|
223 /*optim2(); This seems to slow it down more than anything ...*/
|
|
224
|
|
225 #ifndef EGOBFC
|
|
226 /* and interpret */
|
|
227 bf_interpret();
|
|
228 #else
|
|
229 /* and compile */
|
|
230 bf_compile();
|
|
231 #endif
|
|
232 #else
|
|
233 /* compile to memory and run */
|
|
234 c2m();
|
|
235 #endif
|
|
236
|
|
237 return 0;
|
|
238 }
|