debug.c


    1 #include <stdio.h>
    2 #include "AST.h"
    3 #include "debug.h"
    4 
    5 char *get_string(enum AST_type ast_type) {
    6 	char *buf;
    7 
    8 	if (ast_type == AST_translation_unit_single)
    9 		buf = "AST_translation_unit_single";
   10 	else if (ast_type == AST_translation_unit_pair)
   11 		buf = "AST_translation_unit_pair";
   12 	else if (ast_type == AST_external_declaration_func)
   13 		buf = "AST_external_declaration_func";
   14 	else if (ast_type == AST_external_declaration_dec)
   15 		buf = "AST_external_declaration_dec";
   16 	else if (ast_type == AST_function_definition)
   17 		buf = "AST_function_definition";
   18 	else if (ast_type == AST_declaration_list_null)
   19 		buf = "AST_declaration_list_null";
   20 	else if (ast_type == AST_declaration_list_pair)
   21 		buf = "AST_declaration_list_pair";
   22 	else if (ast_type == AST_declaration)
   23 		buf = "AST_declaration";
   24 	else if (ast_type == AST_type_specifier_void)
   25 		buf = "AST_type_specifier_void";
   26 	else if (ast_type == AST_type_specifier_int)
   27 		buf = "AST_type_specifier_int";
   28 	else if (ast_type == AST_type_specifier_char)
   29 		buf = "AST_type_specifier_char";
   30 	else if (ast_type == AST_declarator_id)
   31 		buf = "AST_declarator_id";
   32 	else if (ast_type == AST_declarator_pointer)
   33 		buf = "AST_declarator_pointer";
   34 	else if (ast_type == AST_declarator_paren)
   35 		buf = "AST_declarator_paren";
   36 	else if (ast_type == AST_declarator_func)
   37 		buf = "AST_declarator_func";
   38 	else if (ast_type == AST_declarator_func_null)
   39 		buf = "AST_declarator_func_null";
   40 	else if (ast_type == AST_parameter_list_single)
   41 		buf = "AST_parameter_list_single";
   42 	else if (ast_type == AST_parameter_list_pair)
   43 		buf = "AST_parameter_list_pair";
   44 	else if (ast_type == AST_parameter_declaration)
   45 		buf = "AST_parameter_declaration";
   46 	else if (ast_type == AST_statement_list_single)
   47 		buf = "AST_statement_list_single";
   48 	else if (ast_type == AST_statement_list_pair)
   49 		buf = "AST_statement_list_pair";
   50 	else if (ast_type == AST_statement_null)
   51 		buf = "AST_statement_null";
   52 	else if (ast_type == AST_statement_exp)
   53 		buf = "AST_statement_exp";
   54 	else if (ast_type == AST_statement_comp)
   55 		buf = "AST_statement_comp";
   56 	else if (ast_type == AST_statement_if)
   57 		buf = "AST_statement_if";
   58 	else if (ast_type == AST_statement_if_else)
   59 		buf = "AST_statement_if_else";
   60 	else if (ast_type == AST_statement_while)
   61 		buf = "AST_statement_while";
   62 	else if (ast_type == AST_statement_goto)
   63 		buf = "AST_statement_goto";
   64 	else if (ast_type == AST_statement_label)
   65 		buf = "AST_statement_label";
   66 	else if (ast_type == AST_statement_return)
   67 		buf = "AST_statement_return";
   68 	else if (ast_type == AST_statement_return_null)
   69 		buf = "AST_statement_return_null";
   70 	else if (ast_type == AST_compound_statement)
   71 		buf = "AST_compound_statement";
   72 	else if (ast_type == AST_expression_id)
   73 		buf = "AST_expression_id";
   74 	else if (ast_type == AST_expression_int)
   75 		buf = "AST_expression_int";
   76 	else if (ast_type == AST_expression_char)
   77 		buf = "AST_expression_char";
   78 	else if (ast_type == AST_expression_string)
   79 		buf = "AST_expression_string";
   80 	else if (ast_type == AST_expression_assign)
   81 		buf = "AST_expression_assign";
   82 	else if (ast_type == AST_expression_lss)
   83 		buf = "AST_expression_lss";
   84 	else if (ast_type == AST_expression_gtr)
   85 		buf = "AST_expression_gtr";
   86 	else if (ast_type == AST_expression_eq)
   87 		buf = "AST_expression_eq";
   88 	else if (ast_type == AST_expression_neq)
   89 		buf = "AST_expression_neq";
   90 	else if (ast_type == AST_expression_leq)
   91 		buf = "AST_expression_leq";
   92 	else if (ast_type == AST_expression_geq)
   93 		buf = "AST_expression_geq";
   94 	else if (ast_type == AST_expression_and)
   95 		buf = "AST_expression_and";
   96 	else if (ast_type == AST_expression_or)
   97 		buf = "AST_expression_or";
   98 	else if (ast_type == AST_expression_add)
   99 		buf = "AST_expression_add";
  100 	else if (ast_type == AST_expression_sub)
  101 		buf = "AST_expression_sub";
  102 	else if (ast_type == AST_expression_mult)
  103 		buf = "AST_expression_mult";
  104 	else if (ast_type == AST_expression_div)
  105 		buf = "AST_expression_div";
  106 	else if (ast_type == AST_expression_mod)
  107 		buf = "AST_expression_mod";
  108 	else if (ast_type == AST_expression_unary_address)
  109 		buf = "AST_expression_unary_address";
  110 	else if (ast_type == AST_expression_unary_deref)
  111 		buf = "AST_expression_unary_deref";
  112 	else if (ast_type == AST_expression_unary_plus)
  113 		buf = "AST_expression_unary_plus";
  114 	else if (ast_type == AST_expression_unary_minus)
  115 		buf = "AST_expression_unary_minus";
  116 	else if (ast_type == AST_expression_unary_negative)
  117 		buf = "AST_expression_unary_negative";
  118 	else if (ast_type == AST_expression_func)
  119 		buf = "AST_expression_func";
  120 	else if (ast_type == AST_expression_func_null)
  121 		buf = "AST_expression_func_null";
  122 	else if (ast_type == AST_expression_paren)
  123 		buf = "AST_expression_paren";
  124 	else if (ast_type == AST_argument_expression_list_single)
  125 		buf = "AST_argument_expression_list_single";
  126 	else if (ast_type == AST_argument_expression_list_pair)
  127 		buf = "AST_argument_expression_list_pair";
  128 	else if (ast_type == AST_IDENTIFIER)
  129 		buf = "AST_IDENTIFIER";
  130 	else if (ast_type == AST_dummy)
  131 		buf = "AST_dummy";
  132 	else
  133 		buf = "?????";
  134 	return buf;
  135 }
  136 
  137 void debug(struct AST *ast, FILE *fp) {
  138 	int i;
  139 
  140 	fprintf(fp, "<AST ast_type=\"%s\" num_child=\"%d\">\n",
  141 			get_string(ast->ast_type), ast->num_child);
  142 		switch (ast->ast_type) {
  143 		case AST_IDENTIFIER:
  144 		case AST_expression_string:
  145 			fprintf(fp, "<id str=\"%s\"/>\n", ast->u.id);
  146 			break;
  147 		case AST_expression_int:
  148 			fprintf(fp, "<int val=\"%d\"/>\n", ast->u.value);
  149 			break;
  150 		case AST_expression_char:
  151 			fprintf(fp, "<char c=\"%c\"/>\n", ast->u.value);
  152 			break;
  153 		default:
  154 			if (ast->num_child != 0)
  155 				for (i = 0; i < ast->num_child; i++)
  156 					debug(ast->u.child[i], fp);
  157 			break;
  158 		}
  159 	fprintf(fp, "</AST>\n");
  160 }