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
|
/*
* # 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, Mel G. <mel@rnrd.eu>
*
* SPDX-License-Identifier: MPL-2.0
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "catboot.h"
const ascii*
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 file_data;
}
int32
main(const int32 argc, const ascii* argv[])
{
if (argc != 2) {
printf("usage: catboot <filename>\n");
return EXIT_FAILURE;
}
const ascii* file_path = argv[1];
const ascii* source_buffer = read_file(file_path);
struct String source = string_from_static_c_string(source_buffer);
struct Lexer lexer;
lexer_new(&lexer, source);
struct Token token;
printf("tokens: ");
do {
token = lexer_next(&lexer);
printf("%s ", token_kind_to_string(token.kind));
} while (token.kind != TOKEN_END_OF_FILE);
printf("\n");
// reset lexer
lexer_new(&lexer, source);
struct Parser parser;
parser_new(&parser, &lexer);
struct Parser_Error parser_error = parser_error_none();
struct Tree tree = parser_do_your_thing(&parser, &parser_error);
if (!parser_error_is_none(&parser_error)) {
printf("parser error: %s\n", parser_error_to_string(&parser_error));
return EXIT_FAILURE;
}
tree_printer(&tree);
return EXIT_SUCCESS;
}
|