/*



OLD VERSION


	ToDo:
		Compiler:
			Parse the statements now
				//-add in (var)'s before strings
				//-add in a double check undefined var return for functions;
				//-add in var return type for functions
				//-add in arguements type for functions
				//-add function and variable declaration into a header
				//-add in the initializer function
				//-write the statement and header arrays into files.
				//-add in the "universal header" includes for each file. 
				
				-add to a seperate file, the universal initializer

		Wrapper:
			add in a global static variable called "undefined"
		

	Current Restrictions:
		-strings must be made using double quotes.
			?-add in support for quotes using '' instead of ""
		-no classes or prototype types.
		-no function pointers
*/

var UniversalHeader = "";

function ep_compile_game()
{
  var array = GetFileList("scripts");
  for (i=0;i<array.length;i++)
  {
			UniversalHeader+="#include \"ep_"+array[i].substring(0, array[i].indexOf('.'))+".h\"\n";
  }
  
  UniversalHeader+="\n\n";
  
  for (i=0;i<array.length;i++)
  {
//		if (array[i] != "ep_compile.js")
//		{

			GetSystemFont().drawText(0,0, array[i]);
			FlipScreen();
			GetKey();

			
			ep_compile_script(array[i]);
//		}
  }
  
	WriteUniversalInitializer(array);
}

var function_boo;

var header_list = new Array();
var header_count = 0;


function ep_compile_script(name)
{
	var font = GetSystemFont();

	var file_temp = OpenFile("../other/"+name);
	file_temp.write("a","b");
	file_temp.flush();
	file_temp.close();

	//remove comments
	ep_compile_remove_comments("../scripts/",name); 

	var file = OpenRawFile("COMPILE"+name,false);
	var file2 = OpenRawFile("2COMPILE"+name,true);


	var string_list = new Array();

	var statement_list = new Array();
	
	var line = 0;
	var file_length = file.getSize();
	var position = 0;
	var temp_position = 0;
	var string = "";
	var string2 = "";
	var character = "";
	var flag = false;

	var type = 0;
	var statement_counter = 0;
	var lbcount = 0;
	var rbcount = 0;
	
	var x = 0;
	var i = 0;

	header_count = 0;
	
	//setup "black-list" positions

	var mode = -1;
	
	while(position < file.getSize())
	{
		//get the next statement
		
		file.setPosition(position);	
		temp_position=position;
		flag = true;
		type = -1;
		mode = -1;

		while(flag == true)
		{
			file.setPosition(temp_position);
			character2 =  CreateStringFromByteArray(file.read(2));
		
			file.setPosition(temp_position);
			character =  CreateStringFromByteArray(file.read(1));

			//mode: ignore quotations
			if (character == "\"" && mode == -1)
			{
				mode = 1;
			}
			else if (character == "\"" && mode == 1)
			{
				mode = -1;
			}

			if (character2 == "\\\"" && mode == 1)
			{
			  temp_position++;
			}
			else if (temp_position == file_length)
			{
				flag = false;
				type = -1;
			}
			else if (character == ";" && mode != 1)
			{
			  type = 1;
				flag = false;
				temp_position++;
			}
			else if (character == "{" && mode != 1)
			{
			  type = 2;
				flag = false;
				temp_position++;
			}
			else
			{
				temp_position++;
			}
		}
		
		if (type == 2)
		{
		  flag = true;
			lbcount = 1;
			rbcount = 0;
			
			mode = -1;

			while(flag == true)
			{
				file.setPosition(temp_position);
				character2 =  CreateStringFromByteArray(file.read(2));

				file.setPosition(temp_position);
				character =  CreateStringFromByteArray(file.read(1));

				//mode: ignore quotations
				if (character == "\"" && mode == -1)
				{
					mode = 1;
				}
				else if (character == "\"" && mode == 1)
				{
					mode = -1;
				}
					

				if (temp_position == file_length)
				{
					flag = false;
				}
				else if (character2 == "\\\\" && mode == 1)
				{
					temp_position++;
				}
				else if (character2 == "\\\"" && mode == 1)
				{
					temp_position++;
				}
				else if (character == "{"&& mode == -1)
				{
					lbcount++;
				}
				else if (character == "}"&& mode == -1)
				{
					rbcount++;
				}

				temp_position++;
				
				if (lbcount == rbcount)
				  flag = false;
			}
		}
		
		if (position > 0)
			file.setPosition(position-1);
		else
			file.setPosition(0);
		statement_list[statement_counter]=CreateStringFromByteArray(file.read(temp_position-position+1));
		
		position = temp_position+1;	
		statement_counter++;
	}

	for (i=0;i<statement_list.length;i++)
	{
		statement_list[i] = AddVarToStrings(statement_list[i]);
		statement_list[i] = AddVarReturnToFunctions(statement_list[i]);
		statement_list[i] = AddTypeVarAndParameters(statement_list[i]);
	}

	for (i=0;i<statement_list.length;i++)
	{/*
		x=0;
		flag = true;
		while(flag)
		{
			while(AreKeysLeft())
			{
				switch(GetKey())
				{
					case KEY_DOWN:
					{
						x-=20;
						break;
					}
					case KEY_UP:
					{
						x+=20;
						break;
					}
					case KEY_ENTER:
					{
						x=0;
						flag = false;
						break;
					}
					case KEY_ESCAPE:
					{
						x=0;
						flag = false;
						break;
					}
				
				}
			}
		font.drawTextBox(0,x,GetScreenWidth(), GetScreenHeight(),0, statement_list[i]);
		FlipScreen();
		}
		*/
	}

	CompileStatements(statement_list, name);
	CompileHeader(statement_list, name);

	file2.close();
}


