home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / NGAWK1.ZIP / DEBUG.C < prev    next >
C/C++ Source or Header  |  1988-07-17  |  10KB  |  490 lines

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