-
Notifications
You must be signed in to change notification settings - Fork 0
/
yeti.c
198 lines (172 loc) · 4.28 KB
/
yeti.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
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include "common.h"
#define ONLY(A) if (action != A) \
_abort("multiple actions cannot be used at the same time (-e, -d, -i)", -1);
/* available actions */
enum {
COMPILE = 1,
EMIT,
INTERPRET,
DEBUG
};
static char tmpdefault[17] = "/tmp/yetiXXXXXX";
static int verbose = 0;
static int from_stdin = 0;
static int action = COMPILE;
static void
help()
{
fprintf(stderr, "USAGE:\tyeti [OPTIONS] [FILE|-]\n\n" \
"OPTIONS:\n" \
"\t-\tRead from stdin.\n"
"\t-o\tOutput file path.\n" \
"\t-d\tDebug mode.\n" \
"\t-e\tOnly emit C code.\n" \
"\t-i\tInterpret bf code.\n" \
"\t-v\tVerbose output.\n");
exit(1);
}
static int
compile(char *s, char *o)
{
char *cpath = "/usr/bin/gcc";
char *cargs[] = {
cpath, s, "-O3", "-Wall", "-std=c99", "-o", o, NULL
};
return execv(cpath, cargs);
}
int
main(int argc, char **argv)
{
struct parser parse;
struct stat inputstat;
char *tmp = NULL, *output = NULL, *input;
char *code;
int inputfd;
int opt;
while ((opt = getopt(argc, argv, "hidevo:")) != -1) {
switch (opt) {
case 'i':
ONLY(COMPILE)
action = INTERPRET; break;
case 'd':
ONLY(COMPILE)
action = DEBUG; break;
case 'e':
ONLY(COMPILE)
action = EMIT; break;
case 'v':
verbose++; break;
case 'o':
output = optarg; break;
case 'h': default:
help();
}
}
argc -= optind;
argv += optind;
if (argc == 1) {
if (*(*argv) == '-' && *(*argv + 1) == 0) {
/* if - is file then read from stdin */
from_stdin++;
} else {
input = *argv;
}
} else {
fprintf(stderr, "error: missing FILE input\n");
help();
}
if (output == NULL) {
if (action == EMIT) {
output = "a.c";
} else if (action == INTERPRET || action == DEBUG) {
output = "-";
} else {
output = "a.out";
}
}
if (verbose)
printf("info: input file '%s', output file '%s'\n", input, output);
/* consume input file code */
size_t pgsize = getpagesize();
if (!from_stdin) {
if ((inputfd = open(input, O_RDONLY)) == -1 || stat(input, &inputstat) == -1)
{
perror("error");
_abort(NULL, -1);
}
code = mmap(0, inputstat.st_size, PROT_READ, MAP_FILE|MAP_SHARED,
inputfd, 0);
} else {
code = mmap(0, pgsize, PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_SHARED, -1, 0);
read(STDIN_FILENO, code, pgsize);
}
/* child process will compile or emit the C code */
int pipes[2];
pid_t child;
pipe(pipes);
/* setup stdin/stdout for getchar() */
close(pipes[0]);
close(pipes[1]);
dup2(STDOUT_FILENO, pipes[0]);
dup2(STDIN_FILENO, pipes[1]);
child = fork();
if (child == 0) {
dup2(pipes[0], STDIN_FILENO);
dup2(pipes[1], STDOUT_FILENO);
if (action == INTERPRET || action == DEBUG) {
/* interpret BF code */
if (verbose)
printf("info: %s bf code.\n",
(action == INTERPRET ? "interpret" : "debug"));
parse = parser_init(code, NULL, 1, action - INTERPRET);
} else if (action == EMIT) {
/* only emit the C code */
if (verbose)
printf("info: emit C code.\n");
parse = parser_init(code, output, 0, 0);
} else {
/* get temp file for generated C code */
if (verbose)
printf("info: compile code.\n");
tmp = mktemp(tmpdefault);
scat(tmp, ".c");
if (verbose)
printf("info: created temp file '%s'\n", tmp);
parse = parser_init(code, tmp, 0, 0);
}
parser_program(&parse);
parser_fin(&parse);
if (action == COMPILE) {
if (verbose)
printf("info: compiling input file\n");
compile(tmp, output);
}
exit(EXIT_SUCCESS);
}
/* check if child failed and wait for it to be done */
if (child == -1) {
perror("fork");
} else {
int childstat;
if (waitpid(child, &childstat, 0) == -1) {
perror("wait");
}
if (WIFEXITED(childstat) ? WEXITSTATUS(childstat) : EXIT_FAILURE) {
fprintf(stderr, "error: parser had an error\n");
}
}
/* final */
if (verbose)
printf("info: done!\n");
if (tmp != NULL)
unlink(tmp);
close(inputfd);
munmap(code, inputstat.st_size);
return 0;
}