function AddVarToStrings(string)
{
	var positiony = 0;
	var mode = -1;

	while (positiony<string.length)
	{
	
	
		if (string.charAt(positiony) == "\"" && mode == -1)
		{
			mode = 1;
			string = string.substring(0, positiony)+"((var) "+string.substring(positiony, string.length);
			positiony+=7;
		}
		else if (mode == 1 && string.charAt(positiony) == "\"" && string.charAt(positiony-1) != "\\")
		{
			mode = -1;
			string = string.substring(0, positiony+1)+")"+string.substring(positiony+1, string.length);
			positiony+=1;
		}
		positiony++;
	}

	return string;
}


function AddVarReturnToFunctions(string)
{
	var positiony = 0;
	var flag = false;
	var bi=true;

	while (bi && positiony < string.length)
	{
		if (string.substr(positiony, 8) == "function" && 
		(string.charAt(positiony+7) == " " ||string.charAt(positiony+7) == "\t" ||string.charAt(positiony+7) == "\n"))
		{
			bi = false;
			flag = true;
		}
		else if (string.charAt(positiony) == ";")
		{
		  bi = false;
		}
		positiony++;
	}

	
	positiony = string.length-1;

	if (flag == true)
	{
		while (positiony>0)
		{
			if (string.charAt(positiony) == ";")
			{
				return string;
			}
			else if (string.charAt(positiony) == "}")
			{
				string = string.substring(0, positiony)+"return var();"+string.substring(positiony, string.length);
				positiony = 0;
			}
			positiony--;
		}
	}

	return string;
}



