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 <errno.h>
|
|
24 #include <stdio.h>
|
|
25 #include <stdlib.h>
|
|
26 #include <string.h>
|
|
27
|
|
28 #ifdef HAVE_WCHAR_H
|
|
29 #include <wchar.h>
|
|
30 #endif
|
|
31
|
|
32 #include "bfi.h"
|
|
33 #include "egobfi.h"
|
|
34
|
|
35 void bf_interpret()
|
|
36 {
|
|
37 int input, cur, iseof;
|
|
38 struct bfi *bfic;
|
|
39
|
|
40 while (prog[pptr].cmd != FIN) {
|
|
41 /*printf("%d\n", pptr);*/
|
|
42 bfic = prog + pptr;
|
|
43 switch (bfic->cmd) {
|
|
44 case ADD:
|
|
45 *mptr += bfic->arg1;
|
|
46 pptr++;
|
|
47 break;
|
|
48
|
|
49 case RHT:
|
|
50 mptr += bfic->arg1;
|
|
51 CHKMSZ(mptr)
|
|
52 pptr++;
|
|
53 break;
|
|
54
|
|
55 case LPO:
|
|
56 if (*mptr) {
|
|
57 /* if it's true, go into the loop */
|
|
58 pptr++;
|
|
59 } else {
|
|
60 /* if it's false, jump to the end */
|
|
61 pptr = bfic->arg1 + 1;
|
|
62 }
|
|
63 break;
|
|
64
|
|
65 case LPC:
|
|
66 /* just jump to the beginning */
|
|
67 pptr = bfic->arg1;
|
|
68 break;
|
|
69
|
|
70 case OUT:
|
|
71 /* output a character */
|
|
72 #ifdef HAVE_WCHAR_H
|
|
73 if (unicode) {
|
|
74 putwchar(*mptr);
|
|
75 } else {
|
|
76 putchar(*mptr);
|
|
77 }
|
|
78 #else
|
|
79 putchar(*mptr);
|
|
80 #endif
|
|
81
|
|
82 fflush(stdout);
|
|
83 pptr++;
|
|
84 break;
|
|
85
|
|
86 case INP:
|
|
87 /* input a character */
|
|
88 iseof = 0;
|
|
89 #ifdef HAVE_WCHAR_H
|
|
90 if (unicode) {
|
|
91 input = (int) getwchar();
|
|
92 iseof = (input == (int) WEOF);
|
|
93 } else {
|
|
94 input = getchar();
|
|
95 iseof = (input == EOF);
|
|
96 }
|
|
97 #else
|
|
98 input = getchar();
|
|
99 iseof = (input == EOF);
|
|
100 #endif
|
|
101
|
|
102 if (iseof) {
|
|
103 switch (eof) {
|
|
104 case 0:
|
|
105 *mptr = 0;
|
|
106 break;
|
|
107
|
|
108 case -1:
|
|
109 *mptr = -1;
|
|
110 break;
|
|
111 }
|
|
112 } else {
|
|
113 *mptr = input;
|
|
114 }
|
|
115 pptr++;
|
|
116 break;
|
|
117
|
|
118 case DBG:
|
|
119 pptr++;
|
|
120 if (debug) {
|
|
121 cur = mptr - mem - 10;
|
|
122 if (cur < 0) cur = 0;
|
|
123 printf("%d:", cur);
|
|
124 for (; cur < mptr - mem + 40; cur++) {
|
|
125 if (cur == mptr - mem) putchar('*');
|
|
126 CHKMSZ(mem + cur)
|
|
127 printf("%d|", (int) mem[cur]);
|
|
128 }
|
|
129 putchar('\n');
|
|
130 }
|
|
131 break;
|
|
132
|
|
133 /* INTERNAL COMMANDS */
|
|
134 case ZRO:
|
|
135 *mptr = 0;
|
|
136 pptr++;
|
|
137 break;
|
|
138
|
|
139 case ADDTO:
|
|
140 CHKMSZ(mptr + bfic->arg1)
|
|
141 mptr[bfic->arg1] += bfic->arg2 * *mptr;
|
|
142 *mptr = 0;
|
|
143 pptr++;
|
|
144 break;
|
|
145
|
|
146 default:
|
|
147 pptr++;
|
|
148 }
|
|
149 }
|
|
150 }
|
|
151
|