#include <string.h>
#include <signal.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
//#include "JokerTao.h"
#include <unordered_map>
//
#include "algo.h"
#include "lexer.h"

using namespace std;

char* f;

[[ noreturn ]] void end(int ignore = 0){
	#ifdef DEBUG
		printf(NORMAL);
	#endif

	#ifdef DEBUG
		printf(GREEN "\" Standard variables\n" NORMAL);
		const char *const *c = algo::standard_vars-1;
		while((c++, *c != NULL)){
			printf("%s: %s\n", *c, algo::vars[*c].c_str());
			algo::vars.erase(*c);
		}

		printf(GREEN "\" User variables\n" NORMAL);
		vector<char*> vpbuf;
		for(auto &&i : algo::vars){
			char *a;
			asprintf(&a, "%s: %s\n", i.first.c_str(), i.second.c_str());
			vpbuf.push_back(a);
		}
		std::sort(vpbuf.begin(), vpbuf.end(), [](char* a, char* b){ return (strcmp(a, b) < 1); });
		for(auto &&i : vpbuf){
			cout << i;
		}
	#endif

	exit(2);
}

bool init(){
	ios::sync_with_stdio(true);
	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);
	signal(SIGINT, end);

	printf("[init] ");
	algo::init();
	puts("");

	return true;
}

signed main(int argc, char* argv[]){
	if(argc < 2){ return 0; }

	init();

	f = argv[1];
	lex_file(f);

	end();
}
