/*
===================================
Quick Basic Compatible Compiler
Pre-Proccessor

Original Concept:  Martyn 'leroy' McDonell
Coding:			   Jason Boettcher


====================================
*/

#include <stdio.h>
#include <malloc.h>
#include "bpp.h"
#include "scanner.h"
#include "error.h"
#include "process.h"

// Tokens
token_t *curToken;					// Current Token
token_t *LookToken;					// Look ahead Token

int Indent = 0;						// Indentation

// Input & Output files
FILE *outfile;
FILE *infile;
char basfile[64];
char convfile[64];

///////////////////
// Main Entry Point
int main(int argc, char *argv[])
{
	int error = 0;	
	
	
	ShowQBPPHeader();								// Show program header
		
	error = CheckParameters(argc,argv);		// Check the parameters
	if(error == FALSE)
		return -1;

	// Open the files
	if( (outfile = fopen(convfile,"wt")) == NULL)
		SysError("main", "Cannot open output file");
	if( (infile = fopen(basfile,"rt")) == NULL)
		SysError("main", "Cannot open input file");
	yyin = infile;
	
	error = Initialize();					// Set up the list
	if(error == FALSE)
		SysError("main", "Out of Memory");

	
#ifdef _DEV_
	printf("- Main Proccessor\n");
#endif

	ProcessFile(infile);


	ShutDown();		

	return 0;
}

///////////////////
// Show Header
void ShowQBPPHeader(void)
{
	printf("QBCC - Pre-Proccessor - Version " VERSION "\n");
   printf("------------------------------------\n");
#ifdef _DEV_
	printf("\tDEVELOPER MODE!\n\n");
#endif
}

///////////////////
// Initialize main list
int Initialize(void)
{
#ifdef _DEV_
	printf("- Initializing\n");
#endif

	// Tokens
	curToken = (token_t *)malloc(sizeof(token_t));
	LookToken = (token_t *)malloc(sizeof(token_t));
	if(curToken == NULL)
		SysError("Initialize","Out of memory");
	if(LookToken == NULL)
		SysError("Initialize","Out of memory");
	

	curToken->type = 0;
	curToken->flags = 0;
	strcpy(curToken->text,"");	
	curToken->VarType = VAR_DEFAULT;
	LookToken->type = 0;
	LookToken->flags = 0;
	strcpy(LookToken->text,"");
	LookToken->VarType = VAR_DEFAULT;
	

	return TRUE;
}

///////////////////
// Shutdown the translator
int ShutDown(void)
{
#ifdef _DEV_
	printf("- ShutDown\n");
#endif

	// Close the files
	if(outfile)
		fclose(outfile);
	if(infile)
		fclose(infile);

	if(curToken)
		free(curToken);
	if(LookToken)
		free(LookToken);

	return TRUE;
}

///////////////////
// Check the parameters
int CheckParameters(int argc, char *argv[])
{
	if(argc < 3)	
	{
		// Tell em how to get help
		printf("Incorrect number of arguements!\nUse bpp.exe --help for help\n");
		return FALSE;
	}

	if(strcmp(argv[1],"--help") == 0)
	{
		// Show help
		printf("Usage:\n");
		printf("bpp.exe basfile.bas outbfile.bas\n");
		return FALSE;
	}	
	
	strcpy(basfile, argv[1]);
	strcpy(convfile,argv[2]);
	return TRUE;
}