function AddTypeVarAndParameters(string)
{
	var positiony = 0;
	var flag = false;
	var bi=true;
	var position_tempy = 0;
	var head_pos = 0;

	while (bi && positiony < string.length)
	{

		if (string.substr(positiony, 8) == "function"&& 
		(string.charAt(positiony+8) == " " ||string.charAt(positiony+8) == "\t" ||string.charAt(positiony+8) == "\n"))
		{


			bi = false;
			flag = true;
		}
		else if (string.charAt(positiony) == ";")
		{
		  bi = false;
		}
		positiony++;



	}

	if (flag == true)
	{
		string = string.substring(0, positiony-1)+"var"+string.substring(positiony-1+8, string.length);
		//continue and do something about the parameters
		
		positiony++;
		
		head_pos = positiony-2;

		//check to see if there are any parameters


		bi = false;
		position_tempy = positiony;
		
		while (position_tempy < string.length)
		{
				if (string.charAt(position_tempy) == "(")
				{
					position_tempy++;
				  break;
				}
				position_tempy++;
		}
		
		position = position_tempy;

		while (position_tempy < string.length)
		{
			if (string.charAt(position_tempy) == ")")
			{
				break;
			}
			if (string.charAt(position_tempy) != "\n"&&string.charAt(position_tempy) != " "&&string.charAt(position_tempy) != "\t")
			{
				bi = true;
				break;
			}
			position_tempy++;
		}

		if (bi == true)
		{
			while (positiony < string.length)
			{
				if (string.charAt(positiony) == "(")
				{
					string = string.substring(0, positiony+1)+"var "+string.substring(positiony+1, string.length);
					positiony+=4;
				}
				else if (string.charAt(positiony) == ",")
				{
					string = string.substring(0, positiony)+"=undefined,var "+string.substring(positiony+1, string.length);
					positiony+=11+4;
				}
				else if (string.charAt(positiony) == ")")
				{
					string = string.substring(0, positiony)+"=undefined)"+string.substring(positiony+1, string.length);
					positiony+=7+4;
					break;
				}
				positiony++;
			}
		}

		//add the header into the header list
		positiony = head_pos;
		while(positiony<string.length)
		{
			if (string.charAt(positiony) == "{")
			{
				header_list[header_count] = string.substring(head_pos, positiony)+";";
				header_count++;
						
				break;
			}
			positiony++;
		
		}

	}
	else
	{
		//check for a starting "var" so we can add it into the header as well.
		positiony = 0;
		while (positiony < string.length)
		{
			if (string.substr(positiony, 3) == "var" && (string.charAt(positiony+3) == " " || string.charAt(positiony+3) == "\t" ||string.charAt(positiony+3) == "\n")
																							 && (string.charAt(positiony-1) == " " || string.charAt(positiony-1) == "\t" ||string.charAt(positiony-1) == "\n"||string.charAt(positiony-1) == ";"||string.charAt(positiony-1) == "}")
			)
			{
				//we have a variable here!
				
				position_tempy = positiony+3;

				while (position_tempy < string.length)
				{
					if (string.charAt(position_tempy) != " " && string.charAt(position_tempy) != "\t"&& string.charAt(position_tempy) != "\n")
					{
						break;
					}
					position_tempy++;
					
				}
					
				while (position_tempy < string.length)
				{
					if (string.charAt(position_tempy) == "=" || string.charAt(position_tempy) == ";" ||string.charAt(position_tempy) == " " ||string.charAt(position_tempy) == "\n" ||string.charAt(position_tempy) == "\t")
					{
						//got the variable name now...
						header_list[header_count] = string.substring(positiony, position_tempy)+";";
						/*
						GetSystemFont().drawTextBox(0,0, GetScreenWidth(), GetScreenHeight(),0,header_list[header_count]);
						FlipScreen();
						GetKey();*/
						
						header_count++;
						break;
							
					}
					position_tempy++;
				}
				break;
			}
			positiony++;
		
		
		}
	
	
	}

	




	return string;





}



function AddDeclaresToHeader(string)
{
	var positiony = 0;
	
	return string;
}




function ep_compile_remove_comments(script,name)
{
	var filey1 = OpenRawFile(script+name,false);
	var filey2 = OpenRawFile("COMPILE"+name,true);
	
	var position = 0;
	
	var curr_skip = -1;
	var character = "";
	var character2 = "";

	while(position < filey1.getSize())
	{
	  filey1.setPosition(position);
		character2 = CreateStringFromByteArray(filey1.read(2));
	  filey1.setPosition(position);
		character = CreateStringFromByteArray(filey1.read(1));

		if (character== "\"" && curr_skip == -1)
		{
			filey2.write(CreateByteArrayFromString(character));
			curr_skip = 3;
		}
		else if (character2== "\\\"" && curr_skip == 3)
		{
			filey2.write(CreateByteArrayFromString(character2));
			curr_skip = 3;
			position++;
		}
		else if (character== "\"" && curr_skip == 3)
		{
			filey2.write(CreateByteArrayFromString(character));
			curr_skip = -1;
		}
		else if (character== "\n" && curr_skip == 3)
		{
			filey2.write(CreateByteArrayFromString(character));
			curr_skip = -1;
		}
		else if (curr_skip == 3)
		{
			filey2.write(CreateByteArrayFromString(character));
			curr_skip = 3;
		}
		else if (character2!= "/*" && character2!= "//" && curr_skip == -1)
		{
			filey2.write(CreateByteArrayFromString(character));
			curr_skip = -1;
		}
		else if (character2 == "/*" && curr_skip == -1)
		{
			curr_skip = 2;
		}
		else if (character2 == "*/" && curr_skip == 2)
		{
			curr_skip = -1;
			position++;
		}
		else if (character2 == "//" && curr_skip == -1)
		{
			curr_skip = 1;
			position++;
		}
		else if (character == "\n" && curr_skip == 1)
		{
			curr_skip = -1;
//			position++;
		}

		position++;



	}
	filey2.close();
} 

















