Add classes to print status of AST (use only for debugging!)
authorMario de Sousa <msousa@fe.up.pt>
Tue, 16 Oct 2012 18:06:32 +0100
changeset 678 01e33dc2a255
parent 677 740da3255d9d
child 679 2f5618c0039a
Add classes to print status of AST (use only for debugging!)
absyntax_utils/Makefile.am
absyntax_utils/Makefile.in
absyntax_utils/absyntax_utils.hh
absyntax_utils/debug_ast.cc
absyntax_utils/debug_ast.hh
--- a/absyntax_utils/Makefile.am	Tue Oct 16 18:01:22 2012 +0100
+++ b/absyntax_utils/Makefile.am	Tue Oct 16 18:06:32 2012 +0100
@@ -26,4 +26,5 @@
 	search_var_instance_decl.cc \
 	spec_init_separator.cc \
 	type_initial_value.cc \
+	debug_ast.cc \
 	get_datatype_info.cc
--- a/absyntax_utils/Makefile.in	Tue Oct 16 18:01:22 2012 +0100
+++ b/absyntax_utils/Makefile.in	Tue Oct 16 18:06:32 2012 +0100
@@ -94,6 +94,7 @@
 	search_varfb_instance_type.$(OBJEXT) \
 	search_var_instance_decl.$(OBJEXT) \
 	spec_init_separator.$(OBJEXT) type_initial_value.$(OBJEXT) \
+	debug_ast.$(OBJEXT) \
 	get_datatype_info.$(OBJEXT)
 
 libabsyntax_utils_a_OBJECTS = $(am_libabsyntax_utils_a_OBJECTS)
@@ -234,6 +235,7 @@
 	search_var_instance_decl.cc \
 	spec_init_separator.cc \
 	type_initial_value.cc \
+	debug_ast.cc \
 	get_datatype_info.cc
 
 
@@ -317,6 +319,7 @@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/add_en_eno_param_decl.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/array_dimension_iterator.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/case_element_iterator.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/debug_ast.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/decompose_var_instance_name.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/function_call_iterator.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/function_call_param_iterator.Po@am__quote@
--- a/absyntax_utils/absyntax_utils.hh	Tue Oct 16 18:01:22 2012 +0100
+++ b/absyntax_utils/absyntax_utils.hh	Tue Oct 16 18:06:32 2012 +0100
@@ -118,6 +118,7 @@
 #include "search_il_label.hh"
 #include "get_var_name.hh"
 #include "get_datatype_info.hh"
+#include "debug_ast.hh"
 
 /***********************************************************************/
 /***********************************************************************/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/absyntax_utils/debug_ast.cc	Tue Oct 16 18:06:32 2012 +0100
