-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbfc.c
212 lines (169 loc) · 3.21 KB
/
bfc.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (c) 2021 Brian Callahan <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "bfc.h"
/*
* bfc -- brainfuck compiler
*/
int l, m, target = TARGET;
unsigned char label[256];
static void
mismatch(void)
{
dputs("bfc: bracket mismatch\n", STDERR_FILENO);
exit(1);
}
static void
prologue(int fd)
{
cg_prologue(fd);
}
static void
epilogue(int fd)
{
cg_epilogue(fd);
}
static void
left(int fd)
{
cg_left(fd);
}
static void
right(int fd)
{
cg_right(fd);
}
static void
inc(int fd)
{
cg_inc(fd);
}
static void
dec(int fd)
{
cg_dec(fd);
}
static void
_getchar(int fd)
{
cg_getchar(fd);
}
static void
_putchar(int fd)
{
cg_putchar(fd);
}
static void
open_loop(int fd)
{
cg_open_loop(fd);
}
static void
close_loop(int fd)
{
cg_close_loop(fd);
}
static int
cross(const char *s)
{
if ((!strcmp("amd64", s)) || (!strcmp("x86_64", s))) {
return TAMD64;
} else if ((!strcmp("i386", s)) || (!strcmp("i486", s)) ||
(!strcmp("i586", s)) || (!strcmp("i686", s)) ||
(!strcmp("i786", s))) {
return TI386;
} else if ((!strcmp("8080", s)) || (!strcmp("i80", s)) ||
(!strcmp("z80", s))) {
return TI80;
} else if ((!strcmp("C", s)) || (!strcmp("c", s))) {
return TC;
}
/* No match? Native compiler. */
return target;
}
static void __dead
usage(void)
{
dputs("usage: bfc [-t target] file\n", STDERR_FILENO);
exit(1);
}
int
main(int argc, char *argv[])
{
char c;
int ch, d = 0, fd;
if (pledge("stdio rpath wpath cpath", NULL) == -1)
usage();
if ((ch = getopt(argc, argv, "t:")) != -1) {
switch (ch) {
case 't':
target = cross(optarg);
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 1)
usage();
if ((fd = open(*argv++, O_RDONLY)) == -1) {
dputs("bfc: could not open input file\n", STDERR_FILENO);
return 1;
}
if (pledge("stdio", NULL) == -1)
usage();
prologue(STDOUT_FILENO);
while (read(fd, &c, STDOUT_FILENO) == 1) {
switch (c) {
case '<':
left(STDOUT_FILENO);
break;
case '>':
right(STDOUT_FILENO);
break;
case '+':
inc(STDOUT_FILENO);
break;
case '-':
dec(STDOUT_FILENO);
break;
case ',':
_getchar(STDOUT_FILENO);
break;
case '.':
_putchar(STDOUT_FILENO);
break;
case '[':
++d;
open_loop(STDOUT_FILENO);
break;
case ']':
if (--d < 0)
mismatch();
close_loop(STDOUT_FILENO);
}
}
if (d != 0)
mismatch();
epilogue(STDOUT_FILENO);
close(fd);
return 0;
}