-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
193 lines (163 loc) · 4.34 KB
/
main.c
File metadata and controls
193 lines (163 loc) · 4.34 KB
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
/*
* cbc: a cursed bean(code) compiler
*
* Copyright (c) Eason Qin <eason@ezntek.com>, 2026.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#define _POSIX_C_SOURCE 200809L
#define _GNU_SOURCE
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <threads.h>
#include "a_string.h"
#include "a_vector.h"
#include "ast.h"
#include "ast_printer.h"
#include "common.h"
#include "compiler/compiler.h"
#include "lexer.h"
#include "parser/parser.h"
#define _UTIL_H_IMPLEMENTATION
#include "util.h"
typedef struct {
a_string in_path;
a_string out_path;
bool has_in_path;
bool has_out_path;
bool debug;
bool no_compile;
bool help;
} Args;
static const struct option LONG_OPTS[] = {
{"out-path", required_argument, 0, 'o'},
{"debug", required_argument, 0, 'd'},
{"no-compile", required_argument, 0, 'N'},
{"help", required_argument, 0, 'h'},
{0},
};
void help(void);
bool parse_args(int argc, char** argv);
void init(int argc, char** argv);
void compile(void);
void deinit(void);
static Args args;
void help(void) {
puts(" --out-path, -o: specify output path (default: out.qbe)");
puts(" --debug, -d: print extra debugging info");
puts(" --no-compile, -N: don't actually compile anything");
}
bool parse_args(int argc, char** argv) {
args = (Args){0};
int c = 0;
while ((c = getopt_long(argc, argv, "o:dhN", LONG_OPTS, NULL)) != -1) {
switch (c) {
case 'o': {
args.out_path = astr(optarg);
args.has_out_path = true;
} break;
case 'd': {
args.debug = true;
} break;
case 'N': {
args.no_compile = true;
} break;
case 'h': {
args.help = true;
} break;
case '?': {
help();
return false;
} break;
}
}
if (optind < argc) {
args.in_path = astr(argv[optind]);
if (args.in_path.len == 0)
fatal("no input file provided");
args.has_in_path = true;
}
return true;
}
void init(int argc, char** argv) {
args = (Args){0};
parse_args(argc, argv);
}
static a_string file_content;
static a_string file_name;
static Lexer l;
static Tokens toks;
static Parser ps;
static CB_Program prog;
static AstPrinter printer;
static Compiler comp;
void compile(void) {
if (!args.has_in_path) {
file_name = astr("(stdin)");
file_content = as_new();
if (!as_read_line(&file_content, stdin))
panic("could not read line from stdin");
} else {
file_name = astr(args.in_path.data);
file_content = as_read_file(args.in_path.data);
if (errno == ENOENT)
panic("file \"%s\" not found", args.in_path.data);
}
l = lx_new(file_content.data, file_content.len);
toks = (Tokens){0};
if (!lx_tokenize(&l, &toks))
return;
if (args.debug) {
eprintf("\x1b[2m=== TOKENS ===\n");
for (usize i = 0; i < toks.len; i++) {
token_print_long(&toks.data[i]);
}
eprintf("==============\x1b[0m\n");
}
ps = ps_new(toks.data, toks.len, as_dupe(&file_name));
if (!ps_program(&ps, &prog)) {
eprintf("error\n");
return;
}
if (args.debug) {
printer = ap_new_with_stderr_writer();
ap_visit_program(&printer, &prog);
putchar('\n');
}
if (args.has_out_path) {
if (!cm_new_with_file_writer(args.out_path.data, &comp))
panic("could not open %s", args.out_path.data);
} else {
comp = cm_new();
}
if (cm_program(&comp, &prog, &file_name))
return;
if (args.has_in_path)
eprintf("Compiled %.*s\n", as_fmt(args.in_path));
return;
}
void deinit(void) {
cm_free(&comp);
cb_program_free(&prog);
ps_free(&ps);
for (usize i = 0; i < toks.len; i++) {
token_free(&toks.data[i]);
}
av_free(&toks);
lx_free(&l);
as_free(&file_content);
as_free(&file_name);
}
i32 main(i32 argc, char** argv) {
init(argc, argv);
if (args.help) {
help();
} else {
compile();
}
deinit();
return 0;
}