etisserant@0: /* etisserant@0: * (c) 2003 Mario de Sousa etisserant@0: * etisserant@0: * Offered to the public under the terms of the GNU General Public License etisserant@0: * as published by the Free Software Foundation; either version 2 of the etisserant@0: * License, or (at your option) any later version. etisserant@0: * etisserant@0: * This program is distributed in the hope that it will be useful, but etisserant@0: * WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General etisserant@0: * Public License for more details. etisserant@0: * etisserant@0: * This code is made available on the understanding that it will not be etisserant@0: * used in safety-critical situations without a full and competent review. etisserant@0: */ etisserant@0: etisserant@0: /* etisserant@0: * An IEC 61131-3 IL and ST compiler. etisserant@0: * etisserant@0: * Based on the etisserant@0: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) etisserant@0: * etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: /* etisserant@0: * Conversion of st statements (i.e. ST code). etisserant@0: * etisserant@0: * This is part of the 4th stage that generates etisserant@0: * a c++ source program equivalent to the IL and ST etisserant@0: * code. etisserant@0: */ etisserant@0: etisserant@0: lbessard@16: /***********************************************************************/ lbessard@16: /***********************************************************************/ lbessard@16: /***********************************************************************/ lbessard@16: /***********************************************************************/ etisserant@0: etisserant@0: etisserant@0: class generate_cc_st_c: public generate_cc_typedecl_c { etisserant@0: etisserant@0: private: etisserant@0: /* When calling a function block, we must first find it's type, etisserant@0: * by searching through the declarations of the variables currently etisserant@0: * in scope. etisserant@0: * This class does just that... etisserant@0: * A new class is instantiated whenever we begin generating the code etisserant@0: * for a function block type declaration, or a program declaration. etisserant@0: * This object instance will then later be called while the etisserant@0: * function block's or the program's body is being handled. etisserant@0: * etisserant@0: * Note that functions cannot contain calls to function blocks, etisserant@0: * so we do not create an object instance when handling etisserant@0: * a function declaration. etisserant@0: */ etisserant@0: search_fb_instance_decl_c *search_fb_instance_decl; etisserant@0: lbessard@16: /* When compiling st code, it becomes necessary to determine the lbessard@16: * data type of st expressions. To do this, we must first find the lbessard@16: * st operand's declaration, within the scope of the function block lbessard@16: * or function currently being processed. lbessard@16: * The following object does just that... lbessard@16: * This object instance will then later be called while the lbessard@16: * remaining st code is being handled. lbessard@16: */ lbessard@16: search_expression_type_c *search_expression_type; etisserant@0: etisserant@0: public: etisserant@0: generate_cc_st_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL) etisserant@0: : generate_cc_typedecl_c(s4o_ptr) { etisserant@0: search_fb_instance_decl = new search_fb_instance_decl_c(scope); lbessard@16: search_expression_type = new search_expression_type_c(scope); etisserant@0: this->set_variable_prefix(variable_prefix); etisserant@0: } etisserant@0: etisserant@0: virtual ~generate_cc_st_c(void) { etisserant@0: delete search_fb_instance_decl; lbessard@16: delete search_expression_type; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: private: etisserant@0: /* Some function calls in the body of functions or function blocks etisserant@0: * may leave some parameters to their default values, and etisserant@0: * ignore some output parameters of the function being called. etisserant@0: * Our conversion of ST functions to C++ does not contemplate that, etisserant@0: * i.e. each called function must get all it's input and output etisserant@0: * parameters set correctly. etisserant@0: * For input parameters we merely need to call the function with etisserant@0: * the apropriate default value, but for output parameters etisserant@0: * we must create temporary variables to hold the output value. etisserant@0: * etisserant@0: * We declare all the temporary output variables at the begining of etisserant@0: * the body of each function or function block, and use them as etisserant@0: * in function calls later on as they become necessary... etisserant@0: * Note that we cannot create these variables just before a function etisserant@0: * call, as the function call itself may be integrated within an etisserant@0: * expression, or another function call! etisserant@0: * etisserant@0: * The variables are declared in the exact same order in which they etisserant@0: * will be used later on during the function calls, which allows us etisserant@0: * to simply re-create the name that was used for the temporary variable etisserant@0: * instead of keeping it in some list. etisserant@0: * The names are recreated by the temp_var_name_factory, after reset() etisserant@0: * has been called! etisserant@0: * etisserant@0: * This function will genertae code similar to... etisserant@0: * etisserant@0: * INT __TMP_0 = 23; etisserant@0: * REAL __TMP_1 = 45.5; etisserant@0: * ... etisserant@0: */ etisserant@0: temp_var_name_c temp_var_name_factory; etisserant@0: etisserant@0: public: etisserant@0: void generate(statement_list_c *stl) { etisserant@0: generate_cc_tempvardecl_c generate_cc_tempvardecl(&s4o); etisserant@0: generate_cc_tempvardecl.generate(stl, &temp_var_name_factory); etisserant@0: stl->accept(*this); etisserant@0: } etisserant@0: etisserant@0: private: lbessard@16: etisserant@0: /***************************************/ etisserant@0: /* B.3 - Language ST (Structured Text) */ etisserant@0: /***************************************/ etisserant@0: /***********************/ etisserant@0: /* B 3.1 - Expressions */ etisserant@0: /***********************/ lbessard@16: void *visit(or_expression_c *symbol) { lbessard@16: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@16: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@16: if (typeid(*left_type) == typeid(bool_type_name_c) && typeid(*right_type) == typeid(bool_type_name_c)) { lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " || "); lbessard@16: } lbessard@16: if (search_expression_type->is_numeric_compatible(left_type) && search_expression_type->is_numeric_compatible(right_type)) { lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " | "); lbessard@16: } lbessard@16: ERROR; lbessard@16: return NULL; lbessard@16: } lbessard@16: lbessard@16: void *visit(xor_expression_c *symbol) { lbessard@16: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@16: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@16: if (typeid(*left_type) == typeid(bool_type_name_c) && typeid(*right_type) == typeid(bool_type_name_c)) { lbessard@16: s4o.print("("); lbessard@16: symbol->l_exp->accept(*this); lbessard@16: s4o.print(" && !"); lbessard@16: symbol->r_exp->accept(*this); lbessard@16: s4o.print(") || (!"); lbessard@16: symbol->l_exp->accept(*this); lbessard@16: s4o.print(" && "); lbessard@16: symbol->r_exp->accept(*this); lbessard@16: s4o.print(")"); lbessard@16: } lbessard@16: if (search_expression_type->is_numeric_compatible(left_type) && search_expression_type->is_numeric_compatible(right_type)) { lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " ^ "); lbessard@16: } lbessard@16: ERROR; lbessard@16: return NULL; lbessard@16: } lbessard@16: lbessard@16: void *visit(and_expression_c *symbol) { lbessard@16: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@16: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@16: if (typeid(*left_type) == typeid(bool_type_name_c) && typeid(*right_type) == typeid(bool_type_name_c)) { lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " && "); lbessard@16: } lbessard@16: if (search_expression_type->is_numeric_compatible(left_type) && search_expression_type->is_numeric_compatible(right_type)) { lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " & "); lbessard@16: } lbessard@16: ERROR; lbessard@16: return NULL; lbessard@16: } lbessard@16: lbessard@16: void *visit(equ_expression_c *symbol) { lbessard@16: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@16: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@16: if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) { lbessard@16: return print_compare_function("__compare_timespec", "==", symbol->l_exp, symbol->r_exp); lbessard@16: } lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " == "); lbessard@16: } lbessard@16: lbessard@16: void *visit(notequ_expression_c *symbol) { lbessard@16: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@16: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@16: if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) { lbessard@16: return print_compare_function("__compare_timespec", "!=", symbol->l_exp, symbol->r_exp); lbessard@16: } lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " != "); lbessard@16: } lbessard@16: lbessard@16: void *visit(lt_expression_c *symbol) { lbessard@16: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@16: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@16: if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) { lbessard@16: return print_compare_function("__compare_timespec", "<", symbol->l_exp, symbol->r_exp); lbessard@16: } lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " < "); lbessard@16: } lbessard@16: lbessard@16: void *visit(gt_expression_c *symbol) { lbessard@16: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@16: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@16: if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) { lbessard@16: return print_compare_function("__compare_timespec", ">", symbol->l_exp, symbol->r_exp); lbessard@16: } lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " > "); lbessard@16: } lbessard@16: lbessard@16: void *visit(le_expression_c *symbol) { lbessard@16: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@16: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@16: if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) { lbessard@16: return print_compare_function("__compare_timespec", "<=", symbol->l_exp, symbol->r_exp); lbessard@16: } lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= "); lbessard@16: } lbessard@16: lbessard@16: void *visit(ge_expression_c *symbol) { lbessard@16: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@16: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@16: if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) { lbessard@16: return print_compare_function("__compare_timespec", ">=", symbol->l_exp, symbol->r_exp); lbessard@16: } lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= "); lbessard@16: } lbessard@16: lbessard@16: void *visit(add_expression_c *symbol) { lbessard@16: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@16: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@16: if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) { lbessard@16: return print_binary_function("__add_timespec", symbol->l_exp, symbol->r_exp); lbessard@16: } lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " + "); lbessard@16: } lbessard@16: lbessard@16: void *visit(sub_expression_c *symbol) { lbessard@16: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@16: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@16: if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) { lbessard@16: return print_binary_function("__sub_timespec", symbol->l_exp, symbol->r_exp); lbessard@16: } lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " - "); lbessard@16: } lbessard@16: lbessard@16: void *visit(mul_expression_c *symbol) { lbessard@16: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@16: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@16: if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) { lbessard@16: return print_binary_function("__mul_timespec", symbol->l_exp, symbol->r_exp); lbessard@16: } lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " * "); lbessard@16: } lbessard@16: etisserant@0: void *visit(div_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " / ");} etisserant@0: void *visit(mod_expression_c *symbol) { etisserant@0: s4o.print("(("); etisserant@0: symbol->r_exp->accept(*this); etisserant@0: s4o.print(" == 0)?0:"); etisserant@0: print_binary_expression(symbol->l_exp, symbol->r_exp, " % "); etisserant@0: s4o.print(")"); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /* TODO: power expression... */ etisserant@0: void *visit(power_expression_c *symbol) {ERROR; return print_binary_expression(symbol->l_exp, symbol->r_exp, " ** ");} etisserant@0: void *visit(neg_expression_c *symbol) {return print_unary_expression(symbol->exp, " -");} etisserant@0: void *visit(not_expression_c *symbol) {return print_unary_expression(symbol->exp, "!");} etisserant@0: etisserant@0: void *visit(function_invocation_c *symbol) { etisserant@0: function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); etisserant@0: etisserant@0: if (f_decl == function_symtable.end_value()) etisserant@0: /* should never occur. The function being called MUST be in the symtable... */ etisserant@0: ERROR; etisserant@0: etisserant@0: symbol->function_name->accept(*this); etisserant@0: s4o.print("("); etisserant@0: etisserant@0: /* loop through each function parameter, find the value we should pass etisserant@0: * to it, and then output the c equivalent... etisserant@0: */ etisserant@0: etisserant@0: function_param_iterator_c fp_iterator(f_decl); etisserant@0: identifier_c *param_name; etisserant@0: function_call_param_iterator_c function_call_param_iterator(symbol); etisserant@0: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { etisserant@0: if (i != 1) etisserant@0: s4o.print(", "); etisserant@0: etisserant@0: symbol_c *param_type = fp_iterator.param_type(); etisserant@0: if (param_type == NULL) ERROR; etisserant@0: etisserant@0: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); etisserant@0: etisserant@0: /* Get the value from a foo( = ) style call */ etisserant@0: symbol_c *param_value = function_call_param_iterator.search(param_name); etisserant@0: etisserant@0: /* Get the value from a foo() style call */ etisserant@0: if (param_value == NULL) etisserant@0: param_value = function_call_param_iterator.next(); etisserant@0: etisserant@0: switch (param_direction) { etisserant@0: case function_param_iterator_c::direction_in: etisserant@0: if (param_value == NULL) { etisserant@0: /* No value given for parameter, so we must use the default... */ etisserant@0: /* First check whether default value specified in function declaration...*/ etisserant@0: param_value = fp_iterator.default_value(); etisserant@0: } etisserant@0: if (param_value == NULL) { etisserant@0: /* If not, get the default value of this variable's type */ etisserant@0: param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); etisserant@0: } etisserant@0: if (param_value == NULL) ERROR; etisserant@0: param_value->accept(*this); etisserant@0: break; etisserant@0: case function_param_iterator_c::direction_out: etisserant@0: case function_param_iterator_c::direction_inout: etisserant@0: if (param_value == NULL) { etisserant@0: /* no parameter value given, so we pass a previously declared temporary variable. */ etisserant@0: std::string *temp_var_name = temp_var_name_factory.new_name(); etisserant@0: s4o.print(*temp_var_name); etisserant@0: delete temp_var_name; etisserant@0: } else { etisserant@0: param_value->accept(*this); etisserant@0: } etisserant@0: break; etisserant@0: case function_param_iterator_c::direction_extref: etisserant@0: /* TODO! */ etisserant@0: ERROR; etisserant@0: break; etisserant@0: } /* switch */ etisserant@0: } /* for(...) */ etisserant@0: etisserant@0: // symbol->parameter_assignment->accept(*this); etisserant@0: s4o.print(")"); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /********************/ etisserant@0: /* B 3.2 Statements */ etisserant@0: /********************/ etisserant@0: void *visit(statement_list_c *symbol) { etisserant@0: return print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); etisserant@0: } etisserant@0: etisserant@0: /*********************************/ etisserant@0: /* B 3.2.1 Assignment Statements */ etisserant@0: /*********************************/ etisserant@0: void *visit(assignment_statement_c *symbol) { etisserant@0: symbol->l_exp->accept(*this); etisserant@0: s4o.print(" = "); etisserant@0: symbol->r_exp->accept(*this); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /*****************************************/ etisserant@0: /* B 3.2.2 Subprogram Control Statements */ etisserant@0: /*****************************************/ etisserant@0: etisserant@0: /* fb_name '(' [param_assignment_list] ')' */ etisserant@0: /* param_assignment_list -> may be NULL ! */ etisserant@0: //SYM_REF2(fb_invocation_c, fb_name, param_assignment_list) etisserant@0: void *visit(fb_invocation_c *symbol) { etisserant@0: TRACE("fb_invocation_c"); etisserant@0: /* first figure out what is the name of the function block type of the function block being called... */ etisserant@0: symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name); etisserant@0: /* should never occur. The function block instance MUST have been declared... */ etisserant@0: if (function_block_type_name == NULL) ERROR; etisserant@0: etisserant@0: /* Now find the declaration of the function block type being called... */ etisserant@0: function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name); etisserant@0: /* should never occur. The function block type being called MUST be in the symtable... */ etisserant@0: if (fb_decl == function_block_type_symtable.end_value()) ERROR; etisserant@0: etisserant@0: /* loop through each function block parameter, find the value we should pass etisserant@0: * to it, and then output the c equivalent... etisserant@0: */ etisserant@0: function_param_iterator_c fp_iterator(fb_decl); etisserant@0: identifier_c *param_name; etisserant@0: function_call_param_iterator_c function_call_param_iterator(symbol); etisserant@0: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { etisserant@0: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); etisserant@0: etisserant@0: /* Get the value from a foo( = ) style call */ etisserant@0: symbol_c *param_value = function_call_param_iterator.search(param_name); etisserant@0: etisserant@0: /* Get the value from a foo() style call */ etisserant@0: if (param_value == NULL) etisserant@0: param_value = function_call_param_iterator.next(); etisserant@0: etisserant@0: /* now output the value assignment */ etisserant@0: if (param_value != NULL) etisserant@0: if ((param_direction == function_param_iterator_c::direction_in) || etisserant@0: (param_direction == function_param_iterator_c::direction_inout)) { etisserant@0: symbol->fb_name->accept(*this); etisserant@0: s4o.print("."); etisserant@0: param_name->accept(*this); etisserant@0: s4o.print(" = "); etisserant@0: param_value->accept(*this); etisserant@0: s4o.print(";\n" + s4o.indent_spaces); etisserant@0: } etisserant@0: } /* for(...) */ etisserant@0: etisserant@0: /* now call the function... */ etisserant@0: function_block_type_name->accept(*this); etisserant@0: s4o.print(FB_FUNCTION_SUFFIX); etisserant@0: s4o.print("(&"); etisserant@0: symbol->fb_name->accept(*this); etisserant@0: s4o.print(")"); etisserant@0: etisserant@0: /* loop through each function parameter, find the variable to which etisserant@0: * we should atribute the value of all output or inoutput parameters. etisserant@0: */ etisserant@0: fp_iterator.reset(); etisserant@0: function_call_param_iterator.reset(); etisserant@0: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { etisserant@0: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); etisserant@0: etisserant@0: /* Get the value from a foo( = ) style call */ etisserant@0: symbol_c *param_value = function_call_param_iterator.search(param_name); etisserant@0: etisserant@0: /* Get the value from a foo() style call */ etisserant@0: if (param_value == NULL) etisserant@0: param_value = function_call_param_iterator.next(); etisserant@0: etisserant@0: /* now output the value assignment */ etisserant@0: if (param_value != NULL) etisserant@0: if ((param_direction == function_param_iterator_c::direction_out) || etisserant@0: (param_direction == function_param_iterator_c::direction_inout)) { etisserant@0: s4o.print(";\n"+ s4o.indent_spaces); etisserant@0: param_value->accept(*this); etisserant@0: s4o.print(" = "); etisserant@0: symbol->fb_name->accept(*this); etisserant@0: s4o.print("."); etisserant@0: param_name->accept(*this); etisserant@0: } etisserant@0: } /* for(...) */ etisserant@0: etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* helper symbol for fb_invocation */ etisserant@0: /* param_assignment_list ',' param_assignment */ etisserant@0: void *visit(param_assignment_list_c *symbol) { etisserant@0: TRACE("param_assignment_list_c"); etisserant@0: /* this should never be called... */ etisserant@0: ERROR; etisserant@0: return NULL; etisserant@0: // return print_list(symbol, "", ", "); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: void *visit(input_variable_param_assignment_c *symbol) { etisserant@0: TRACE("input_variable_param_assignment_c"); etisserant@0: /* this should never be called... */ etisserant@0: ERROR; etisserant@0: return NULL; etisserant@0: /* etisserant@0: symbol->variable_name->accept(*this); etisserant@0: s4o.print(" = "); etisserant@0: symbol->expression->accept(*this); etisserant@0: return NULL; etisserant@0: */ etisserant@0: } etisserant@0: etisserant@0: void *visit(output_variable_param_assignment_c *symbol) { etisserant@0: TRACE("output_variable_param_assignment_c"); etisserant@0: /* this should never be called... */ etisserant@0: ERROR; etisserant@0: return NULL; etisserant@0: /* etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: if (symbol->not_param != NULL) etisserant@0: symbol->not_param->accept(*this); etisserant@0: symbol->variable_name->accept(*this); etisserant@0: s4o.print(" => "); etisserant@0: symbol->variable->accept(*this); etisserant@0: return NULL; etisserant@0: */ etisserant@0: } etisserant@0: etisserant@0: // TODO: the NOT symbol in function (block) calls... etisserant@0: void *visit(not_paramassign_c *symbol) { etisserant@0: TRACE("not_paramassign_c"); etisserant@0: /* this should never be called... */ etisserant@0: ERROR; etisserant@0: return NULL; etisserant@0: /* etisserant@0: s4o.print("NOT "); etisserant@0: return NULL; etisserant@0: */ etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /********************************/ etisserant@0: /* B 3.2.3 Selection Statements */ etisserant@0: /********************************/ etisserant@0: void *visit(if_statement_c *symbol) { etisserant@0: s4o.print("if ("); etisserant@0: symbol->expression->accept(*this); etisserant@0: s4o.print(") {\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->statement_list->accept(*this); etisserant@0: s4o.indent_left(); etisserant@0: symbol->elseif_statement_list->accept(*this); etisserant@0: etisserant@0: if (symbol->else_statement_list != NULL) { etisserant@0: s4o.print(s4o.indent_spaces); s4o.print("} else {\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->else_statement_list->accept(*this); etisserant@0: s4o.indent_left(); etisserant@0: } etisserant@0: s4o.print(s4o.indent_spaces); s4o.print("}"); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /* helper symbol for if_statement */ etisserant@0: void *visit(elseif_statement_list_c *symbol) {return print_list(symbol);} etisserant@0: etisserant@0: /* helper symbol for elseif_statement_list */ etisserant@0: void *visit(elseif_statement_c *symbol) { etisserant@0: s4o.print(s4o.indent_spaces); s4o.print("} else if ("); etisserant@0: symbol->expression->accept(*this); etisserant@0: s4o.print(") {\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->statement_list->accept(*this); etisserant@0: s4o.indent_left(); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(case_statement_c *symbol) { etisserant@0: s4o.print("switch("); etisserant@0: symbol->expression->accept(*this); etisserant@0: s4o.print(") {\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->case_element_list->accept(*this); etisserant@0: if (symbol->statement_list != NULL) { etisserant@0: s4o.print(s4o.indent_spaces + "default:\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->statement_list->accept(*this); etisserant@0: s4o.print(s4o.indent_spaces + "break;\n"); etisserant@0: s4o.indent_left(); etisserant@0: } etisserant@0: s4o.indent_left(); etisserant@0: s4o.print(s4o.indent_spaces + "}"); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /* helper symbol for case_statement */ etisserant@0: void *visit(case_element_list_c *symbol) {return print_list(symbol);} etisserant@0: etisserant@0: void *visit(case_element_c *symbol) { etisserant@0: s4o.print(s4o.indent_spaces + "case "); etisserant@0: symbol->case_list->accept(*this); etisserant@0: s4o.print(" :\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->statement_list->accept(*this); etisserant@0: s4o.print(s4o.indent_spaces + "break;\n"); etisserant@0: s4o.indent_left(); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(case_list_c *symbol) {return print_list(symbol, "", ", ");} etisserant@0: etisserant@0: /********************************/ etisserant@0: /* B 3.2.4 Iteration Statements */ etisserant@0: /********************************/ etisserant@0: void *visit(for_statement_c *symbol) { etisserant@0: s4o.print("for("); etisserant@0: symbol->control_variable->accept(*this); etisserant@0: s4o.print(" = "); etisserant@0: symbol->beg_expression->accept(*this); etisserant@0: s4o.print("; "); etisserant@0: symbol->control_variable->accept(*this); etisserant@0: s4o.print(" != "); etisserant@0: symbol->end_expression->accept(*this); etisserant@0: s4o.print("; "); etisserant@0: symbol->control_variable->accept(*this); etisserant@0: if (symbol->by_expression != NULL) { etisserant@0: s4o.print(" += "); etisserant@0: symbol->by_expression->accept(*this); etisserant@0: } else { etisserant@0: s4o.print("++"); etisserant@0: } etisserant@0: s4o.print(") {\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->statement_list->accept(*this); etisserant@0: s4o.indent_left(); etisserant@0: s4o.print(s4o.indent_spaces); s4o.print("}"); etisserant@0: return NULL; etisserant@0: } etisserant@0: void *visit(while_statement_c *symbol) { etisserant@0: s4o.print("while ("); etisserant@0: symbol->expression->accept(*this); etisserant@0: s4o.print(") {\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->statement_list->accept(*this); etisserant@0: s4o.indent_left(); etisserant@0: s4o.print(s4o.indent_spaces); s4o.print("}"); etisserant@0: return NULL; etisserant@0: } etisserant@0: void *visit(repeat_statement_c *symbol) { etisserant@0: s4o.print("do {\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->statement_list->accept(*this); etisserant@0: s4o.indent_left(); etisserant@0: s4o.print(s4o.indent_spaces); s4o.print("} while("); etisserant@0: symbol->expression->accept(*this); etisserant@0: s4o.print(")"); etisserant@0: return NULL; etisserant@0: } etisserant@0: void *visit(exit_statement_c *symbol) { etisserant@0: s4o.print("exit(0)"); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: etisserant@0: }; /* generate_cc_st_c */ etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: