/* * # 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 #include #include #include #include #include "common.c" #include "lex.c" #include "tree.c" #include "parse.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 \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_print(&tree); return EXIT_SUCCESS; }