main.c (1369B)
1 #define DLIB_IMPLEMENTATION 2 #include "dlib.h" 3 4 #include "lexer.h" 5 #include "parser.h" 6 7 Ast_node * 8 malloc_ast() 9 { 10 void *result = malloc(sizeof(Ast_node)); 11 if (!result) { 12 fprintf(stderr, "malloc_ast(): Out of memory\n"); 13 abort(); 14 } 15 return result; 16 } 17 18 void 19 free_ast(Ast_node *node) 20 { 21 free(node); 22 } 23 24 void 25 usage(FILE *out, const char *program_name) 26 { 27 fprintf(out, "Usage: %s <input>\n", program_name); 28 } 29 30 int 31 main(int argc, char **argv) 32 { 33 assert(argc > 0); 34 char *program_name = pop_arg(argc, argv); 35 if (!argc) { 36 fprintf(stderr, "Error: Missing input file\n"); 37 usage(stderr, program_name); 38 return 1; 39 } 40 Sb input_source = {0}; 41 char *input_file_name = pop_arg(argc, argv); 42 if (!sb_read_file(&input_source, input_file_name)) { 43 return 1; 44 } 45 Lexer lexer = { 46 .source = sv_from_sb(input_source), 47 .current_loc = { 48 .filename = sv_from_cstr(input_file_name), 49 .line = 1, 50 .column = 1 51 } 52 }; 53 Parser parser = { 54 .lexer = lexer, 55 .ast_alloc = malloc_ast, 56 .ast_free = free_ast, 57 }; 58 Ast_node_list ast = parse(&parser); 59 for (int i = 0; i < ast.count; ++i) { 60 debug_print_ast_node(ast.items[i], 0); 61 } 62 /* 63 Token t; 64 while ((t = next_token(&lexer)).kind != T_EOF && t.kind != T_ERROR) { 65 if (t.kind != T_WHITESPACE) debug_print_token(t); 66 } 67 debug_print_token(t); 68 */ 69 return 0; 70 }