function CompileStatements(statements, name)
{
	var file_temp1 = OpenFile("../other/source/ep_"+name.substring(0,name.indexOf('.'))+".cpp");
	file_temp1.write("a","b");
	file_temp1.flush();
	file_temp1.close();

	var file_temp2 = OpenFile("../other/source/ep_"+name.substring(0,name.indexOf('.'))+".h");
	file_temp2.write("a","b");
	file_temp2.flush();
	file_temp2.close();


	var filey = OpenRawFile("./source/ep_"+name.substring(0,name.indexOf('.'))+".cpp",true);
	var filey2 = OpenRawFile("./source/ep_"+name.substring(0,name.indexOf('.'))+".h",true);





	
	var positiony = 0;
	var position_tempy = 0;
	
	var functions = "";
	var initializer = "";
	var bi = false;
	var flag = false;

	var j=0;

	initializer = "void "+name.substring(0, name.indexOf('.'))+"_initializer(){";
	
	var the_header = "";
	
	
	for (j=0;j<statements.length;j++)
	{
		//determine if this is a function statement, or an initializer
		string = statements[j];
		
		bi = true;
		positiony = 0;
		flag = false;
		
		while (bi && positiony < string.length)
		{
			if (string.substr(positiony, 3) == "var"&& 
			(string.charAt(positiony+3) == " " ||string.charAt(positiony+3) == "\t" ||string.charAt(positiony+3) == "\n"))
			{
				while (bi && positiony < string.length)
				{
					if (string.charAt(positiony) == ";")
					{
						bi = false;
						break;
					}
					else if (string.charAt(positiony) == "=")
					{
						bi = false;
						break;
					}
					else if (string.charAt(positiony) == "(")
					{
						bi = false;
						flag = true;
						break;
					}

					positiony++;


				
				}
				
				
				
				
				
				
			
			}
			else if (string.charAt(positiony) == ";")
			{
				bi = false;
				break;
			}
			positiony++;
		}
		
		if (flag == true)
		{
			//this is a function statment
			functions+=string;
		}
		else 
		{
			//is this a var statement?
			bi = false;
			
			positiony = 0;
			while (positiony < string.length)
			{
				if (string.substr(positiony, 3) == "var" && (string.charAt(positiony+3) == " " || string.charAt(positiony+3) == "\t" ||string.charAt(positiony+3) == "\n")
																								 && (string.charAt(positiony-1) == " " || string.charAt(positiony-1) == "\t" ||string.charAt(positiony-1) == "\n"||string.charAt(positiony-1) == ";"||string.charAt(positiony-1) == "}")
				)
				{
				  bi = true;
					//we have a variable here!
					position_tempy = positiony+3;
					while (position_tempy < string.length)
					{/*
						GetSystemFont().drawText(0,0, string.charAt(position_tempy));
						FlipScreen();
						GetKey();
					*/
						if (string.charAt(position_tempy)=="=")
						{
							//this is an assignment, copy everything after the var
							initializer+=string.substring(positiony+3, string.length)+"\n";
							break;
						}
						else if (string.charAt(position_tempy)==";")
						{
							//this is just a declaration, so ignore it.
							break;
						}
						position_tempy++;
					}
					break;
				}
				positiony++;
			}
			
			if (bi == false)
			{
				//this is not a declaration, so add to initializer
				initializer+=string;
			
			}
		}
	}
	initializer += "}";




/*

		var x=0;
		flag = true;
		while(flag)
		{
			while(AreKeysLeft())
			{
				switch(GetKey())
				{
					case KEY_DOWN:
					{
						x-=20;
						break;
					}
					case KEY_UP:
					{
						x+=20;
						break;
					}
					case KEY_ENTER:
					{
						x=0;
						flag = false;
						break;
					}
					case KEY_ESCAPE:
					{
						x=0;
						flag = false;
						break;
					}
				
				}
			}
		GetSystemFont().drawTextBox(0,x,GetScreenWidth(), GetScreenHeight(),0, initializer);
		FlipScreen();
		}


		x=0;
		flag = true;
		while(flag)
		{
			while(AreKeysLeft())
			{
				switch(GetKey())
				{
					case KEY_DOWN:
					{
						x-=20;
						break;
					}
					case KEY_UP:
					{
						x+=20;
						break;
					}
					case KEY_ENTER:
					{
						x=0;
						flag = false;
						break;
					}
					case KEY_ESCAPE:
					{
						x=0;
						flag = false;
						break;
					}
				
				}
			}
		GetSystemFont().drawTextBox(0,x,GetScreenWidth(), GetScreenHeight(),0, functions);
		FlipScreen();
		}
*/

		var cpp_file = ""

		cpp_file += "#ifndef "+("ep_"+name.substring(0, name.indexOf('.'))).toUpperCase()+"_CPP\n"
		cpp_file += "#define "+("ep_"+name.substring(0, name.indexOf('.'))).toUpperCase()+"_CPP\n"
		cpp_file += "#include \"../var.h\"\n\n";

		cpp_file += UniversalHeader;
		cpp_file += initializer+functions;

		cpp_file += "\n#endif"+"\n";


		filey.write(CreateByteArrayFromString(cpp_file));
		filey.close();



		the_header += "#ifndef "+("ep_"+name.substring(0, name.indexOf('.'))).toUpperCase()+"_H\n"
		the_header += "#define "+("ep_"+name.substring(0, name.indexOf('.'))).toUpperCase()+"_H\n"
		the_header += "#include \"../var.h\"\n\n";


		the_header += "void "+name.substring(0, name.indexOf('.'))+"_initializer();\n";

		for (j=0;j<header_count;j++)
		{
			the_header+=header_list[j]+"\n";
		}
		the_header += "#endif"+"\n"
/*
		GetSystemFont().drawTextBox(0,0,320,240, 0, the_header);
		FlipScreen();
		GetKey();
*/
		filey2.write(CreateByteArrayFromString(the_header));
		filey2.close();
} 


