home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / gawk213s.lzh / GAWK213S / DEBUG.C < prev    next >
C/C++ Source or Header  |  1993-07-29  |  10KB  |  502 lines

  1. /*
  2.  * debug.c -- Various debugging routines 
  3.  */
  4.  
  5. /* 
  6.  * Copyright (C) 1986, 1988, 1989, 1991 the Free Software Foundation, Inc.
  7.  * 
  8.  * This file is part of GAWK, the GNU implementation of the
  9.  * AWK Progamming Language.
  10.  * 
  11.  * GAWK is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 1, or (at your option)
  14.  * any later version.
  15.  * 
  16.  * GAWK is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  * 
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with GAWK; see the file COPYING.  If not, write to
  23.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25.  
  26. #include "awk.h"
  27.  
  28. #ifdef DEBUG
  29.  
  30. extern NODE **fields_arr;
  31.  
  32.  
  33. /* This is all debugging stuff.  Ignore it and maybe it'll go away. */
  34.  
  35. /*
  36.  * Some of it could be turned into a really cute trace command, if anyone
  37.  * wants to.  
  38.  */
  39. char *nnames[] = {
  40.     "illegal", "times", "quotient", "mod", "plus",
  41.     "minus", "cond_pair", "subscript", "concat", "exp",
  42.     /* 10 */
  43.     "preincrement", "predecrement", "postincrement", "postdecrement",
  44.     "unary_minus",
  45.     "field_spec", "assign", "assign_times", "assign_quotient", "assign_mod",
  46.     /* 20 */
  47.     "assign_plus", "assign_minus", "assign_exp", "and", "or",
  48.     "equal", "notequal", "less", "greater", "leq",
  49.     /* 30 */
  50.     "geq", "match", "nomatch", "not", "rule_list",
  51.     "rule_node", "statement_list", "if_branches", "expression_list",
  52.     "param_list",
  53.     /* 40 */
  54.     "K_if", "K_while", "K_for", "K_arrayfor", "K_break",
  55.     "K_continue", "K_print", "K_printf", "K_next", "K_exit",
  56.     /* 50 */
  57.     "K_do", "K_return", "K_delete", "K_getline", "K_function",
  58.     "redirect_output", "redirect_append", "redirect_pipe",
  59.     "redirect_pipein", "redirect_input",
  60.     /* 60 */
  61.     "var", "var_array", "val", "builtin", "line_range",
  62.     "in_array", "func", "func_call", "cond_exp", "regex",
  63.     /* 70 */
  64.     "hashnode", "ahash"
  65. };
  66.  
  67. ptree(n)
  68. NODE *n;
  69. {
  70.     print_parse_tree(n);
  71. }
  72.  
  73. static depth = 0;
  74.  
  75. print_parse_tree(ptr)
  76. NODE *ptr;
  77. {
  78.     if (!ptr) {
  79.         printf("NULL\n");
  80.         return;
  81.     }
  82.     if ((int) (ptr->type) < 0 || (int) (ptr->type) > sizeof(nnames) / sizeof(nnames[0])) {
  83.         printf("(0x%lx Type %d?!)\n", (long)ptr, ptr->type);
  84.         return;
  85.     }
  86.     printf("(%d)%*s", depth, depth, "");
  87.     switch ((int) ptr->type) {
  88.     case (int) Node_val:
  89.         printf("(0x%lx Value ", (long)ptr);
  90.         if (ptr->flags&STR)
  91.             printf("str: \"%.*s\" ", ptr->stlen, ptr->stptr);
  92.         if (ptr->flags&NUM)
  93.             printf("num: %g", ptr->numbr);
  94.         printf(")\n");
  95.         return;
  96.     case (int) Node_var_array:
  97.         {
  98.         struct search l;
  99.  
  100.         printf("(0x%lx Array)\n", (long)ptr);
  101.         for (assoc_scan(ptr, &l); l.retval; assoc_next(&l)) {
  102.             printf("\tindex: ");
  103.             print_parse_tree(l.retval);
  104.             printf("\tvalue: ");
  105.             print_parse_tree(*assoc_lookup(ptr, l.retval));
  106.             printf("\n");
  107.         }
  108.         return;
  109.         }
  110.     case Node_param_list:
  111.         printf("(0x%lx Local variable %s)\n", (long)ptr, ptr->param);
  112.         if (ptr->rnode)
  113.             print_parse_tree(ptr->rnode);
  114.         return;
  115.     case Node_regex:
  116.         printf("(0x%lx Regular expression %s\n", (long)ptr, ptr->re_text);
  117.         return;
  118.     }
  119.     if (ptr->lnode)
  120.         printf("0x%lx = left<--", (long)(ptr->lnode));
  121.     printf("(0x%lx %s.%d)", (long)ptr, nnames[(int) (ptr->type)], ptr->type);
  122.     if (ptr->rnode)
  123.         printf("-->right = 0x%lx", (long)(ptr->rnode));
  124.     printf("\n");
  125.     depth++;
  126.     if (ptr->lnode)
  127.         print_parse_tree(ptr->lnode);
  128.     switch ((int) ptr->type) {
  129.     case (int) Node_line_range:
  130.     case (int) Node_match:
  131.     case (int) Node_nomatch:
  132.         break;
  133.     case (int) Node_builtin:
  134.         printf("Builtin: %d\n", ptr->proc);
  135.         break;
  136.     case (int) Node_K_for:
  137.     case (int) Node_K_arrayfor:
  138.         printf("(%s:)\n", nnames[(int) (ptr->type)]);
  139.         print_parse_tree(ptr->forloop->init);
  140.         printf("looping:\n");
  141.         print_parse_tree(ptr->forloop->cond);
  142.         printf("doing:\n");
  143.         print_parse_tree(ptr->forloop->incr);
  144.         break;
  145.     default:
  146.         if (ptr->rnode)
  147.             print_parse_tree(ptr->rnode);
  148.         break;
  149.     }
  150.     --depth;
  151. }
  152.  
  153. /* VARARGS1 */
  154. print_debug(str, n)
  155. char *str;
  156. void *n;
  157. {
  158.     extern int debugging;
  159.  
  160.     if (debugging)
  161.         printf("%s:0x%lx\n", str, (long)n);
  162. }
  163.  
  164. int indent = 0;
  165.  
  166. print_a_node(ptr)
  167. NODE *ptr;
  168. {
  169.     NODE *p1;
  170.     char *str, *str2;
  171.     int n;
  172.     NODE *buc;
  173.  
  174.     if (!ptr)
  175.         return;        /* don't print null ptrs */
  176.     switch (ptr->type) {
  177.     case Node_val:
  178.         if (ptr->flags&NUM)
  179.             printf("%g", ptr->numbr);
  180.         else
  181.             printf("\"%.*s\"", ptr->stlen, ptr->stptr);
  182.         return;
  183.     case Node_times:
  184.         str = "*";
  185.         goto pr_twoop;
  186.     case Node_quotient:
  187.         str = "/";
  188.         goto pr_twoop;
  189.     case Node_mod:
  190.         str = "%";
  191.         goto pr_twoop;
  192.     case Node_plus:
  193.         str = "+";
  194.         goto pr_twoop;
  195.     case Node_minus:
  196.         str = "-";
  197.         goto pr_twoop;
  198.     case Node_exp:
  199.         str = "^";
  200.         goto pr_twoop;
  201.     case Node_concat:
  202.         str = " ";
  203.         goto pr_twoop;
  204.     case Node_assign:
  205.         str = "=";
  206.         goto pr_twoop;
  207.     case Node_assign_times:
  208.         str = "*=";
  209.         goto pr_twoop;
  210.     case Node_assign_quotient:
  211.         str = "/=";
  212.         goto pr_twoop;
  213.     case Node_assign_mod:
  214.         str = "%=";
  215.         goto pr_twoop;
  216.     case Node_assign_plus:
  217.         str = "+=";
  218.         goto pr_twoop;
  219.     case Node_assign_minus:
  220.         str = "-=";
  221.         goto pr_twoop;
  222.     case Node_assign_exp:
  223.         str = "^=";
  224.         goto pr_twoop;
  225.     case Node_and:
  226.         str = "&&";
  227.         goto pr_twoop;
  228.     case Node_or:
  229.         str = "||";
  230.         goto pr_twoop;
  231.     case Node_equal:
  232.         str = "==";
  233.         goto pr_twoop;
  234.     case Node_notequal:
  235.         str = "!=";
  236.         goto pr_twoop;
  237.     case Node_less:
  238.         str = "<";
  239.         goto pr_twoop;
  240.     case Node_greater:
  241.         str = ">";
  242.         goto pr_twoop;
  243.     case Node_leq:
  244.         str = "<=";
  245.         goto pr_twoop;
  246.     case Node_geq:
  247.         str = ">=";
  248.         goto pr_twoop;
  249.  
  250. pr_twoop:
  251.         print_a_node(ptr->lnode);
  252.         printf("%s", str);
  253.         print_a_node(ptr->rnode);
  254.         return;
  255.  
  256.     case Node_not:
  257.         str = "!";
  258.         str2 = "";
  259.         goto pr_oneop;
  260.     case Node_field_spec:
  261.         str = "$(";
  262.         str2 = ")";
  263.         goto pr_oneop;
  264.     case Node_postincrement:
  265.         str = "";
  266.         str2 = "++";
  267.         goto pr_oneop;
  268.     case Node_postdecrement:
  269.         str = "";
  270.         str2 = "--";
  271.         goto pr_oneop;
  272.     case Node_preincrement:
  273.         str = "++";
  274.         str2 = "";
  275.         goto pr_oneop;
  276.     case Node_predecrement:
  277.         str = "--";
  278.         str2 = "";
  279.         goto pr_oneop;
  280. pr_oneop:
  281.         printf(str);
  282.         print_a_node(ptr->subnode);
  283.         printf(str2);
  284.         return;
  285.  
  286.     case Node_expression_list:
  287.         print_a_node(ptr->lnode);
  288.         if (ptr->rnode) {
  289.             printf(",");
  290.             print_a_node(ptr->rnode);
  291.         }
  292.         return;
  293.  
  294.     case Node_var:
  295.         /*
  296.         for (n = 0; n < HASHSIZE; n++) {
  297.             for (buc = variables[n]; buc; buc = buc->hnext) {
  298.                 if (buc->hvalue == ptr) {
  299.                     printf("%.*s", buc->hlength, buc->hname);
  300.                     n = HASHSIZE;
  301.                     break;
  302.                 }
  303.             }
  304.         }
  305.         */
  306.         return;
  307.     case Node_subscript:
  308.         print_a_node(ptr->lnode);
  309.         printf("[");
  310.         print_a_node(ptr->rnode);
  311.         printf("]");
  312.         return;
  313.     case Node_builtin:
  314.         printf("some_builtin(");
  315.         print_a_node(ptr->subnode);
  316.         printf(")");
  317.         return;
  318.  
  319.     case Node_statement_list:
  320.         printf("{\n");
  321.         indent++;
  322.         for (n = indent; n; --n)
  323.             printf("  ");
  324.         while (ptr) {
  325.             print_maybe_semi(ptr->lnode);
  326.             if (ptr->rnode)
  327.                 for (n = indent; n; --n)
  328.                     printf("  ");
  329.             ptr = ptr->rnode;
  330.         }
  331.         --indent;
  332.         for (n = indent; n; --n)
  333.             printf("  ");
  334.         printf("}\n");
  335.         for (n = indent; n; --n)
  336.             printf("  ");
  337.         return;
  338.  
  339.     case Node_K_if:
  340.         printf("if(");
  341.         print_a_node(ptr->lnode);
  342.         printf(") ");
  343.         ptr = ptr->rnode;
  344.         if (ptr->lnode->type == Node_statement_list) {
  345.             printf("{\n");
  346.             indent++;
  347.             for (p1 = ptr->lnode; p1; p1 = p1->rnode) {
  348.                 for (n = indent; n; --n)
  349.                     printf("  ");
  350.                 print_maybe_semi(p1->lnode);
  351.             }
  352.             --indent;
  353.             for (n = indent; n; --n)
  354.                 printf("  ");
  355.             if (ptr->rnode) {
  356.                 printf("} else ");
  357.             } else {
  358.                 printf("}\n");
  359.                 return;
  360.             }
  361.         } else {
  362.             print_maybe_semi(ptr->lnode);
  363.             if (ptr->rnode) {
  364.                 for (n = indent; n; --n)
  365.                     printf("  ");
  366.                 printf("else ");
  367.             } else
  368.                 return;
  369.         }
  370.         if (!ptr->rnode)
  371.             return;
  372.         deal_with_curls(ptr->rnode);
  373.         return;
  374.  
  375.     case Node_K_while:
  376.         printf("while(");
  377.         print_a_node(ptr->lnode);
  378.         printf(") ");
  379.         deal_with_curls(ptr->rnode);
  380.         return;
  381.  
  382.     case Node_K_do:
  383.         printf("do ");
  384.         deal_with_curls(ptr->rnode);
  385.         printf("while(");
  386.         print_a_node(ptr->lnode);
  387.         printf(") ");
  388.         return;
  389.  
  390.     case Node_K_for:
  391.         printf("for(");
  392.         print_a_node(ptr->forloop->init);
  393.         printf(";");
  394.         print_a_node(ptr->forloop->cond);
  395.         printf(";");
  396.         print_a_node(ptr->forloop->incr);
  397.         printf(") ");
  398.         deal_with_curls(ptr->forsub);
  399.         return;
  400.     case Node_K_arrayfor:
  401.         printf("for(");
  402.         print_a_node(ptr->forloop->init);
  403.         printf(" in ");
  404.         print_a_node(ptr->forloop->incr);
  405.         printf(") ");
  406.         deal_with_curls(ptr->forsub);
  407.         return;
  408.  
  409.     case Node_K_printf:
  410.         printf("printf(");
  411.         print_a_node(ptr->lnode);
  412.         printf(")");
  413.         return;
  414.     case Node_K_print:
  415.         printf("print(");
  416.         print_a_node(ptr->lnode);
  417.         printf(")");
  418.         return;
  419.     case Node_K_next:
  420.         printf("next");
  421.         return;
  422.     case Node_K_break:
  423.         printf("break");
  424.         return;
  425.     case Node_K_delete:
  426.         printf("delete ");
  427.         print_a_node(ptr->lnode);
  428.         return;
  429.     case Node_func:
  430.         printf("function %s (", ptr->lnode->param);
  431.         if (ptr->lnode->rnode)
  432.             print_a_node(ptr->lnode->rnode);
  433.         printf(")\n");
  434.         print_a_node(ptr->rnode);
  435.         return;
  436.     case Node_param_list:
  437.         printf("%s", ptr->param);
  438.         if (ptr->rnode) {
  439.             printf(", ");
  440.             print_a_node(ptr->rnode);
  441.         }
  442.         return;
  443.     default:
  444.         print_parse_tree(ptr);
  445.         return;
  446.     }
  447. }
  448.  
  449. print_maybe_semi(ptr)
  450. NODE *ptr;
  451. {
  452.     print_a_node(ptr);
  453.     switch (ptr->type) {
  454.     case Node_K_if:
  455.     case Node_K_for:
  456.     case Node_K_arrayfor:
  457.     case Node_statement_list:
  458.         break;
  459.     default:
  460.         printf(";\n");
  461.         break;
  462.     }
  463. }
  464.  
  465. deal_with_curls(ptr)
  466. NODE *ptr;
  467. {
  468.     int n;
  469.  
  470.     if (ptr->type == Node_statement_list) {
  471.         printf("{\n");
  472.         indent++;
  473.         while (ptr) {
  474.             for (n = indent; n; --n)
  475.                 printf("  ");
  476.             print_maybe_semi(ptr->lnode);
  477.             ptr = ptr->rnode;
  478.         }
  479.         --indent;
  480.         for (n = indent; n; --n)
  481.             printf("  ");
  482.         printf("}\n");
  483.     } else {
  484.         print_maybe_semi(ptr);
  485.     }
  486. }
  487. #endif
  488.  
  489. #ifdef MEMDEBUG
  490.  
  491. #undef free
  492. extern void free();
  493.  
  494. void
  495. do_free(s)
  496. char *s;
  497. {
  498.     free(s);
  499. }
  500.  
  501. #endif
  502.