chibicc/main.c

61 lines
1.2 KiB
C
Raw Normal View History

2020-10-07 14:11:16 +03:00
#include "chibicc.h"
2019-08-03 09:36:43 +03:00
2020-05-08 14:44:25 +03:00
static char *opt_o;
static char *input_path;
static void usage(int status) {
fprintf(stderr, "chibicc [ -o <path> ] <file>\n");
exit(status);
}
static void parse_args(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--help"))
usage(0);
if (!strcmp(argv[i], "-o")) {
if (!argv[++i])
usage(1);
opt_o = argv[i];
continue;
}
if (!strncmp(argv[i], "-o", 2)) {
opt_o = argv[i] + 2;
continue;
}
if (argv[i][0] == '-' && argv[i][1] != '\0')
error("unknown argument: %s", argv[i]);
input_path = argv[i];
}
if (!input_path)
error("no input files");
}
static FILE *open_file(char *path) {
if (!path || strcmp(path, "-") == 0)
return stdout;
FILE *out = fopen(path, "w");
if (!out)
error("cannot open output file: %s: %s", path, strerror(errno));
return out;
}
2019-08-03 09:36:43 +03:00
int main(int argc, char **argv) {
2020-05-08 14:44:25 +03:00
parse_args(argc, argv);
2019-08-03 09:36:43 +03:00
// Tokenize and parse.
2020-05-08 14:44:25 +03:00
Token *tok = tokenize_file(input_path);
Obj *prog = parse(tok);
2020-10-07 14:12:19 +03:00
// Traverse the AST to emit assembly.
2020-05-08 14:44:25 +03:00
FILE *out = open_file(opt_o);
codegen(prog, out);
return 0;
}