@@ -0,0 +1,176 @@
+/*
+ *  matiec - a compiler for the programming languages defined in IEC 61131-3
+ *  Copyright (C) 2012  Mario de Sousa (msousa@fe.up.pt)
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * This code is made available on the understanding that it will not be
+ * used in safety-critical situations without a full and competent review.
+ */
+
+/*
+ * An IEC 61131-3 compiler.
+ *
+ * Based on the
+ * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
+ *
+ */
+
+
+/*
+ * Some classes to help with debuging.
+ *
+ * These classes will print out the current state of a symbol or a portion of the Abstract Syntax Tree.
+ */
+
+/* TODO: Use a class similar to stage4out_c so that we can have nice indentation when printing an AST
+ *       Create a template so that we can TRACE the execution of other visitor classes doing usefull work!
+ */
+
+
+
+#include <unistd.h>
+#include <stdio.h>  /* required for NULL */
+#include "absyntax_utils.hh"
+
+
+
+
+
+
+
+
+
+
+
+
+print_symbol_c *print_symbol_c::singleton = NULL;
+
+
+void print_symbol_c::print(symbol_c* symbol) {
+  if (NULL == singleton)   singleton = new print_symbol_c();
+  if (NULL == singleton)   ERROR;
+
+  symbol->accept(*singleton);
+}
+
+
+
+
+
+
+
+
+void print_symbol_c::fcall(symbol_c* symbol) {
+  dump_symbol(symbol);
+  fprintf(stderr, "\n");
+}
+
+
+void print_symbol_c::dump_symbol(symbol_c* symbol) {
+  fprintf(stderr, "(%03d:%03d..%03d:%03d) \t%s\t", symbol->first_line, symbol->first_column, symbol->last_line, symbol->last_column, symbol->absyntax_cname());
+
+  fprintf(stderr, "  datatype=");
+  if (NULL == symbol->datatype)
+    fprintf(stderr, "NULL\t\t");
+  else 
+    fprintf(stderr, symbol->datatype->absyntax_cname());
+  fprintf(stderr, "\t<-{");
+  if (symbol->candidate_datatypes.size() == 0) {
+    fprintf(stderr, "\t\t\t\t\t");
+  } else if (symbol->candidate_datatypes.size() <= 2) {
+    for (unsigned int i = 0; i < 2; i++)
+      if (i < symbol->candidate_datatypes.size())
+        fprintf(stderr, " %s,", symbol->candidate_datatypes[i]->absyntax_cname());
+      else
+        fprintf(stderr, "\t\t\t");
+  } else {
+    fprintf(stderr, "(%d)\t\t\t\t\t", symbol->candidate_datatypes.size());
+  }
+  fprintf(stderr, "}\t");          
+}
+
+
+
+void *print_symbol_c::visit(il_instruction_c *symbol) {
+   dump_symbol(symbol);
+
+  fprintf(stderr, "  next_il_=%d ", symbol->next_il_instruction.size());
+  fprintf(stderr, "  prev_il_=%d ", symbol->prev_il_instruction.size());
+  
+  if (symbol->prev_il_instruction.size() == 0)
+    fprintf(stderr, "(----,");
+  else if (symbol->prev_il_instruction[0]->datatype == NULL)
+    fprintf(stderr, "(NULL,");
+  else if (!get_datatype_info_c::is_type_valid(symbol->prev_il_instruction[0]->datatype))
+    fprintf(stderr, "(****,");
+  else
+    fprintf(stderr, "(    ,");
+  
+  if (symbol->next_il_instruction.size() == 0)
+    fprintf(stderr, "----)");
+  else if (symbol->next_il_instruction[0]->datatype == NULL)
+    fprintf(stderr, "NULL)");
+  else if (!get_datatype_info_c::is_type_valid(symbol->next_il_instruction[0]->datatype))
+    fprintf(stderr, "****)");
+  else 
+    fprintf(stderr, "    )");
+  
+  fprintf(stderr, "\n");
+  
+  return NULL;
+};
+
+
+
+
+
+
+
+
+
+
+
+
+
+print_ast_c *print_ast_c::singleton = NULL;
+
+
+void print_ast_c::print(symbol_c* symbol) {
+  if (NULL == singleton)   singleton = new print_ast_c();
+  if (NULL == singleton)   ERROR;
+
+  symbol->accept(*singleton);
+}
+
+  
+void print_ast_c::prefix_fcall(symbol_c* symbol) {print_symbol_c::print(symbol);}
+void print_ast_c::suffix_fcall(symbol_c* symbol) {}
+  
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/absyntax_utils/debug_ast.hh	Tue Oct 16 18:06:32 2012 +0100
@@ -0,0 +1,94 @@
+/*
+ *  matiec - a compiler for the programming languages defined in IEC 61131-3
+ *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
+ *  Copyright (C) 2007-2011  Laurent Bessard and Edouard Tisserant
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * This code is made available on the understanding that it will not be
+ * used in safety-critical situations without a full and competent review.
+ */
+
+/*
+ * An IEC 61131-3 compiler.
+ *
+ * Based on the
+ * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
+ *
+ */
+
+
+/*
+ * Some classes to help with debuging.
+ *
+ * These classes will print out the current state of a symbol or a portion of the Abstract Syntax Tree.
+ */
+
+
+
+
+
+
+#include "../absyntax/visitor.hh"
+
+
+
+class print_symbol_c: public fcall_visitor_c { 
+  public:
+    static void print(symbol_c *symbol);
+    
+  protected:
+    void fcall(symbol_c *symbol);  
+    /* AST symbols with extra data have their own specialised methods for printing that data */
+    void *visit(il_instruction_c *symbol);
+
+  private:
+    static print_symbol_c *singleton;
+    
+    void dump_symbol(symbol_c* symbol);
+};
+
+
+
+
+
+
+class print_ast_c: public fcall_iterator_visitor_c { 
+  public:
+    static void print(symbol_c *symbol);
+    
+  protected:
+  void prefix_fcall(symbol_c *symbol);
+  void suffix_fcall(symbol_c *symbol);  
+  
+  private:
+    static print_ast_c *singleton;    
+};
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+