about summary refs log tree commit diff
path: root/boot/catboot.c
blob: fec47c0d618d2d4240ae73f4dd5374df94d15125 (plain)
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
/*
 * # 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)
 */

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include "common.c"
#include "lex.c"
#include "tree.c"

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 :(");

    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;
    do {
        token = lexer_next(&lexer);
        printf("%s ", token_kind_to_string(token.kind));
    } while (token.kind != TOKEN_END_OF_FILE);

    return EXIT_SUCCESS;
}