#include <stdio.h>
#include <stdlib.h>

#define IMP
#include "./parser.h"
#include "./lexer.h"
#include "./da.h"
#include "./str.h"
#include "./file.h"

#include "./int.h"

/*
 * TODO:
 * - Parse main function
 * - Run parsed code
 * - Parse generic function
 * - Handle variables
 * - Get source from argv
 * - Help flag
 * */

int
main(int argc, const char *argv[])
{
	struct str str;

	/* const char *src_path = "./main.c"; */
	/* const char *src_path = "./src/cint.c"; */
	const char *src_path = "./t.c";
	enum file_err ferr;
	char *src = NULL;
	size_t src_size = 0;

	struct lexer l = {0};
	struct parser p = {0};

	struct parser_err perr = {0};

	src = (char *) file_read_all(src_path, &src_size, &ferr);
	if ( ferr != FILE_ERR_OK ) {
		fprintf(stderr, "Failed to open src file `%s`: `%s`",
			src_path, file_err_to_cstr(ferr));
		exit(EXIT_FAILURE);
	}

	str = str_from_cstr(src, src_size);

	l = lexer_create(str);
	/*
	{
	struct token t = {0};

	struct token ts[2048] = {0};
	size_t ts_size = 0;

	size_t i = 0;
	t = lexer_next_token(&l, NULL);
	for ( ; t.typ.code != TT_EOF; t = lexer_next_token(&l, NULL) ) {
		ts[ts_size++] = t;
	}

	for ( i = 0; i < ts_size; ++i ) {
		printf("%ld, %d - %s -> `%.*s`\n", i,
		       ts[i].typ.code, ts[i].typ.name,
		       (int) ts[i].lit.size, ts[i].lit.data);
	}
	}
	*/

	p = parser_create(l, &perr);
	if ( perr.code != PARSER_ERR_OK ) {
		fprintf(stderr, "Failed to create parser: `%s`\n", perr.name);
	}
	parser_parse_program(&p, &perr);
	if ( perr.code != PARSER_ERR_OK ) {
		fprintf(stderr, "Failed to parse program: `%s`\n", perr.name);
	}

	free(src);

	(void) argc; (void) argv;
	return 0;
}

