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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/*
* # catboot
*
* the bootstrap compiler for catskill,
* implemented as simply as possible,
* depending only on the C standard library,
* in this case musl, and built statically
* in a single union build.
*
* should only be used to compile `catskill` itself,
* as it is not a full-featured compiler, and never will be.
* once `catskill` can compile itself using a C backend,
* this compiler version will be permanently retired.
* (although that's still very far away!! have fun! :3)
*
* Copyright (c) 2025-2026, Mel G. <mel@rnrd.eu>
*
* SPDX-License-Identifier: MPL-2.0
*/
#define _POSIX_C_SOURCE 200809L
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "catboot.h"
#define VERSION "0.0.0"
struct String
read_file(const ascii* path)
{
struct stat stat_info;
if (stat(path, &stat_info) == -1) failure("i couldn't open that file, sorry :(");
check(stat_info.st_size > 0, "file is empty, i can't map an empty file");
const int32 file_descriptor = open(path, O_RDONLY);
check(file_descriptor != -1, "i couldn't open that file, sorry :(");
const flags mmap_prot = PROT_READ;
const flags mmap_flags = MAP_PRIVATE;
const unknown* file_data =
mmap(nil, stat_info.st_size, mmap_prot, mmap_flags, file_descriptor, 0);
return string_from_static_c_string(file_data);
}
void
usage(void)
{
fprintf(
stderr,
"usage:\n"
" catboot <filename>\n"
" catboot --test-lex <filename>\n"
" catboot --test-parse <filename>\n"
"\n"
"options:\n"
" --test-lex <file>\t output token stream for file\n"
" --test-parse <file>\t output abstract syntax tree for file in s-expr format\n"
" --test-transpile <file>\t output transpiled c language source from file\n"
" -?, --help\t\t\t print this help message\n"
" -v, --version\t\t print current version\n");
}
void
version(void)
{
fprintf(
stderr,
"catboot (catskill), version " VERSION "\n"
"\n"
"This program's source code is subject to the terms of the Mozilla Public\n"
"License, v. 2.0. If a copy of the MPL was not distributed with this\n"
"file, You can obtain one at https://mozilla.org/MPL/2.0/.\n"
"\n"
"Copyright (c) 2025, Mel G. <mel@rnrd.eu>\n");
}
integer
debug_lex_pass(struct String source)
{
struct Lexer lexer;
lexer_new(&lexer, source);
struct Token token;
do {
token = lexer_next(&lexer);
token_debug_print(&token);
printf(" ");
} while (token.kind != TOKEN_END_OF_FILE);
return 0;
}
integer
debug_parse_pass(struct Source_File source_file)
{
struct Lexer lexer;
lexer_new(&lexer, source_file.source);
struct Parser parser;
parser_new(&parser, &lexer, source_file);
struct Tree tree;
int parser_result = parser_do_your_thing(&parser, &tree);
if (parser_result != 0) {
log_error("parser finished with errors\n");
return parser_result;
}
tree_printer(&tree);
return parser_result;
}
integer
debug_transpile_pass(struct Source_File source_file)
{
struct Lexer lexer;
lexer_new(&lexer, source_file.source);
struct Parser parser;
parser_new(&parser, &lexer, source_file);
struct Tree tree;
int parser_result = parser_do_your_thing(&parser, &tree);
if (parser_result != 0) {
log_error("parser finished with errors\n");
return parser_result;
}
struct Transpiler transpiler;
transpiler_new(&transpiler, stdout);
return transpiler_catskill_to_c(&transpiler, &tree);
}
enum Command_Result
{
COMMAND_OK,
COMMAND_FAIL,
};
struct Command_Arguments
{
const ascii* input;
const ascii* output;
};
enum Command_Result
version_command(struct Command_Arguments* arguments)
{
version();
return COMMAND_OK;
}
enum Command_Result
help_command(struct Command_Arguments* arguments)
{
usage();
return COMMAND_OK;
}
enum Command_Result
test_lex_command(struct Command_Arguments* arguments)
{
struct String source = read_file(arguments->input);
// TODO: lexer errors
debug_lex_pass(source);
return COMMAND_OK;
}
enum Command_Result
test_parse_command(struct Command_Arguments* arguments)
{
struct String source = read_file(arguments->input);
struct Source_File source_file = {
.source = source,
.path = string_from_static_c_string(arguments->input),
};
if (debug_parse_pass(source_file)) return COMMAND_FAIL;
return COMMAND_OK;
}
enum Command_Result
test_transpile_command(struct Command_Arguments* arguments)
{
struct String source = read_file(arguments->input);
struct Source_File source_file = {
.source = source,
.path = string_from_static_c_string(arguments->input),
};
if (debug_transpile_pass(source_file)) return COMMAND_FAIL;
return COMMAND_OK;
}
enum Command_Result
default_command(struct Command_Arguments* arguments)
{
enum Command_Result result = COMMAND_OK;
struct String source = read_file(arguments->input);
struct Source_File source_file = {
.source = source,
.path = string_from_static_c_string(arguments->input),
};
struct Lexer lexer;
lexer_new(&lexer, source);
struct Parser parser;
parser_new(&parser, &lexer, source_file);
struct Tree tree;
if (parser_do_your_thing(&parser, &tree) != 0) {
log_error("parser finished with errors\n");
result = COMMAND_FAIL;
goto end;
}
ascii build_directory_template[] = "/tmp/catboot-build-XXXXXX"; // TODO: /tmp/catboot/build-XXXXXX
ascii* build_directory_path = mkdtemp(build_directory_template);
struct String build_path = string_from_c_string(build_directory_path);
struct String source_path = string_append_c_str(build_path, "/input.c");
FILE* output_file = fopen(string_c_str(source_path), "w");
if (!output_file) {
log_error("could not open temporary output file: %s\n", string_c_str(source_path));
result = COMMAND_FAIL;
goto end;
}
struct Transpiler transpiler;
transpiler_new(&transpiler, output_file);
if (transpiler_catskill_to_c(&transpiler, &tree) != 0) {
log_error("transpiler finished with errors\n");
fclose(output_file);
result = COMMAND_FAIL;
goto end;
}
fclose(output_file);
struct Build_Result build_result = build_executable(build_path);
if (!build_result.success) {
log_error("backend failure with exit code %ld\n", build_result.compiler_exit_code);
log_error("compiler output:\n%.*s\n", (int)build_result.compiler_log.length, build_result.compiler_log.data);
result = COMMAND_FAIL;
goto end;
}
// copy the output executable to the desired location
const ascii* output_filename = arguments->output ? arguments->output : "a.out";
// simple copy command for now
ascii copy_command[2048];
snprintf(copy_command, sizeof(copy_command), "cp %s %s", string_c_str(build_result.output_path), output_filename);
if (system(copy_command) != 0) {
log_error("failed to copy executable to %s\n", output_filename);
result = COMMAND_FAIL;
}
end:
return result;
}
typedef enum Command_Result (*Command_Function)(struct Command_Arguments*);
struct Command_Definition
{
bool is_default;
const ascii* name;
const ascii short_name;
Command_Function function;
};
struct Command_Definition command_definitions[] = {
{ .name = "test-lex", .function = test_lex_command },
{ .name = "test-parse", .function = test_parse_command },
{ .name = "test-transpile", .function = test_transpile_command },
{ .name = "version", .short_name = 'v', .function = version_command },
{ .name = "help", .short_name = '?', .function = help_command },
{ .is_default = true, .function = default_command },
};
int32
main(const int32 argc, ascii* argv[])
{
if (argc < 2) {
usage();
return EXIT_FAILURE;
}
bool have_command = false;
ascii *command_name = nil, short_command_name = '\0', *input = nil, *output = nil;
for (uint a = 1; a < argc; ++a) {
ascii* arg = argv[a];
if (arg[0] == '-') {
check(!have_command, "multiple commands given");
if (arg[1] == '-') {
check(!command_name, "multiple full-name commands given");
command_name = &arg[2];
} else {
check(!short_command_name, "multiple short-name commands given");
short_command_name = arg[1];
}
have_command = true;
} else {
if (!input)
input = arg;
else if (!output)
output = arg;
else
failure("too many arguments, expected at most 2: input and output");
}
}
struct Command_Arguments arguments = { .input = input, .output = output };
for (uint d = 0; d < ARRAY_SIZE(command_definitions); ++d) {
struct Command_Definition* definition = &command_definitions[d];
if (definition->is_default || (command_name && strcmp(definition->name, command_name) == 0)
|| (short_command_name && definition->short_name == short_command_name)) {
switch (definition->function(&arguments)) {
case COMMAND_OK:
return EXIT_SUCCESS;
case COMMAND_FAIL:
return EXIT_FAILURE;
}
}
}
}
|