#include "lexer.h"
#include "parser.h"
#include "codegen.h"
#include <stdlib.h>
#include <stdio.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) {
    struct root_node* root;
    struct root_node** p_cur = &root;
    for (int i = 1; i < argc; i++) {
        *p_cur = parse(argv[i]);
        p_cur = &((*p_cur)->next);
    }

    emit_code(root, "test/simple.s");
    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);
}
