home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / pascal / qparser.arc / CALCDBUG.PAS < prev    next >
Pascal/Delphi Source File  |  1985-07-26  |  6KB  |  217 lines

  1.   { CALCDBUG:  Skeleton file debugging routines. }
  2.   { Copyright (C) 1984 by QCAD Systems Inc., All Rights Reserved. }
  3.  
  4.   {******************}
  5.   procedure WRSTRING(STX: int);
  6.     { writes a string to the report file, stored at
  7.       stx in the string table. }
  8.   begin
  9.     while strtab[stx]<>chr(eos) do begin
  10.       write(rfile, strtab[stx]);
  11.       stx := stx+1;
  12.     end
  13.   end;
  14.  
  15.   {******************}
  16.   procedure WRSYMBOL(var SYM: symbol);
  17.     { write out a symbol name. }
  18.     var SX: int;
  19.   begin
  20.     sx := 1;
  21.     while (sx <= maxtoklen) and (sym[sx] <> ' ') do begin
  22.       write(rfile, sym[sx]);
  23.       sx := sx+1
  24.     end
  25.   end;
  26.  
  27.   {******************}
  28.   function WRTOK(TX: int): int;
  29.     { writes the print name of the TX'th token, returning
  30.       the number of characters output. }
  31.     var TL: int;
  32.   begin
  33.     tx := tokx[tx];
  34.     tl := 0;
  35.     while tokchar[tx] <> chr(0) do begin
  36.       write(rfile, tokchar[tx]);
  37.       tx := tx+1;
  38.       tl := tl+1
  39.     end;
  40.     wrtok := tl;
  41.   end;
  42.  
  43.   {****************}
  44.   procedure WRPROD(PRX: int);
  45.     { write out the PRX'th production (a series of tokens). }
  46.     var TL: int;
  47.   begin
  48.     prx := prodx[prx];
  49.     tl := wrtok(prods[prx]);
  50.     write(rfile, ' ->');
  51.     prx := prx+1;
  52.     while prods[prx]<>0 do begin
  53.       write(rfile, ' ');
  54.       tl := wrtok(prods[prx]);
  55.       prx := prx+1;
  56.     end
  57.   end;
  58.  
  59.   {******************}
  60.   procedure IDEBUG;  forward;
  61.  
  62.   {******************}
  63.   procedure DUMP_SYM(INDENT: int; SYMP: symtabp);
  64.     { output information on the given symbol table entry.  this can
  65.       be extended to handle user-defined symbol types (e.g. functions
  66.       and variables). }
  67.   begin
  68.     if symp<>nil then
  69.     with symp^ do begin
  70.       write(rfile, ' ':indent);
  71.       wrsymbol(sym);
  72.       write(rfile, ' (', symtypename[symt], ' ', level:1, ' ');
  73.       case symt of
  74.         reserved, symerr: ;
  75.         user:     write(rfile, 'undeclared');
  76.         { add application-specific type cases here }
  77.         {========= added real_variable for calcskel ===============}
  78.         real_variable: write(rfile, rval);
  79.         ELSE  write(rfile, 'other type')
  80.       end;
  81.       write(rfile, ')');
  82.     end
  83.   end;
  84.  
  85.   {*****************}
  86.   procedure DUMP_SEM(INDENT: int; SEMSTK: semrecp);
  87.     { output a semantic stack record. }
  88.   begin
  89.     if semstk<>nil then begin
  90.       with semstk^ do begin
  91.         write(rfile, ' ': indent);
  92.         write(rfile, semtypename[semt], ': ');
  93.         case semt of
  94.           other:  ;
  95.           strng:  wrstring(stx);
  96.           ident:  dump_sym(indent+2, symp);
  97.           fixed:  write(rfile, numval:1);
  98.           float:  write(rfile, rval:10);
  99.           ELSE    write(rfile, ' ... user form')
  100.         end
  101.       end
  102.     end
  103.   end;
  104.  
  105.   {*********************}
  106.   procedure STK_DUMP(KIND: string8;  var STACK: state_stack;
  107.                      STACKX: int;  CSTATE: int);
  108.     { produce a symbolic dump of the parser stack. }
  109.     var SX, TL, LL: int;
  110.   begin
  111.     if debug>2 then begin
  112.       write(rfile, kind {, ', state ', cstate:1} );
  113.       if cstate>=readstate then begin
  114.         write(rfile, ', on token ');
  115.         tl := wrtok(token);
  116.       end;
  117.       writeln(rfile, ', memavail ', memavail:1);
  118.     end;
  119.     if cstate<readstate then begin
  120.       { reduce state }
  121.       if debug>1 then begin  {complete stack dump}
  122.         if stackx>15 then begin
  123.           writeln(rfile, '  ###');
  124.           ll := stackx-15;
  125.         end
  126.         else
  127.         ll := 1;
  128.         for sx := ll to stackx do begin
  129.           write(rfile, ' ' {, stack[sx]:3, ' '} );
  130.           if sx=stackx then
  131.             tl := wrtok(insym[cstate])
  132.           else
  133.           tl := wrtok(insym[stack[sx+1]]);
  134.           write(rfile, ' ':maxtoklen-tl+1);
  135.           dump_sem(0, semstack[sx]);
  136.           writeln(rfile);
  137.         end
  138.       end;
  139.       wrprod(cstate);
  140.       writeln(rfile)
  141.     end;
  142.     { don't let this roll off the top of the screen }
  143.     idebug
  144.   end;
  145.  
  146.   {****************}
  147.   procedure IDEBUG;
  148.     { interactive debugging support }
  149.     var QUIT:  boolean;
  150.  
  151.     {..................}
  152.     procedure SHOW_SYM;
  153.       { asks for a symbol, then dumps the symbol table entry for it }
  154.       var SP:  symtabp;
  155.           STR:  symbol;
  156.           LINE:  string80;
  157.           SX:  int;
  158.     begin
  159.       write('What symbol? ');
  160.       readln(line);
  161.       for sx := 1 to maxtoklen do
  162.         str[sx] := ' ';
  163.       for sx := 1 to length(line) do
  164.         str[sx] := upshift(line[sx]);
  165.       sp := findsym(str);
  166.       if sp<>nil then
  167.         dump_sym(0, sp)
  168.       else
  169.         writeln('Unknown symbol');
  170.       writeln
  171.     end;
  172.  
  173.     {.................}
  174.     procedure DUMP_ALL;
  175.       { show everything in the symbol table }
  176.       var HX: int;
  177.           SP: symtabp;
  178.     begin
  179.       for hx := 0 to hlimit do begin
  180.         sp := symtab[hx];
  181.         while sp<>nil do begin
  182.           with sp^ do begin
  183.             if not (symt in [reserved, symerr]) then begin
  184.               { report only the nontrivial stuff }
  185.               wrsymbol(sym);
  186.               write(rfile, ' ');
  187.             end;
  188.             sp := next
  189.           end
  190.         end
  191.       end;
  192.       writeln(rfile)
  193.     end;
  194.  
  195.     {................}
  196.     procedure SET_DEBUG;
  197.       { prompts for a debug level number }
  198.     begin
  199.       write('Set debug level to (0, 1, ...)? ');
  200.       readln(debug);
  201.     end;
  202.  
  203.   begin { idebug }
  204.     quit := false;
  205.     while not quit do begin
  206.       case upshift(resp(
  207.            'I(dentifier, D(ebug level, A(ll symbols, C(ontinue? ')) of
  208.         'I':  show_sym;
  209.         'A':  dump_all;
  210.         'D':  set_debug;
  211.         'C':  quit := true;
  212.       ELSE ;
  213.       end
  214.     end
  215.   end { idebug };
  216.  
  217.