home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / AWK.ZIP / DEBUG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-09-09  |  11.7 KB  |  531 lines

  1. /**
  2.  * $Revision:   1.1  $
  3.  * $Log:   C:/AWK/DEBUG.C_V  $
  4.  * 
  5.  *    Rev 1.1   09 Sep 1988 18:30:40   vince
  6.  * MC 5.1 version
  7.  * 
  8.  *    Rev 1.0   09 Sep 1988 18:02:56   vince
  9.  * Original source
  10.  * 
  11.  *
  12.  * Debug.c -- Various debugging routines 
  13.  *
  14.  * Copyright (C) 1986 Free Software Foundation
  15.  *    Written by Jay Fenlason, December 1986 
  16.  *
  17.  *    Modifications by Andrew D. Estes, July 1988 
  18.  */
  19.  
  20. /**
  21.  * GAWK is distributed in the hope that it will be useful, but WITHOUT ANY
  22.  * WARRANTY.  No author or distributor accepts responsibility to anyone
  23.  * for the consequences of using it or for whether it serves any
  24.  * particular purpose or works at all, unless he says so in writing.
  25.  * Refer to the GAWK General Public License for full details.
  26.  * 
  27.  * Everyone is granted permission to copy, modify and redistribute GAWK,
  28.  * but only under the conditions described in the GAWK General Public
  29.  * License.  A copy of this license is supposed to have been given to you
  30.  * along with GAWK so you can know your rights and responsibilities.  It
  31.  * should be in a file named COPYING.  Among other things, the copyright
  32.  * notice and this notice must be preserved on all copies.
  33.  * 
  34.  * In other words, go ahead and share GAWK, but don't try to stop
  35.  * anyone else from sharing it farther.  Help stamp out software hoarding!
  36.  */
  37.  
  38. #include <stdio.h>
  39. #include "awk.h"
  40.  
  41. #ifndef FAST
  42.  
  43. extern NODE **fields_arr;
  44. extern f_arr_siz;
  45.  
  46. /* This is all debugging stuff.  Ignore it and maybe it'll go away. */
  47.  
  48. /*
  49.  * Some of it could be turned into a really cute trace command, if anyone wants to.  
  50.  */
  51. char *nnames[] = {
  52.    "Illegal Node",
  53.    "Pos", "Times", "Divide", "Mod", "Plus", "Minus",
  54.    "Cond-pair" /* jfw */ , "Subscript", "Concat",
  55.    "++Pre", "--Pre", "Post++",
  56.    "Post--", "Uminus", "Field",
  57.    "Assign", "^=", "*=", "/=", "%=",
  58.    "+=", "-=", "x?y:z",
  59.    "And", "Or",
  60.    "Equal", "!=", "Less", "Greater", "<=", ">=",
  61.    "Not",
  62.    "Match", "Nomatch",
  63.    "String", "TmpString", "Number", "Regexp",
  64.    "Rule_list", "Rule_node", "State_list", "If_branches", "Exp_list",
  65.    "BEGIN", "END", "IF", "WHILE", "FOR",
  66.    "arrayfor", "BREAK", "CONTINUE", "GETLINE", "PRINT", "PRINTF",
  67.    "next", "exit", "redirect", "Append",
  68.    "Pipe", "redir in", "variable", "Varray", "builtin",
  69.    "Line-range" /* jfw */ ,
  70. };
  71.  
  72. ptree(n)
  73. INT n;
  74. {
  75.    print_parse_tree((NODE *) n);
  76. }
  77.  
  78. pt()
  79. {
  80.    INT x;
  81.    scanf("%x", &x);
  82.    printf("0x%x\n", x);
  83.    print_parse_tree((NODE *) x);
  84.    fflush(stdout);
  85. }
  86.  
  87. static depth = 0;
  88. print_parse_tree(ptr)
  89. NODE *ptr;
  90. {
  91.    register int n;
  92.  
  93.    if (!ptr)
  94.    {
  95.       printf("NULL\n");
  96.       return;
  97.    }
  98.    if ((int) (ptr->type) < 0 || (int) (ptr->type) > sizeof(nnames) / sizeof(nnames[0]))
  99.    {
  100.       printf("(%p Type %d??)\n", ptr, ptr->type);
  101.       return;
  102.    }
  103.    printf("(%d)%*s", depth, depth, "");
  104.    switch ((int) ptr->type)
  105.    {
  106.    case (int) Node_string:
  107.    case (int) Node_temp_string:
  108.       printf("(%p String \"%.*s\")\n", ptr, ptr->stlen, ptr->stptr);
  109.       return;
  110.    case (int) Node_number:
  111.       printf("(%p Number %g)\n", ptr, ptr->numbr);
  112.       return;
  113.    case (int) Node_var_array:
  114.       printf("(%p Array of %d)\n", ptr, ptr->arrsiz);
  115.       for (n = 0; n < ptr->arrsiz; n++)
  116.       {
  117.          printf("'");
  118.          print_simple((ptr->array)[n * 2], stdout);
  119.          printf("' is '");
  120.          print_simple((ptr->array)[n * 2 + 1], stdout);
  121.          printf("'\n");
  122.       }
  123.       return;
  124.    }
  125.    if (ptr->lnode)
  126.       printf("%p = left<--", ptr->lnode);
  127.    printf("(%p %s.%d)", ptr, nnames[(int) (ptr->type)], ptr->type);
  128.    if (ptr->rnode)
  129.       printf("-->right = %p", ptr->rnode);
  130.    printf("\n");
  131.    depth++;
  132.    if (ptr->lnode)
  133.       print_parse_tree(ptr->lnode);
  134.    switch ((int) ptr->type)
  135.    {
  136.    case (int) Node_line_range:        /* jfw */
  137.    case (int) Node_match:
  138.    case (int) Node_nomatch:
  139.       break;
  140.    case (int) Node_builtin:
  141.       printf("Builtin: %p\n", ptr->proc);       /* jfw: was \N */
  142.       break;
  143.    case (int) Node_K_for:
  144.    case (int) Node_K_arrayfor:
  145.       printf("(%s:)\n", nnames[(int) (ptr->type)]);
  146.       print_parse_tree(ptr->forloop->init);
  147.       printf("looping:\n");
  148.       print_parse_tree(ptr->forloop->cond);
  149.       printf("doing:\n");
  150.       print_parse_tree(ptr->forloop->incr);
  151.       break;
  152.    default:
  153.       if (ptr->rnode)
  154.          print_parse_tree(ptr->rnode);
  155.       break;
  156.    }
  157.    --depth;
  158. }
  159. #endif
  160.  
  161. #ifndef FAST
  162. /*
  163.  * print out all the variables in the world 
  164.  */
  165.  
  166. dump_vars()
  167. {
  168.    register int n;
  169.    register HASHNODE *buc;
  170.  
  171.    printf("Fields:");
  172.    dump_fields();
  173.    printf("Vars:\n");
  174.    for (n = 0; n < HASHSIZE; n++)
  175.    {
  176.       for (buc = variables[n]; buc; buc = buc->next)
  177.       {
  178.          printf("'%.*s': ", buc->length, buc->name);
  179.          print_simple(buc->value->var_value, stdout);
  180.          printf(":");
  181.          print_parse_tree(buc->value->lnode);
  182.          /* print_parse_tree(buc->value); */
  183.       }
  184.    }
  185.    printf("End\n");
  186. }
  187. #endif
  188.  
  189. #ifndef FAST
  190. dump_fields()
  191. {
  192.    register NODE **p;
  193.    register int n;
  194.  
  195.    printf("%d fields\n", f_arr_siz);
  196.    for (n = 0, p = &fields_arr[0]; n < f_arr_siz; n++, p++)
  197.    {
  198.       printf("$%d is '", n);
  199.       print_simple(*p, stdout);
  200.       printf("'\n");
  201.    }
  202. }
  203. #endif
  204.  
  205. #ifndef FAST
  206. /* VARARGS1 */
  207. print_debug(str, n)
  208. char *str;
  209. {
  210.    extern int debugging;
  211.  
  212.    if (debugging)
  213.       printf("%s:0x%x\n", str, n);
  214. }
  215.  
  216. int indent = 0;
  217.  
  218. print_a_node(ptr)
  219. NODE *ptr;
  220. {
  221.    NODE *p1;
  222.    char *str, *str2;
  223.    int n;
  224.    HASHNODE *buc;
  225.  
  226.    if (!ptr)
  227.       return;
  228.    switch (ptr->type)
  229.    {
  230.    case Node_number:
  231.       printf("0x%x", ptr->numbr);
  232.       return;
  233.    case Node_string:
  234.       printf("\"%.*s\"", ptr->stlen, ptr->stptr);
  235.       return;
  236.    case Node_times:
  237.       str = "*";
  238.       goto pr_twoop;
  239.    case Node_quotient:
  240.       str = "/";
  241.       goto pr_twoop;
  242.    case Node_mod:
  243.       str = "%";
  244.       goto pr_twoop;
  245.    case Node_plus:
  246.       str = "+";
  247.       goto pr_twoop;
  248.    case Node_minus:
  249.       str = "-";
  250.       goto pr_twoop;
  251.    case Node_concat:
  252.       str = " ";
  253.       goto pr_twoop;
  254.    case Node_assign:
  255.       str = "=";
  256.       goto pr_twoop;
  257.    case Node_assign_times:
  258.       str = "*=";
  259.       goto pr_twoop;
  260.    case Node_assign_quotient:
  261.       str = "/=";
  262.       goto pr_twoop;
  263.    case Node_assign_mod:
  264.       str = "%=";
  265.       goto pr_twoop;
  266.    case Node_assign_plus:
  267.       str = "+=";
  268.       goto pr_twoop;
  269.    case Node_assign_minus:
  270.       str = "-=";
  271.       goto pr_twoop;
  272.    case Node_and:
  273.       str = "&&";
  274.       goto pr_twoop;
  275.    case Node_or:
  276.       str = "||";
  277.       goto pr_twoop;
  278.    case Node_equal:
  279.       str = "==";
  280.       goto pr_twoop;
  281.    case Node_notequal:
  282.       str = "!=";
  283.       goto pr_twoop;
  284.    case Node_less:
  285.       str = "<";
  286.       goto pr_twoop;
  287.    case Node_greater:
  288.       str = ">";
  289.       goto pr_twoop;
  290.    case Node_leq:
  291.       str = "<=";
  292.       goto pr_twoop;
  293.    case Node_geq:
  294.       str = ">=";
  295.       goto pr_twoop;
  296.  
  297. pr_twoop:
  298.       print_a_node(ptr->lnode);
  299.       printf("%s", str);
  300.       print_a_node(ptr->rnode);
  301.       return;
  302.  
  303.    case Node_not:
  304.       str = "!";
  305.       str2 = "";
  306.       goto pr_oneop;
  307.    case Node_field_spec:
  308.       str = "$(";
  309.       str2 = ")";
  310.       goto pr_oneop;
  311.    case Node_postincrement:
  312.       str = "";
  313.       str2 = "++";
  314.       goto pr_oneop;
  315.    case Node_postdecrement:
  316.       str = "";
  317.       str2 = "--";
  318.       goto pr_oneop;
  319.    case Node_preincrement:
  320.       str = "++";
  321.       str2 = "";
  322.       goto pr_oneop;
  323.    case Node_predecrement:
  324.       str = "--";
  325.       str2 = "";
  326.       goto pr_oneop;
  327. pr_oneop:
  328.       printf(str);
  329.       print_a_node(ptr->subnode);
  330.       printf(str2);
  331.       return;
  332.  
  333.    case Node_expression_list:
  334.       print_a_node(ptr->lnode);
  335.       if (ptr->rnode)
  336.       {
  337.          printf(",");
  338.          print_a_node(ptr->rnode);
  339.       }
  340.       return;
  341.  
  342.    case Node_var:
  343.       for (n = 0; n < HASHSIZE; n++)
  344.       {
  345.          for (buc = variables[n]; buc; buc = buc->next)
  346.          {
  347.             if (buc->value == ptr)
  348.             {
  349.                printf("%.*s", buc->length, buc->name);
  350.                n = HASHSIZE;
  351.                break;
  352.             }
  353.          }
  354.       }
  355.       return;
  356.    case Node_subscript:
  357.       print_a_node(ptr->lnode);
  358.       printf("[");
  359.       print_a_node(ptr->rnode);
  360.       printf("]");
  361.       return;
  362.    case Node_builtin:
  363.       printf("some_builtin(");
  364.       print_a_node(ptr->subnode);
  365.       printf(")");
  366.       return;
  367.  
  368.    case Node_statement_list:
  369.       printf("{\n");
  370.       indent++;
  371.       for (n = indent; n; --n)
  372.          printf("  ");
  373.       while (ptr)
  374.       {
  375.          print_maybe_semi(ptr->lnode);
  376.          if (ptr->rnode)
  377.             for (n = indent; n; --n)
  378.                printf("  ");
  379.          ptr = ptr->rnode;
  380.       }
  381.       --indent;
  382.       for (n = indent; n; --n)
  383.          printf("  ");
  384.       printf("}\n");
  385.       for (n = indent; n; --n)
  386.          printf("  ");
  387.       return;
  388.  
  389.    case Node_K_if:
  390.       printf("if(");
  391.       print_a_node(ptr->lnode);
  392.       printf(") ");
  393.       ptr = ptr->rnode;
  394.       if (ptr->lnode->type == Node_statement_list)
  395.       {
  396.          printf("{\n");
  397.          indent++;
  398.          for (p1 = ptr->lnode; p1; p1 = p1->rnode)
  399.          {
  400.             for (n = indent; n; --n)
  401.                printf("  ");
  402.             print_maybe_semi(p1->lnode);
  403.          }
  404.          --indent;
  405.          for (n = indent; n; --n)
  406.             printf("  ");
  407.          if (ptr->rnode)
  408.          {
  409.             printf("} else ");
  410.          }
  411.          else
  412.          {
  413.             printf("}\n");
  414.             return;
  415.          }
  416.       }
  417.       else
  418.       {
  419.          print_maybe_semi(ptr->lnode);
  420.          if (ptr->rnode)
  421.          {
  422.             for (n = indent; n; --n)
  423.                printf("  ");
  424.             printf("else ");
  425.          }
  426.          else
  427.             return;
  428.       }
  429.       if (!ptr->rnode)
  430.          return;
  431.       deal_with_curls(ptr->rnode);
  432.       return;
  433.  
  434.    case Node_K_for:
  435.       printf("for(");
  436.       print_a_node(ptr->forloop->init);
  437.       printf(";");
  438.       print_a_node(ptr->forloop->cond);
  439.       printf(";");
  440.       print_a_node(ptr->forloop->incr);
  441.       printf(") ");
  442.       deal_with_curls(ptr->forsub);
  443.       return;
  444.    case Node_K_arrayfor:
  445.       printf("for(");
  446.       print_a_node(ptr->forloop->init);
  447.       printf(" in ");
  448.       print_a_node(ptr->forloop->incr);
  449.       printf(") ");
  450.       deal_with_curls(ptr->forsub);
  451.       return;
  452.  
  453.    case Node_K_printf:
  454.       printf("printf(");
  455.       print_a_node(ptr->lnode);
  456.       printf(")");
  457.       return;
  458.    case Node_K_print:
  459.       printf("print(");
  460.       print_a_node(ptr->lnode);
  461.       printf(")");
  462.       return;
  463.    case Node_K_next:
  464.       printf("next");
  465.       return;
  466.    case Node_K_break:
  467.       printf("break");
  468.       return;
  469.    default:
  470.       print_parse_tree(ptr);
  471.       return;
  472.    }
  473. }
  474.  
  475. print_maybe_semi(ptr)
  476. NODE *ptr;
  477. {
  478.    print_a_node(ptr);
  479.    switch (ptr->type)
  480.    {
  481.    case Node_K_if:
  482.    case Node_K_for:
  483.    case Node_K_arrayfor:
  484.    case Node_statement_list:
  485.       break;
  486.    default:
  487.       printf(";\n");
  488.       break;
  489.    }
  490. }
  491.  
  492. deal_with_curls(ptr)
  493. NODE *ptr;
  494. {
  495.    int n;
  496.  
  497.    if (ptr->type == Node_statement_list)
  498.    {
  499.       printf("{\n");
  500.       indent++;
  501.       while (ptr)
  502.       {
  503.          for (n = indent; n; --n)
  504.             printf("  ");
  505.          print_maybe_semi(ptr->lnode);
  506.          ptr = ptr->rnode;
  507.       }
  508.       --indent;
  509.       for (n = indent; n; --n)
  510.          printf("  ");
  511.       printf("}\n");
  512.    }
  513.    else
  514.    {
  515.       print_maybe_semi(ptr);
  516.    }
  517. }
  518.  
  519. NODE *do_prvars()
  520. {
  521.    dump_vars();
  522.    return Nnull_string;
  523. }
  524.  
  525. NODE *do_bp()
  526. {
  527.    return Nnull_string;
  528. }
  529.  
  530. #endif
  531.