-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbfuck.c
189 lines (163 loc) · 4.04 KB
/
bfuck.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* bfuck.c - brainfuck interpreter
* Copyright (C) 2022 Werner Koch
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* For sample programs see:
* https://git.gnupg.org/cgi-bin/gitweb.cgi?p=wk-misc.git;a=tree;f=data
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#define PGM "bfuck"
#define DATASIZE 1000
#define STACKSIZE 10
static int *code;
static size_t codesize, codelen;
static int data[DATASIZE];
static int *stack[STACKSIZE];
static int stackidx;
static int
read_file (const char *fname)
{
FILE *fp;
int c;
fp = fopen (fname, "r");
if (!fp)
{
fprintf (stderr, PGM": error opening '%s': %s\n",
fname, strerror (errno));
return -1;
}
do
{
c = getc (fp);
if (c == EOF && ferror (fp))
{
fprintf (stderr, PGM": error reading '%s': %s\n",
fname, strerror (errno));
fclose (fp);
return -1;
}
if (!code || codelen >= codesize)
{
codesize += 1024;
code = realloc (code, codesize * sizeof *code);
if (!code)
{
fprintf (stderr, PGM": out of code reading '%s'\n", fname);
fclose (fp);
return -1;
}
}
code[codelen++] = c;
}
while (c != EOF);
fclose (fp);
return 0;
}
int
main (int argc, char **argv)
{
int *ip, *ipend;
int *dp, *dpend;
int lnr;
if (argc)
{
argc--;
argv++;
}
while (argc--)
if (read_file (*argv++))
return 2;
if (!code || !codelen)
{
fprintf (stderr, PGM ": no program to execute\n");
return 1;
}
setbuf (stdin, NULL);
setbuf (stdout, NULL);
ip = code;
ipend = code + codelen - 1;
dp = data;
dpend = data + DATASIZE - 1;
lnr = 1;
for ( ; ip <= ipend; ip++)
{
switch (*ip)
{
case EOF: /* Prepare for a next file. */
putchar ('\n');
memset (data, 0, DATASIZE * sizeof *data);
dp = data;
dpend = data + DATASIZE - 1;
stackidx = 0;
break;
case '>':
if (dp < dpend)
dp++;
else
{
fprintf (stderr, PGM ": pointer overflow at line %d\n", lnr);
return 2;
}
break;
case '<':
if (dp > data)
dp--;
else
{
fprintf (stderr, PGM ": pointer underflow at line %d\n", lnr);
return 2;
}
break;
case '+': ++*dp; break;
case '-': --*dp; break;
case '.': putchar (*dp); break;
case ',': *dp = getchar (); break;
case '[':
if (stackidx+1 >= STACKSIZE)
{
fprintf (stderr, PGM ": loop too deeply nested at line %d\n",lnr);
return 2;
}
else
{
stack[stackidx] = ip;
stackidx++;
}
break;
case ']':
if (!stackidx)
{
fprintf (stderr, PGM ": no open loop at line %d\n", lnr);
return 2;
}
else
{
stackidx--;
if (*dp)
{
ip = stack[stackidx];
ip--; /* fix the bump in the for(). */
}
}
break;
case '\n': lnr++; break;
default: break;
}
}
return 0;
}