home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / unix / gawk.sit / source / debug.c < prev    next >
Text File  |  1990-08-18  |  11KB  |  563 lines

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