-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommand.c
322 lines (270 loc) · 5.78 KB
/
command.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* arbre
*
* (c) 2011, Alexis Sellier
*
* command.c
*
* CLI commands
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include <errno.h>
#include <sys/stat.h>
char *strdup(const char *);
#include "arbre.h"
#include "scanner.h"
#include "parser.h"
#include "op.h"
#include "runtime.h"
#include "vm.h"
#include "generator.h"
#include "command.h"
#include "error.h"
#include "reduce.h"
#include "limits.h"
static int command_build(Command *cmd);
static int command_run(Command *cmd);
static int command_test(Command *cmd);
static bool command_parseopt(Command *cmd, char opt, char *arg);
static void command_parselopt(Command *cmd, char *arg);
static uint8_t *freadbin(FILE *fp, const char *path);
struct {
CommandOption type;
const char *name;
} CMD_OPTIONS[] = {
{CMDOPT_SYNTAX, "syntax"},
{CMDOPT_AST, "ast"},
{CMDOPT_PRE, "pre"},
{CMDOPT_V, "verbose"},
{0, NULL}
};
struct {
CommandType type;
const char *name;
int (*f)(Command *c);
} CMD_MAP[] = {
{CMD_BUILD, "build", command_build},
{CMD_RUN, "run", command_run},
{CMD_TEST, "test", command_test},
// {CMD_VERSION, "version", command_version},
{0, NULL, NULL}
};
const char *CMD_USAGE =
"usage: arbre <command> [options]\n"
"\n"
"commands:\n"
" build compile modules and dependencies\n"
" clean remove .arb.bin files\n"
" run compile and run\n"
"\n"
"options:\n"
" -v verbose\n"
" --help help\n"
" --version print version and exit\n"
" --ast print the AST\n"
" --pre only run the pre-processor phase\n"
" --syntax only run the syntax checking phase\n";
/*
* Command allocator/initialzer
*/
Command *command(int argv, char *argc[])
{
Command *c = malloc(sizeof(*c));
c->type = 0;
c->options = 0;
c->inputs = malloc(sizeof(char*) * argv);
c->inputc = 0;
c->argv = argv;
c->argc = argc;
c->output = NULL;
c->fp = NULL;
c->f = NULL;
return c;
}
/*
* Command de-allocator
*/
void command_free(Command *c)
{
if (c->fp != NULL)
fclose(c->fp);
free(c->inputs);
free(c);
}
/*
* Execute a command.
*/
int command_exec(Command *cmd)
{
/* No command passed, print usage */
if (cmd->argv == 1) {
puts(CMD_USAGE);
exit(0);
}
/* Retrieve command type */
for (int i = 0; CMD_MAP[i].type; i++) {
if (strcmp(cmd->argc[1], CMD_MAP[i].name) == 0) {
cmd->type = CMD_MAP[i].type;
cmd->f = CMD_MAP[i].f;
/* Parse command options */
for (int i = 2; i < cmd->argv; i++) {
if (cmd->argc[i][0] == '-') {
switch (cmd->argc[i][1]) {
case '-':
command_parselopt(cmd, &cmd->argc[i][2]);
break;
case '\0':
// TODO: Read from STDIN
break;
default: {
char opt = cmd->argc[i][1],
*arg = NULL;
if (i < cmd->argv - 1 && cmd->argc[i + 1][0] != '-') {
arg = cmd->argc[++i];
}
command_parseopt(cmd, opt, arg);
}
}
} else {
cmd->inputs[cmd->inputc++] = cmd->argc[i];
}
}
/* Run command */
return cmd->f(cmd);
}
}
error(1, 0, "unknown command `%s`", cmd->argc[1]);
return -1;
}
/*
* Parse short option. Example:
*
* -a
*/
static bool command_parseopt(Command *cmd, char opt, char *arg)
{
switch (opt) {
case 'o':
cmd->output = arg;
return true;
default:
break;
}
return false;
}
/*
* Parse long option. Example:
*
* --help
*/
static void command_parselopt(Command *cmd, char *arg)
{
for (int i = 0; CMD_OPTIONS[i].type; i++) {
if (strcmp(CMD_OPTIONS[i].name, arg) == 0) {
cmd->options |= CMD_OPTIONS[i].type;
}
}
}
/*
* 'test' command
*/
static int command_test(Command *cmd)
{
return 0;
}
/*
* 'run' command
*/
static int command_run(Command *c)
{
struct tvalue *ret;
if (c->inputc > 1)
error(1, 0, "more than one input file was given");
if (c->inputc == 0)
puts("usage: arbre run <module>"), exit(0);
// TODO: Use unique path
c->output = "/tmp/arbre-build.bin";
command_build(c);
const char *path = c->inputs[0];
char *module = strdup(path);
for (int i = 0; i < strlen(module); i++) {
if (module[i] == '.') {
module[i] = '\0';
break;
}
}
if (c->options & CMDOPT_SYNTAX)
return 0;
uint8_t *code = freadbin(c->fp, module);
VM *v = vm();
vm_open(v, module, code);
ret = vm_run(v, module, "main");
assert(ret->t == TYPE_NUMBER);
return ret->v.number;
}
#ifdef DEBUG
static void ontoken(Token *t)
{
char buff[128];
tokentos(t, buff, true);
puts(buff);
}
#endif
/*
* 'build' command
*/
static int command_build(Command *c)
{
static char out[PATH_MAX];
// TODO: If no files were specified, build all arbre
// files in the current dir.
if (c->inputc == 0)
puts("usage: arbre build <files...>"), exit(0);
for (int i = 0; i < c->inputc; i++) {
char *path = c->inputs[i];
struct source *src = source(path);
Parser *p = parser(src);
#ifdef DEBUG
p->ontoken = ontoken;
#endif
Tree *tree = parse(p);
#ifdef DEBUG
putchar('\n');
#endif
if (c->options & CMDOPT_AST)
pp_tree(tree), putchar('\n');
if (p->errors > 0)
return 1;
reduce(tree);
if (c->options & CMDOPT_SYNTAX)
break;
Generator *g = generator(tree, src);
mkdir(ARBRE_DIR, 0755);
mkdir(ARBRE_BIN_DIR, 0755);
sprintf(out, ARBRE_BIN_FMT, path);
if (! (c->fp = fopen(c->output ? c->output : out, "w+"))) {
error(1, errno, "couldn't create file %s for writing", out);
}
generate(g, c->fp);
}
return 0;
}
/*
* Read an arb.bin file
*/
static uint8_t *freadbin(FILE *fp, const char *module)
{
uint8_t *buffer;
size_t size;
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
rewind(fp);
buffer = malloc(size);
fread(buffer, sizeof(uint8_t), size, fp);
return buffer;
}