/* MasC interpreter/compiler
   (Master Assembly Script Container)
   written by Jared Bruni (LmaSterD)
   
   After reading Writing Compilers and Interpreters A approach using C++ by Ronald Mak
   and the Dragon Book
   code completly re-written but using concepts teached in this book

   making alL my nowhere planS for------->noboDy
  
  
   steps this program goes through:
   The lexical anyalizer breaks it up and then feeds it to the syntax anaylizer
   then once the syntax anaylizer makes sure its correct it feeds it to the parser
   then the parser feeds the code to the back end intermediate code generator 
   once the code is intermediate form its converted to a form that can be interpeted
   then is executed, or translated to a different language

   in her eyes I C nothing
   I C:   what I Look for.
   got to be good Lookin cause hes so hard to C
   a little dose of LostSideDead:
*/

/*
	1 +  Why software should be free
	     its simply different states your computer can be in:

	     the computer wasnt free
	     there is no need
	     who is really the leach

	1 +  but what you got means so much to me:
	
	1    float up stream
	     i dont mind I think there crazy
	     taking my time
	     making each day of the year
	     nobody can deny that theres something there
	     Love is something to share
		 come together 

    = "Who put all those things in your head"
*/
// gooD day Sunshine i Love 
// spinning: taking pre-exisitng values and putting your own 'spin' on it
// try to see it my way , do I have to keep talking till I cant go on


// main Driver 
// baby you can drive my car

#include<stdio.h>
#include "textin.h"
#include "parser.h"
#include "debug_info.h"
#include "output_c.h"



// globals for this scope
int error_count = 0;
int line_count = 0;
int output_ = 1;
int debug_it = 0;
char outfile[256];
int verbose = 0;
struct text_in source_text;
struct BackEnd back_end;

// free all structures allocated
void freeAll()
{
     freeText_in(&source_text);
}

// Pause the system (Windows Only)
void sys_pause()
{
// only pause if were compiling under windows
#ifdef _WIN32
	system("PAUSE"); 
#endif 
	
}

// Print usage
void printUsage()
{

	printf("MasC written by Jared Bruni of LostSideDead\n");
	printf("Usage: masc <sourcefile> [-o,-c] <outfile> [-flags] or masc <icode> [-flags]\n");
}

// main function where the code begins
int main(int argc, char *argv[])
{
	struct convertHolder hold;
	struct Token *ptr_tok = 0;
    verbose = 0;
	debug_it = 0;
	output_ = 0;

	if(argc > 3)
	{
		
		if(strcmp(argv[2], "-o") == 0)
			output_ = 1;
		else if(strcmp(argv[2], "-c") == 0)
			output_ = 2;
		else
			printUsage();

		if(argc == 5) 
		{
			if(strcmp(argv[4], "-v") == 0)
				verbose = 1;
		}

		strcpy(outfile,argv[3]);

		initText_in(argv[1], &source_text);
		parse(&source_text,ptr_tok);
		freeText_in(&source_text);

		if(err <= 1)
		// switch so more translaters can be applied
		switch(output_) {
		case 2: {
			hold = convertIcode(outfile,ptr_tok,&back_end);
			if(hold.rt_val != 0) {
			translateMasc_C(hold.bend, hold.tokens);
			printf("(%d) source lines (%d) syntax errors\n translated to C compile with gcc\n c file %s\n",line_count,error_count,outfile);
			freeIcode(hold.tokens,hold.bend);
			}
			else {
				printf("Errors couldnt continue.\n");
			}
			sys_pause();
			return 0;
			break;
		}
		case 1: {
			if(err == 0)
			printf("(%d) source lines (%d) syntax errors\n intermediate code outputed to: %s\n", line_count, error_count, outfile);
			else
			printf(" (%d) syntax errors \n", err);
			free(ptr_tok);
			sys_pause();
			return 0;
		}

		}
		else
		printf("Errors in source maybe you forgot the newline at the end ;]\n");
		sys_pause();
	}
	else if(argc > 1)
	{
			
		if(argc > 2 && strcmp(argv[2], "-d") == 0)
		{
			debug_it = 1;
		}

		hold = convertIcode(argv[1],ptr_tok,&back_end);
		if(debug_it == 1) {
		printDebugInfo("debug_before.html",&back_end,hold.tokens);
		printf("Symbol Table\n");
		symNode_Print(&back_end.g_stable.root);
		printf("Token codes\n");
		printTokens(hold.tokens, back_end.inc_len);
		printf("\n executing script..\n");
		}
		if(hold.rt_val != 0) 
		executeIcode(hold.tokens,&back_end);
		if(debug_it == 1) {
			printDebugInfo("debug_after.html",&back_end,hold.tokens);
		}
		freeIcode(ptr_tok,&back_end);
		if(debug_it == 1)
			printf("\ndebug information before execution outputed to: debug_before.html\n after exeuction outputed to: debug_after.html\n");
		sys_pause();

	}
	else
	{
		printUsage();
		sys_pause();
	}
	
    return 0;   
}
// you cant see Me
// when your prized possesions start to weigh you down
// look in my direction ill be round
// no sign of love behind the tears
// spinal cracker
// take a drink from this special cup
// well well your feeling fine
// :)
// Shine on you crazy Diamond *
// dont know why im riddin so high?

