aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 66142bdbec01527ed57e13599b7c2fa00aac01b1 (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
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
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>

#include "badlib.h"
#include "lexer.c"
#include "parser.c"

typedef enum ExecMode {
    RUN_NORMAL,
    PRINT_LEX,
    PRINT_PARSE,
    PRINT_SEMANTIC,
    PRINT_SYMTABLES,
} ExecMode;

static ExecMode mode = RUN_NORMAL;

void
init(void) {
    log_init_default();
}

void
process_file(Str path) {
    Arena lexer_arena = arena_create(LEXER_MEM, os_allocator);

    FileContents file = platform_read_file(path, &lexer_arena);
    if (file.err) {
        eprintln("%s: error: %s", path, cstr("WOT"));
        exit(EXIT_FAILURE);
    }
    sz errors = 0;

    // Lexer.
    Scanner scanner = {.str = file.data};
    Token *tokens = NULL;
    Token tok = {0};
    while (tok.kind != TOK_EOF) {
        tok = scan_token(&scanner);
        if (tok.kind == TOK_UNKNOWN) {
            eprintln("%s:%d:%d:%s %s", path, tok.line, tok.col,
                     token_str[tok.kind], tok.val);
            errors++;
            continue;
        }
        array_push(tokens, tok, &lexer_arena);
    }
    // Only proceed if there are no errors.
    if (errors) {
        exit(EXIT_FAILURE);
    }
    if (mode == PRINT_LEX) {
        print_tokens(path, tokens);
        goto stop;
    }

    // Parser.
    Parser parser = {
        .tokens = tokens,
        .storage = &lexer_arena,
        .file_name = path,
    };
    array_init(parser.nodes, 256, parser.storage);
    parse_advance(&parser);
    while (parser.current.kind != TOK_EOF) {
#if DEBUG == 1
        static sz ctr = 0;
        println("parsing root: %d", ctr++);
#endif
        parse_expr(&parser, PREC_LOW);
        if (parser.panic) {
            break;
        }
    }
    if (parser.err) {
        exit(EXIT_FAILURE);
    }
    if (mode == PRINT_PARSE) {
        graph_ast(parser.nodes);
        goto stop;
    }

    sz n_roots = array_size(parser.nodes);
    for (sz i = 0; i < n_roots; i++) {
        // The parser stores the root nodes as a stack.
        Node *root = parser.nodes[i];
        (void)root;
    }
    parse_consume(&parser, TOK_EOF, cstr("expected end of file"));

stop:
    // Free up resources.
    arena_destroy(&lexer_arena, os_allocator);
}

#ifndef BIN_NAME
#define BIN_NAME "bdl"
#endif

void
print_usage(void) {
    printf("Usage: %s [options] <filename filename ...>\n", BIN_NAME);
    printf("\n");
    printf("\t-h \t\tShow usage.\n");
    printf(
        "\t-p [l|p|s|t]\tPrint mode for [l]exing, [p]arsing, "
        "[s]emantic "
        "analysis, symbol [t]ables\n");
    printf("\n");
}

int
main(int argc, char *argv[]) {
    int option;
    while ((option = getopt(argc, argv, "hp:")) != -1) {
        switch (option) {
            case 'h': {
                print_usage();
                goto exit_success;
            } break;
            case 'p': {
                if (optarg[0] == 'l' && optarg[1] == '\0') {
                    mode = PRINT_LEX;
                } else if (optarg[0] == 'p' && optarg[1] == '\0') {
                    mode = PRINT_PARSE;
                } else if (optarg[0] == 's' && optarg[1] == '\0') {
                    mode = PRINT_SEMANTIC;
                } else if (optarg[0] == 't' && optarg[1] == '\0') {
                    mode = PRINT_SYMTABLES;
                } else {
                    print_usage();
                    return EXIT_FAILURE;
                }
            } break;
            default: {
                print_usage();
                return EXIT_FAILURE;
            } break;
        }
    }

    init();

    // Run from stdin.
    if (optind == argc) {
        // TODO: REPL
        // repl();
        goto exit_success;
    }

    // Run from file.
    while (optind < argc) {
        char *file_name = argv[optind];
        Str file_path = STR(file_name);
        process_file(file_path);
        optind++;
    }

exit_success:
    return EXIT_SUCCESS;
}