summaryrefslogtreecommitdiff
path: root/main.c
blob: 5c26dd321dc965a86128b539fee9c9462f88da97 (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
#include "lexer.h"
#include "parser.h"
#include "codegen.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void test_lexer(int argc, char** argv) {
    struct token token;
    for (int i = 1; i < argc; i++) {
        lexer_load(argv[i]);
        while (lexer_pop(&token)) {
            printf("[%s]: ", argv[i]);
            switch (token.type) {
                case TK_IDENT:
                    printf("got identifier: %s\n", token.data.ident);
                    free(token.data.ident);
                    break;
                case TK_STR_LIT:
                    printf("got string: %s\n", token.data.str_lit);
                    free(token.data.str_lit);
                    break;
                case TK_INT_LIT:
                    printf("got int: %lld\n", token.data.int_lit);
                    break;
                case TK_FLOAT_LIT:
                    printf("got float: %lf\n", token.data.float_lit);
                    break;
                case TK_CHAR_LIT:
                    printf("got char: %c\n", token.data.char_lit);
                    break;
                default:
                    printf("got simple token: %d\n", token.type);
            }
        }
        lexer_close();
    }
}

void test_parser(int argc, char** argv) {
    for (int i = 1; i < argc; i++) {
        struct root_node* root = parse(argv[i]);
        unsigned int fn_sz = strlen(argv[i]);
        char outfile[fn_sz + 1];
        strcpy(outfile, argv[i]);
        outfile[fn_sz - 1] = 's';
        outfile[fn_sz] = 0;

        emit_code(root, outfile);
        ast_destroy(root);
    }
}

int main(int argc, char** argv) {
    if (argc < 2) {
        fprintf(stderr, "ccc: no input files\n");
        return 1;
    }

    test_parser(argc, argv);
}