msousa@678: /*
msousa@678: * matiec - a compiler for the programming languages defined in IEC 61131-3
msousa@678: * Copyright (C) 2012 Mario de Sousa (msousa@fe.up.pt)
msousa@678: *
msousa@678: * This program is free software: you can redistribute it and/or modify
msousa@678: * it under the terms of the GNU General Public License as published by
msousa@678: * the Free Software Foundation, either version 3 of the License, or
msousa@678: * (at your option) any later version.
msousa@678: *
msousa@678: * This program is distributed in the hope that it will be useful,
msousa@678: * but WITHOUT ANY WARRANTY; without even the implied warranty of
msousa@678: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
msousa@678: * GNU General Public License for more details.
msousa@678: *
msousa@678: * You should have received a copy of the GNU General Public License
msousa@678: * along with this program. If not, see .
msousa@678: *
msousa@678: *
msousa@678: * This code is made available on the understanding that it will not be
msousa@678: * used in safety-critical situations without a full and competent review.
msousa@678: */
msousa@678:
msousa@678: /*
msousa@678: * An IEC 61131-3 compiler.
msousa@678: *
msousa@678: * Based on the
msousa@678: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
msousa@678: *
msousa@678: */
msousa@678:
msousa@678:
msousa@678: /*
msousa@678: * Some classes to help with debuging.
msousa@678: *
msousa@678: * These classes will print out the current state of a symbol or a portion of the Abstract Syntax Tree.
msousa@678: */
msousa@678:
msousa@678: /* TODO: Use a class similar to stage4out_c so that we can have nice indentation when printing an AST
msousa@678: * Create a template so that we can TRACE the execution of other visitor classes doing usefull work!
msousa@678: */
msousa@678:
msousa@678:
msousa@678:
msousa@678: #include
msousa@678: #include /* required for NULL */
msousa@678: #include "absyntax_utils.hh"
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678: print_symbol_c *print_symbol_c::singleton = NULL;
msousa@678:
msousa@678:
msousa@678: void print_symbol_c::print(symbol_c* symbol) {
msousa@678: if (NULL == singleton) singleton = new print_symbol_c();
msousa@678: if (NULL == singleton) ERROR;
msousa@678:
msousa@678: symbol->accept(*singleton);
msousa@678: }
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678: void print_symbol_c::fcall(symbol_c* symbol) {
msousa@678: dump_symbol(symbol);
msousa@678: fprintf(stderr, "\n");
msousa@678: }
msousa@678:
msousa@678:
msousa@678: void print_symbol_c::dump_symbol(symbol_c* symbol) {
msousa@678: fprintf(stderr, "(%03d:%03d..%03d:%03d) \t%s\t", symbol->first_line, symbol->first_column, symbol->last_line, symbol->last_column, symbol->absyntax_cname());
msousa@678:
msousa@678: fprintf(stderr, " datatype=");
msousa@678: if (NULL == symbol->datatype)
msousa@678: fprintf(stderr, "NULL\t\t");
msousa@678: else
msousa@678: fprintf(stderr, symbol->datatype->absyntax_cname());
msousa@678: fprintf(stderr, "\t<-{");
msousa@678: if (symbol->candidate_datatypes.size() == 0) {
msousa@678: fprintf(stderr, "\t\t\t\t\t");
msousa@678: } else if (symbol->candidate_datatypes.size() <= 2) {
msousa@678: for (unsigned int i = 0; i < 2; i++)
msousa@678: if (i < symbol->candidate_datatypes.size())
msousa@678: fprintf(stderr, " %s,", symbol->candidate_datatypes[i]->absyntax_cname());
msousa@678: else
msousa@678: fprintf(stderr, "\t\t\t");
msousa@678: } else {
msousa@678: fprintf(stderr, "(%d)\t\t\t\t\t", symbol->candidate_datatypes.size());
msousa@678: }
msousa@678: fprintf(stderr, "}\t");
msousa@678: }
msousa@678:
msousa@678:
msousa@678:
msousa@678: void *print_symbol_c::visit(il_instruction_c *symbol) {
msousa@678: dump_symbol(symbol);
msousa@678:
msousa@678: fprintf(stderr, " next_il_=%d ", symbol->next_il_instruction.size());
msousa@678: fprintf(stderr, " prev_il_=%d ", symbol->prev_il_instruction.size());
msousa@678:
msousa@678: if (symbol->prev_il_instruction.size() == 0)
msousa@678: fprintf(stderr, "(----,");
msousa@678: else if (symbol->prev_il_instruction[0]->datatype == NULL)
msousa@678: fprintf(stderr, "(NULL,");
msousa@678: else if (!get_datatype_info_c::is_type_valid(symbol->prev_il_instruction[0]->datatype))
msousa@678: fprintf(stderr, "(****,");
msousa@678: else
msousa@678: fprintf(stderr, "( ,");
msousa@678:
msousa@678: if (symbol->next_il_instruction.size() == 0)
msousa@678: fprintf(stderr, "----)");
msousa@678: else if (symbol->next_il_instruction[0]->datatype == NULL)
msousa@678: fprintf(stderr, "NULL)");
msousa@678: else if (!get_datatype_info_c::is_type_valid(symbol->next_il_instruction[0]->datatype))
msousa@678: fprintf(stderr, "****)");
msousa@678: else
msousa@678: fprintf(stderr, " )");
msousa@678:
msousa@678: fprintf(stderr, "\n");
msousa@678:
msousa@678: return NULL;
msousa@678: };
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678: print_ast_c *print_ast_c::singleton = NULL;
msousa@678:
msousa@678:
msousa@678: void print_ast_c::print(symbol_c* symbol) {
msousa@678: if (NULL == singleton) singleton = new print_ast_c();
msousa@678: if (NULL == singleton) ERROR;
msousa@678:
msousa@678: symbol->accept(*singleton);
msousa@678: }
msousa@678:
msousa@678:
msousa@678: void print_ast_c::prefix_fcall(symbol_c* symbol) {print_symbol_c::print(symbol);}
msousa@678: void print_ast_c::suffix_fcall(symbol_c* symbol) {}
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678:
msousa@678: