absyntax_utils/debug_ast.cc
changeset 678 01e33dc2a255
child 725 bfbe4aca6b77
equal deleted inserted replaced
677:740da3255d9d 678:01e33dc2a255
       
     1 /*
       
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
       
     3  *  Copyright (C) 2012  Mario de Sousa (msousa@fe.up.pt)
       
     4  *
       
     5  *  This program is free software: you can redistribute it and/or modify
       
     6  *  it under the terms of the GNU General Public License as published by
       
     7  *  the Free Software Foundation, either version 3 of the License, or
       
     8  *  (at your option) any later version.
       
     9  *
       
    10  *  This program is distributed in the hope that it will be useful,
       
    11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  *  GNU General Public License for more details.
       
    14  *
       
    15  *  You should have received a copy of the GNU General Public License
       
    16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    17  *
       
    18  *
       
    19  * This code is made available on the understanding that it will not be
       
    20  * used in safety-critical situations without a full and competent review.
       
    21  */
       
    22 
       
    23 /*
       
    24  * An IEC 61131-3 compiler.
       
    25  *
       
    26  * Based on the
       
    27  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    28  *
       
    29  */
       
    30 
       
    31 
       
    32 /*
       
    33  * Some classes to help with debuging.
       
    34  *
       
    35  * These classes will print out the current state of a symbol or a portion of the Abstract Syntax Tree.
       
    36  */
       
    37 
       
    38 /* TODO: Use a class similar to stage4out_c so that we can have nice indentation when printing an AST
       
    39  *       Create a template so that we can TRACE the execution of other visitor classes doing usefull work!
       
    40  */
       
    41 
       
    42 
       
    43 
       
    44 #include <unistd.h>
       
    45 #include <stdio.h>  /* required for NULL */
       
    46 #include "absyntax_utils.hh"
       
    47 
       
    48 
       
    49 
       
    50 
       
    51 
       
    52 
       
    53 
       
    54 
       
    55 
       
    56 
       
    57 
       
    58 
       
    59 print_symbol_c *print_symbol_c::singleton = NULL;
       
    60 
       
    61 
       
    62 void print_symbol_c::print(symbol_c* symbol) {
       
    63   if (NULL == singleton)   singleton = new print_symbol_c();
       
    64   if (NULL == singleton)   ERROR;
       
    65 
       
    66   symbol->accept(*singleton);
       
    67 }
       
    68 
       
    69 
       
    70 
       
    71 
       
    72 
       
    73 
       
    74 
       
    75 
       
    76 void print_symbol_c::fcall(symbol_c* symbol) {
       
    77   dump_symbol(symbol);
       
    78   fprintf(stderr, "\n");
       
    79 }
       
    80 
       
    81 
       
    82 void print_symbol_c::dump_symbol(symbol_c* symbol) {
       
    83   fprintf(stderr, "(%03d:%03d..%03d:%03d) \t%s\t", symbol->first_line, symbol->first_column, symbol->last_line, symbol->last_column, symbol->absyntax_cname());
       
    84 
       
    85   fprintf(stderr, "  datatype=");
       
    86   if (NULL == symbol->datatype)
       
    87     fprintf(stderr, "NULL\t\t");
       
    88   else 
       
    89     fprintf(stderr, symbol->datatype->absyntax_cname());
       
    90   fprintf(stderr, "\t<-{");
       
    91   if (symbol->candidate_datatypes.size() == 0) {
       
    92     fprintf(stderr, "\t\t\t\t\t");
       
    93   } else if (symbol->candidate_datatypes.size() <= 2) {
       
    94     for (unsigned int i = 0; i < 2; i++)
       
    95       if (i < symbol->candidate_datatypes.size())
       
    96         fprintf(stderr, " %s,", symbol->candidate_datatypes[i]->absyntax_cname());
       
    97       else
       
    98         fprintf(stderr, "\t\t\t");
       
    99   } else {
       
   100     fprintf(stderr, "(%d)\t\t\t\t\t", symbol->candidate_datatypes.size());
       
   101   }
       
   102   fprintf(stderr, "}\t");          
       
   103 }
       
   104 
       
   105 
       
   106 
       
   107 void *print_symbol_c::visit(il_instruction_c *symbol) {
       
   108    dump_symbol(symbol);
       
   109 
       
   110   fprintf(stderr, "  next_il_=%d ", symbol->next_il_instruction.size());
       
   111   fprintf(stderr, "  prev_il_=%d ", symbol->prev_il_instruction.size());
       
   112   
       
   113   if (symbol->prev_il_instruction.size() == 0)
       
   114     fprintf(stderr, "(----,");
       
   115   else if (symbol->prev_il_instruction[0]->datatype == NULL)
       
   116     fprintf(stderr, "(NULL,");
       
   117   else if (!get_datatype_info_c::is_type_valid(symbol->prev_il_instruction[0]->datatype))
       
   118     fprintf(stderr, "(****,");
       
   119   else
       
   120     fprintf(stderr, "(    ,");
       
   121   
       
   122   if (symbol->next_il_instruction.size() == 0)
       
   123     fprintf(stderr, "----)");
       
   124   else if (symbol->next_il_instruction[0]->datatype == NULL)
       
   125     fprintf(stderr, "NULL)");
       
   126   else if (!get_datatype_info_c::is_type_valid(symbol->next_il_instruction[0]->datatype))
       
   127     fprintf(stderr, "****)");
       
   128   else 
       
   129     fprintf(stderr, "    )");
       
   130   
       
   131   fprintf(stderr, "\n");
       
   132   
       
   133   return NULL;
       
   134 };
       
   135 
       
   136 
       
   137 
       
   138 
       
   139 
       
   140 
       
   141 
       
   142 
       
   143 
       
   144 
       
   145 
       
   146 
       
   147 
       
   148 print_ast_c *print_ast_c::singleton = NULL;
       
   149 
       
   150 
       
   151 void print_ast_c::print(symbol_c* symbol) {
       
   152   if (NULL == singleton)   singleton = new print_ast_c();
       
   153   if (NULL == singleton)   ERROR;
       
   154 
       
   155   symbol->accept(*singleton);
       
   156 }
       
   157 
       
   158   
       
   159 void print_ast_c::prefix_fcall(symbol_c* symbol) {print_symbol_c::print(symbol);}
       
   160 void print_ast_c::suffix_fcall(symbol_c* symbol) {}
       
   161   
       
   162 
       
   163 
       
   164 
       
   165 
       
   166 
       
   167 
       
   168 
       
   169 
       
   170 
       
   171 
       
   172 
       
   173 
       
   174 
       
   175 
       
   176