996
|
1 /*
|
|
2 * Copyright (C) 2005 Gregor Richards
|
|
3 *
|
|
4 * This file is part of EgoBCh.
|
|
5 *
|
|
6 * EgoBCh 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 * EgoBCh 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 EgoBCh; 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 #include <stdlib.h>
|
|
23 #include <string.h>
|
|
24
|
|
25 #include "bchi.h"
|
|
26 #include "egobch.h"
|
|
27 #include "optimize.h"
|
|
28
|
|
29 char *iprog;
|
|
30 int ipptr;
|
|
31
|
|
32 struct bchi *prog;
|
|
33 int pptr;
|
|
34
|
|
35 unsigned char *mem;
|
|
36 unsigned char *mptr;
|
|
37 unsigned char membit;
|
|
38
|
|
39 int lazy_io = 0;
|
|
40 int debug = 0;
|
|
41
|
|
42 int main(int argc, char **argv)
|
|
43 {
|
|
44 int i;
|
|
45 FILE *bchin;
|
|
46 char *bchins = NULL;
|
|
47
|
|
48 for (i = 1; i < argc; i++) {
|
|
49 if (argv[i][0] == '-') {
|
|
50 if (!strcmp(argv[i], "-lazy-io")) {
|
|
51 i++;
|
|
52 if (!strcmp(argv[i], "on"))
|
|
53 lazy_io = 1;
|
|
54 } else if (!strcmp(argv[i], "-debug")) {
|
|
55 debug = 1;
|
|
56 } else if (!strcmp(argv[i], "--help") ||
|
|
57 !strcmp(argv[i], "-help") ||
|
|
58 !strcmp(argv[i], "-h")) {
|
|
59 fprintf(stderr,
|
|
60 "Use: egobchi <options> <input file>\n"
|
|
61 "Options:\n"
|
|
62 " -lazy-io <on|off>\n"
|
|
63 " allow the use of . and , for IO (not standard BitChanger) [default off]\n"
|
|
64 " -debug\n"
|
|
65 " allow # debugging\n");
|
|
66 exit(0);
|
|
67 } else {
|
|
68 fprintf(stderr, "Unrecognized option: %s\n", argv[i]);
|
|
69 exit(1);
|
|
70 }
|
|
71 } else {
|
|
72 bchins = argv[i];
|
|
73 }
|
|
74 }
|
|
75
|
|
76 /* now allocate memory */
|
|
77 iprog = (char *) malloc(30000);
|
|
78 ipptr = 0;
|
|
79 prog = (struct bchi *) malloc(30000 * sizeof(struct bchi));
|
|
80 pptr = 0;
|
|
81 mem = (unsigned char *) malloc(30000);
|
|
82 memset(mem, 0, 30000);
|
|
83 mptr = mem + 2;
|
|
84 membit = 0x80;
|
|
85
|
|
86 /* read the file */
|
|
87 if (bchins) {
|
|
88 bchin = fopen(bchins, "r");
|
|
89 if (bchin == NULL) {
|
|
90 perror(bchins);
|
|
91 exit(1);
|
|
92 }
|
|
93 } else {
|
|
94 bchin = stdin;
|
|
95 }
|
|
96
|
|
97 while (!feof(bchin) && !ferror(bchin)) {
|
|
98 iprog[ipptr] = fgetc(bchin);
|
|
99 if ISBCH(iprog[ipptr]) {
|
|
100 ipptr++;
|
|
101 }
|
|
102 }
|
|
103 iprog[ipptr] = '\0';
|
|
104
|
|
105 /* "optimize" (which really just matches loops) */
|
|
106 ipptr = 0;
|
|
107 optimize();
|
|
108 if (pptr != 0 && prog[0].cmd != FIN) {
|
|
109 fprintf(stderr, "Unmatched loop!\n");
|
|
110 exit(1);
|
|
111 }
|
|
112
|
|
113 /* interpret */
|
|
114 pptr = 0;
|
|
115 bch_interpret();
|
|
116
|
|
117 return 0;
|
|
118 }
|