function CompileHeader(headers, name)
{

}


function WriteUniversalInitializer(array)
{
	var file_temp1 = OpenFile("../other/source/ep_Uni_Init.cpp");
	file_temp1.write("a","b");
	file_temp1.flush();
	file_temp1.close();

	var file_temp2 = OpenFile("../other/source/ep_Uni_Init.h");
	file_temp2.write("a","b");
	file_temp2.flush();
	file_temp2.close();



	var filey = OpenRawFile("./source/ep_Uni_Init.cpp",true);	

	var filey2 = OpenRawFile("./source/ep_Uni_Init.h",true);	

  var j=0;
	var string = "#ifndef EP_UNI_INIT_CPP\n";
	string +="#define EP_UNI_INIT_CPP\n";
	string +=""+UniversalHeader+"\n";
	string +="\n\n";
	
	string += "void UniversalInitializer()\n{\n";
	
	for (j=0;j<array.length;j++)
	{
		string += array[j].substring(0,array[j].indexOf('.'))+"_initializer();\n";
	}
	string += "}\n\n";

	string += "#endif";

	filey.write(CreateByteArrayFromString(string));
	filey.close();
	






	string = "#ifndef EP_UNI_INIT_H\n";
	string +="#define EP_UNI_INIT_H\n";
	string +="\n\n";
	
	string += "void UniversalInitializer();\n\n";
	string += "#endif";

	filey2.write(CreateByteArrayFromString(string));
	filey2.close();


	

}

