/* MasC interpreter/compiler
   (Master Assembly Script Container)
   written by Jared Bruni
   
   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
*/

// debug information implemntation file


#include "debug_info.h"

// get Reserved Byte code name as a string
char *getReservedCodeName(char code)
{
	switch(code) {
		case 99:
			return "reserved code: Numeric value";
			break;
		case 100:
			return "reserved code: String value";
			break;
		default:
			return "Terminating Symbol";
	}

	return "";
}

// get Symbol code name as a string
char *getSymbolCodeName(enum var_type vcode)
{
	switch(vcode) {
		case tVAR_INT:
			return "Integer";
		case tVAR_FLOAT:
			return "Floating point";
		case tVAR_STR:
			return "String";
		case tVAR_LABEL:
			return "Code Label";
		case tVAR_NONE:
			return "Void";
	}
	return "";
}

// print Debug information to html file
void printDebugInfo(char *debug_file, struct BackEnd *bend, struct Token *tokens)
{
     FILE *f_ptr = fopen(debug_file, "w");
	 struct symNode *node_ptr = 0;
	 int i = 0;
     if(!f_ptr) {
                printf("File debug.html could not be opened for writing\n");
                           
     }    
     
	 fprintf(f_ptr,"<html><title> Debug information for (%s) </title><body><font size=\"6\" color=\"#bd0000\" face=\"FixedSys\">", bend->obj_name);
	 fprintf(f_ptr, " Debug information for</font> <font size=\"6\" color=\"blue\" face=\"FixedSys\"> %s </font><br><br><hr>", bend->obj_name); 
	 fprintf(f_ptr, "<br><font size=\"4\" color=\"black\"> <TABLE border=\"4\" bgcolor=\"#FFFFFF\" BORDERCOLOR=\"#000000\" CELLPADDING=\"3\" CELLSPACING=\"0\"><br>");
     fprintf(f_ptr, "<TR> Intermediate Code </TR><br><TH> Byte Code </TH><TH> Symbols </TH> <TH> String </TH> <TH> Value </TH>");

	 while(tokens[i++].code != End) {
		 fprintf(f_ptr, "<TR><TH> %d </TH> <TH> %s </TH> <TH> %s </TH> <TH> %f </TH>", tokens[i].code, tokens[i].code < getReservedLen() ? syntax_str[tokens[i].code] : getReservedCodeName(tokens[i].code),tokens[i].text != 0 ? tokens[i].text :  "", (float)tokens[i].value == 0 ? 0 : (float)tokens[i].value );
	 }
	
	 fprintf(f_ptr, "</table><TABLE border=\"4\" bgcolor=\"#FFFFFF\" BORDERCOLOR=\"#000000\" CELLPADDING=\"3\" CELLSPACING=\"0\"><br><TR> Symbol Table</TR>");
	 fprintf(f_ptr, "<TR><TH> Entery ID </TH> <TH> Variable Type </TH> <TH> Numeric Value </TH><TH> String Value </TH>"); 
	 node_ptr = &bend->g_stable.root;
	 while(node_ptr != 0) {
		 fprintf(f_ptr, "<TR><TH> %s </TH> <TH> %s </TH> <TH> %f </TH> <TH> %s </TH>",node_ptr->str, getSymbolCodeName(node_ptr->vtype), (float)node_ptr->value, node_ptr->str_value);
		 node_ptr = node_ptr->next;
	 }

	 fprintf(f_ptr, "</font></table></body></html>");
     fclose(f_ptr);
}
// spun 81 lines of code

