home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 419b.lha / GNU_Awk_v2.10_Beta / awk6.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  12KB  |  600 lines

  1. /*
  2.  * awk6.c -- Various debugging routines 
  3.  *
  4.  * $Log:    awk6.c,v $
  5.  * Revision 1.13  89/03/31  13:26:12  david
  6.  * GNU license
  7.  * 
  8.  * Revision 1.12  89/03/29  14:11:54  david
  9.  * delinting
  10.  * 
  11.  * Revision 1.11  89/03/24  15:58:04  david
  12.  * HASHNODE becomes NODE
  13.  * 
  14.  * Revision 1.10  89/03/21  10:48:51  david
  15.  * minor cleanup
  16.  * 
  17.  * Revision 1.9  89/03/15  22:06:37  david
  18.  * remove old case stuff
  19.  * 
  20.  * Revision 1.8  88/11/22  13:51:34  david
  21.  * Arnold: changes for case-insensitive matching
  22.  * 
  23.  * Revision 1.7  88/11/15  10:28:08  david
  24.  * Arnold: minor cleanup
  25.  * 
  26.  * Revision 1.6  88/11/01  12:20:46  david
  27.  * small improvements to debugging code
  28.  * 
  29.  * Revision 1.5  88/10/17  20:53:37  david
  30.  * purge FAST
  31.  * 
  32.  * Revision 1.4  88/05/31  09:56:39  david
  33.  * oops! fix to last change
  34.  * 
  35.  * Revision 1.3  88/05/31  09:25:48  david
  36.  * expunge Node_local_var
  37.  * 
  38.  * Revision 1.2  88/04/15  13:15:47  david
  39.  * brought slightly up-to-date
  40.  * 
  41.  * Revision 1.1  88/04/08  15:14:38  david
  42.  * Initial revision
  43.  *  Revision 1.5  88/04/08  14:48:39  david changes from
  44.  * Arnold Robbins 
  45.  *
  46.  * Revision 1.4  88/03/28  14:13:57  david *** empty log message *** 
  47.  *
  48.  * Revision 1.3  88/03/18  21:00:15  david Baseline -- hoefully all the
  49.  * functionality of the new awk added. Just debugging and tuning to do. 
  50.  *
  51.  * Revision 1.2  87/11/19  14:41:07  david trying to keep it up to date with
  52.  * changes elsewhere ... 
  53.  *
  54.  * Revision 1.1  87/10/27  15:23:36  david Initial revision 
  55.  *
  56.  */
  57.  
  58. /* 
  59.  * Copyright (C) 1986, 1988, 1989 the Free Software Foundation, Inc.
  60.  * 
  61.  * This file is part of GAWK, the GNU implementation of the
  62.  * AWK Progamming Language.
  63.  * 
  64.  * GAWK is free software; you can redistribute it and/or modify
  65.  * it under the terms of the GNU General Public License as published by
  66.  * the Free Software Foundation; either version 1, or (at your option)
  67.  * any later version.
  68.  * 
  69.  * GAWK is distributed in the hope that it will be useful,
  70.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  71.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  72.  * GNU General Public License for more details.
  73.  * 
  74.  * You should have received a copy of the GNU General Public License
  75.  * along with GAWK; see the file COPYING.  If not, write to
  76.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  77.  */
  78.  
  79. #include "awk.h"
  80.  
  81. #ifdef DEBUG
  82.  
  83. extern NODE **fields_arr;
  84.  
  85.  
  86. /* This is all debugging stuff.  Ignore it and maybe it'll go away. */
  87.  
  88. /*
  89.  * Some of it could be turned into a really cute trace command, if anyone
  90.  * wants to.  
  91.  */
  92. char *nnames[] = {
  93.           "Illegal Node",
  94.           "Times", "Divide", "Mod", "Plus", "Minus",
  95.           "Cond-pair", "Subscript", "Concat",
  96.           "++Pre", "--Pre", "Post++",
  97.           "Post--", "Uminus", "Field",
  98.           "Assign", "*=", "/=", "%=",
  99.           "+=", "-=",
  100.           "And", "Or",
  101.           "Equal", "!=", "Less", "Greater", "<=", ">=",
  102.           "Not",
  103.           "Match", "Nomatch",
  104.           "String", "TmpString", "Number",
  105.       "Rule_list", "Rule_node", "State_list", "If_branches", "Exp_list",
  106.           "Param_list",
  107.           "BEGIN", "END", "IF", "WHILE",
  108.           "FOR",
  109.           "arrayfor", "BREAK", "CONTINUE", "PRINT", "PRINTF",
  110.  
  111.           "next", "exit", "DO", "RETURN", "DELETE",
  112.           "redirect", "Append", "pipe", "Pipe in",
  113.           "redirect input", "variable", "Varray",
  114.           "builtin", "Line-range",
  115.           "In_Array", "FUNCTION", "function def", "function call",
  116.           "local variable",
  117.           "getline", "sub", "gsub", "match", "?:",
  118.           "^", "^=", "/regexp/", "Str_num",
  119.           "~~", "!~~",
  120. };
  121.  
  122. ptree(n)
  123. NODE *n;
  124. {
  125.     print_parse_tree(n);
  126. }
  127.  
  128. pt()
  129. {
  130.     int x;
  131.  
  132.     (void) scanf("%x", &x);
  133.     printf("0x%x\n", x);
  134.     print_parse_tree((NODE *) x);
  135.     fflush(stdout);
  136. }
  137.  
  138. static depth = 0;
  139.  
  140. print_parse_tree(ptr)
  141. NODE *ptr;
  142. {
  143.     if (!ptr) {
  144.         printf("NULL\n");
  145.         return;
  146.     }
  147.     if ((int) (ptr->type) < 0 || (int) (ptr->type) > sizeof(nnames) / sizeof(nnames[0])) {
  148.         printf("(0x%x Type %d??)\n", ptr, ptr->type);
  149.         return;
  150.     }
  151.     printf("(%d)%*s", depth, depth, "");
  152.     switch ((int) ptr->type) {
  153.     case (int) Node_val:
  154.         printf("(0x%x Value ", ptr);
  155.         if (ptr->flags&STR)
  156.             printf("str: \"%.*s\" ", ptr->stlen, ptr->stptr);
  157.         if (ptr->flags&NUM)
  158.             printf("num: %g", ptr->numbr);
  159.         printf(")\n");
  160.         return;
  161.     case (int) Node_var_array:
  162.         {
  163.         struct search *l;
  164.  
  165.         printf("(0x%x Array)\n", ptr);
  166.         for (l = assoc_scan(ptr); l; l = assoc_next(l)) {
  167.             printf("\tindex: ");
  168.             print_parse_tree(l->retval);
  169.             printf("\tvalue: ");
  170.             print_parse_tree(*assoc_lookup(ptr, l->retval));
  171.             printf("\n");
  172.         }
  173.         return;
  174.         }
  175.     case Node_param_list:
  176.         printf("(0x%x Local variable %s)\n", ptr, ptr->param);
  177.         if (ptr->rnode)
  178.             print_parse_tree(ptr->rnode);
  179.         return;
  180.     }
  181.     if (ptr->lnode)
  182.         printf("0x%x = left<--", ptr->lnode);
  183.     printf("(0x%x %s.%d)", ptr, nnames[(int) (ptr->type)], ptr->type);
  184.     if (ptr->rnode)
  185.         printf("-->right = 0x%x", ptr->rnode);
  186.     printf("\n");
  187.     depth++;
  188.     if (ptr->lnode)
  189.         print_parse_tree(ptr->lnode);
  190.     switch ((int) ptr->type) {
  191.     case (int) Node_line_range:
  192.     case (int) Node_match:
  193.     case (int) Node_nomatch:
  194.         break;
  195.     case (int) Node_builtin:
  196.         printf("Builtin: %d\n", ptr->proc);
  197.         break;
  198.     case (int) Node_K_for:
  199.     case (int) Node_K_arrayfor:
  200.         printf("(%s:)\n", nnames[(int) (ptr->type)]);
  201.         print_parse_tree(ptr->forloop->init);
  202.         printf("looping:\n");
  203.         print_parse_tree(ptr->forloop->cond);
  204.         printf("doing:\n");
  205.         print_parse_tree(ptr->forloop->incr);
  206.         break;
  207.     default:
  208.         if (ptr->rnode)
  209.             print_parse_tree(ptr->rnode);
  210.         break;
  211.     }
  212.     --depth;
  213. }
  214.  
  215.  
  216. /*
  217.  * print out all the variables in the world 
  218.  */
  219.  
  220. dump_vars()
  221. {
  222.     register int n;
  223.     register NODE *buc;
  224.  
  225. #ifdef notdef
  226.     printf("Fields:");
  227.     dump_fields();
  228. #endif
  229.     printf("Vars:\n");
  230.     for (n = 0; n < HASHSIZE; n++) {
  231.         for (buc = variables[n]; buc; buc = buc->hnext) {
  232.             printf("'%.*s': ", buc->hlength, buc->hname);
  233.             print_parse_tree(buc->hvalue);
  234.         }
  235.     }
  236.     printf("End\n");
  237. }
  238.  
  239. #ifdef notdef
  240. dump_fields()
  241. {
  242.     register NODE **p;
  243.     register int n;
  244.  
  245.     printf("%d fields\n", f_arr_siz);
  246.     for (n = 0, p = &fields_arr[0]; n < f_arr_siz; n++, p++) {
  247.         printf("$%d is '", n);
  248.         print_simple(*p, stdout);
  249.         printf("'\n");
  250.     }
  251. }
  252. #endif
  253.  
  254. /* VARARGS1 */
  255. print_debug(str, n)
  256. char *str;
  257. {
  258.     extern int debugging;
  259.  
  260.     if (debugging)
  261.         printf("%s:0x%x\n", str, n);
  262. }
  263.  
  264. int indent = 0;
  265.  
  266. print_a_node(ptr)
  267. NODE *ptr;
  268. {
  269.     NODE *p1;
  270.     char *str, *str2;
  271.     int n;
  272.     NODE *buc;
  273.  
  274.     if (!ptr)
  275.         return;        /* don't print null ptrs */
  276.     switch (ptr->type) {
  277.     case Node_val:
  278.         if (ptr->flags&NUM)
  279.             printf("%g", ptr->numbr);
  280.         else
  281.             printf("\"%.*s\"", ptr->stlen, ptr->stptr);
  282.         return;
  283.     case Node_times:
  284.         str = "*";
  285.         goto pr_twoop;
  286.     case Node_quotient:
  287.         str = "/";
  288.         goto pr_twoop;
  289.     case Node_mod:
  290.         str = "%";
  291.         goto pr_twoop;
  292.     case Node_plus:
  293.         str = "+";
  294.         goto pr_twoop;
  295.     case Node_minus:
  296.         str = "-";
  297.         goto pr_twoop;
  298.     case Node_exp:
  299.         str = "^";
  300.         goto pr_twoop;
  301.     case Node_concat:
  302.         str = " ";
  303.         goto pr_twoop;
  304.     case Node_assign:
  305.         str = "=";
  306.         goto pr_twoop;
  307.     case Node_assign_times:
  308.         str = "*=";
  309.         goto pr_twoop;
  310.     case Node_assign_quotient:
  311.         str = "/=";
  312.         goto pr_twoop;
  313.     case Node_assign_mod:
  314.         str = "%=";
  315.         goto pr_twoop;
  316.     case Node_assign_plus:
  317.         str = "+=";
  318.         goto pr_twoop;
  319.     case Node_assign_minus:
  320.         str = "-=";
  321.         goto pr_twoop;
  322.     case Node_assign_exp:
  323.         str = "^=";
  324.         goto pr_twoop;
  325.     case Node_and:
  326.         str = "&&";
  327.         goto pr_twoop;
  328.     case Node_or:
  329.         str = "||";
  330.         goto pr_twoop;
  331.     case Node_equal:
  332.         str = "==";
  333.         goto pr_twoop;
  334.     case Node_notequal:
  335.         str = "!=";
  336.         goto pr_twoop;
  337.     case Node_less:
  338.         str = "<";
  339.         goto pr_twoop;
  340.     case Node_greater:
  341.         str = ">";
  342.         goto pr_twoop;
  343.     case Node_leq:
  344.         str = "<=";
  345.         goto pr_twoop;
  346.     case Node_geq:
  347.         str = ">=";
  348.         goto pr_twoop;
  349.  
  350. pr_twoop:
  351.         print_a_node(ptr->lnode);
  352.         printf("%s", str);
  353.         print_a_node(ptr->rnode);
  354.         return;
  355.  
  356.     case Node_not:
  357.         str = "!";
  358.         str2 = "";
  359.         goto pr_oneop;
  360.     case Node_field_spec:
  361.         str = "$(";
  362.         str2 = ")";
  363.         goto pr_oneop;
  364.     case Node_postincrement:
  365.         str = "";
  366.         str2 = "++";
  367.         goto pr_oneop;
  368.     case Node_postdecrement:
  369.         str = "";
  370.         str2 = "--";
  371.         goto pr_oneop;
  372.     case Node_preincrement:
  373.         str = "++";
  374.         str2 = "";
  375.         goto pr_oneop;
  376.     case Node_predecrement:
  377.         str = "--";
  378.         str2 = "";
  379.         goto pr_oneop;
  380. pr_oneop:
  381.         printf(str);
  382.         print_a_node(ptr->subnode);
  383.         printf(str2);
  384.         return;
  385.  
  386.     case Node_expression_list:
  387.         print_a_node(ptr->lnode);
  388.         if (ptr->rnode) {
  389.             printf(",");
  390.             print_a_node(ptr->rnode);
  391.         }
  392.         return;
  393.  
  394.     case Node_var:
  395.         for (n = 0; n < HASHSIZE; n++) {
  396.             for (buc = variables[n]; buc; buc = buc->hnext) {
  397.                 if (buc->hvalue == ptr) {
  398.                     printf("%.*s", buc->hlength, buc->hname);
  399.                     n = HASHSIZE;
  400.                     break;
  401.                 }
  402.             }
  403.         }
  404.         return;
  405.     case Node_subscript:
  406.         print_a_node(ptr->lnode);
  407.         printf("[");
  408.         print_a_node(ptr->rnode);
  409.         printf("]");
  410.         return;
  411.     case Node_builtin:
  412.         printf("some_builtin(");
  413.         print_a_node(ptr->subnode);
  414.         printf(")");
  415.         return;
  416.  
  417.     case Node_statement_list:
  418.         printf("{\n");
  419.         indent++;
  420.         for (n = indent; n; --n)
  421.             printf("  ");
  422.         while (ptr) {
  423.             print_maybe_semi(ptr->lnode);
  424.             if (ptr->rnode)
  425.                 for (n = indent; n; --n)
  426.                     printf("  ");
  427.             ptr = ptr->rnode;
  428.         }
  429.         --indent;
  430.         for (n = indent; n; --n)
  431.             printf("  ");
  432.         printf("}\n");
  433.         for (n = indent; n; --n)
  434.             printf("  ");
  435.         return;
  436.  
  437.     case Node_K_if:
  438.         printf("if(");
  439.         print_a_node(ptr->lnode);
  440.         printf(") ");
  441.         ptr = ptr->rnode;
  442.         if (ptr->lnode->type == Node_statement_list) {
  443.             printf("{\n");
  444.             indent++;
  445.             for (p1 = ptr->lnode; p1; p1 = p1->rnode) {
  446.                 for (n = indent; n; --n)
  447.                     printf("  ");
  448.                 print_maybe_semi(p1->lnode);
  449.             }
  450.             --indent;
  451.             for (n = indent; n; --n)
  452.                 printf("  ");
  453.             if (ptr->rnode) {
  454.                 printf("} else ");
  455.             } else {
  456.                 printf("}\n");
  457.                 return;
  458.             }
  459.         } else {
  460.             print_maybe_semi(ptr->lnode);
  461.             if (ptr->rnode) {
  462.                 for (n = indent; n; --n)
  463.                     printf("  ");
  464.                 printf("else ");
  465.             } else
  466.                 return;
  467.         }
  468.         if (!ptr->rnode)
  469.             return;
  470.         deal_with_curls(ptr->rnode);
  471.         return;
  472.  
  473.     case Node_K_while:
  474.         printf("while(");
  475.         print_a_node(ptr->lnode);
  476.         printf(") ");
  477.         deal_with_curls(ptr->rnode);
  478.         return;
  479.  
  480.     case Node_K_do:
  481.         printf("do ");
  482.         deal_with_curls(ptr->rnode);
  483.         printf("while(");
  484.         print_a_node(ptr->lnode);
  485.         printf(") ");
  486.         return;
  487.  
  488.     case Node_K_for:
  489.         printf("for(");
  490.         print_a_node(ptr->forloop->init);
  491.         printf(";");
  492.         print_a_node(ptr->forloop->cond);
  493.         printf(";");
  494.         print_a_node(ptr->forloop->incr);
  495.         printf(") ");
  496.         deal_with_curls(ptr->forsub);
  497.         return;
  498.     case Node_K_arrayfor:
  499.         printf("for(");
  500.         print_a_node(ptr->forloop->init);
  501.         printf(" in ");
  502.         print_a_node(ptr->forloop->incr);
  503.         printf(") ");
  504.         deal_with_curls(ptr->forsub);
  505.         return;
  506.  
  507.     case Node_K_printf:
  508.         printf("printf(");
  509.         print_a_node(ptr->lnode);
  510.         printf(")");
  511.         return;
  512.     case Node_K_print:
  513.         printf("print(");
  514.         print_a_node(ptr->lnode);
  515.         printf(")");
  516.         return;
  517.     case Node_K_next:
  518.         printf("next");
  519.         return;
  520.     case Node_K_break:
  521.         printf("break");
  522.         return;
  523.     case Node_K_delete:
  524.         printf("delete ");
  525.         print_a_node(ptr->lnode);
  526.         return;
  527.     case Node_func:
  528.         printf("function %s (", ptr->lnode->param);
  529.         if (ptr->lnode->rnode)
  530.             print_a_node(ptr->lnode->rnode);
  531.         printf(")\n");
  532.         print_a_node(ptr->rnode);
  533.         return;
  534.     case Node_param_list:
  535.         printf("%s", ptr->param);
  536.         if (ptr->rnode) {
  537.             printf(", ");
  538.             print_a_node(ptr->rnode);
  539.         }
  540.         return;
  541.     default:
  542.         print_parse_tree(ptr);
  543.         return;
  544.     }
  545. }
  546.  
  547. print_maybe_semi(ptr)
  548. NODE *ptr;
  549. {
  550.     print_a_node(ptr);
  551.     switch (ptr->type) {
  552.     case Node_K_if:
  553.     case Node_K_for:
  554.     case Node_K_arrayfor:
  555.     case Node_statement_list:
  556.         break;
  557.     default:
  558.         printf(";\n");
  559.         break;
  560.     }
  561. }
  562.  
  563. deal_with_curls(ptr)
  564. NODE *ptr;
  565. {
  566.     int n;
  567.  
  568.     if (ptr->type == Node_statement_list) {
  569.         printf("{\n");
  570.         indent++;
  571.         while (ptr) {
  572.             for (n = indent; n; --n)
  573.                 printf("  ");
  574.             print_maybe_semi(ptr->lnode);
  575.             ptr = ptr->rnode;
  576.         }
  577.         --indent;
  578.         for (n = indent; n; --n)
  579.             printf("  ");
  580.         printf("}\n");
  581.     } else {
  582.         print_maybe_semi(ptr);
  583.     }
  584. }
  585.  
  586. NODE *
  587. do_prvars()
  588. {
  589.     dump_vars();
  590.     return Nnull_string;
  591. }
  592.  
  593. NODE *
  594. do_bp()
  595. {
  596.     return Nnull_string;
  597. }
  598.  
  599. #endif
  600.