stage4/generate_c/generate_c_st.cc
changeset 149 05ca171a3d57
parent 146 eef5e62048c7
child 160 59d58f5e6caa
equal deleted inserted replaced
148:912bdab1eca8 149:05ca171a3d57
   353   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   353   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   354 	symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   354 	symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   355 	if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
   355 	if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
   356       (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
   356       (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
   357       (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)))
   357       (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)))
   358     return print_binary_function("__time_add", symbol->l_exp, symbol->r_exp);
   358     return print_binary_function("__TIME_ADD", symbol->l_exp, symbol->r_exp);
   359   if (!search_expression_type->is_same_type(left_type, right_type))
   359   if (!search_expression_type->is_same_type(left_type, right_type))
   360       ERROR;
   360       ERROR;
   361   if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
   361   if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
   362     return print_binary_expression(symbol->l_exp, symbol->r_exp, " + ");
   362     return print_binary_expression(symbol->l_exp, symbol->r_exp, " + ");
   363   ERROR;
   363   ERROR;
   371       (typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) ||
   371       (typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) ||
   372       (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
   372       (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
   373       (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) ||
   373       (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) ||
   374       (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
   374       (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
   375       (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c)))
   375       (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c)))
   376     return print_binary_function("__time_sub", symbol->l_exp, symbol->r_exp);
   376     return print_binary_function("__TIME_SUB", symbol->l_exp, symbol->r_exp);
   377   if (!search_expression_type->is_same_type(left_type, right_type))
   377   if (!search_expression_type->is_same_type(left_type, right_type))
   378       ERROR;
   378       ERROR;
   379   if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
   379   if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
   380     return print_binary_expression(symbol->l_exp, symbol->r_exp, " - ");
   380     return print_binary_expression(symbol->l_exp, symbol->r_exp, " - ");
   381   ERROR;
   381   ERROR;
   385 void *visit(mul_expression_c *symbol) {
   385 void *visit(mul_expression_c *symbol) {
   386   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   386   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   387   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   387   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   388   if ((typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_integer_type(right_type)) ||
   388   if ((typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_integer_type(right_type)) ||
   389       (typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_real_type(right_type)))
   389       (typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_real_type(right_type)))
   390     return print_binary_function("__time_mul", symbol->l_exp, symbol->r_exp);
   390     return print_binary_function("__TIME_MUL", symbol->l_exp, symbol->r_exp);
   391   if (!search_expression_type->is_same_type(left_type, right_type))
   391   if (!search_expression_type->is_same_type(left_type, right_type))
   392       ERROR;
   392       ERROR;
   393   if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
   393   if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
   394     return print_binary_expression(symbol->l_exp, symbol->r_exp, " * ");
   394     return print_binary_expression(symbol->l_exp, symbol->r_exp, " * ");
   395   ERROR;
   395   ERROR;
   397 }
   397 }
   398 
   398 
   399 void *visit(div_expression_c *symbol) {
   399 void *visit(div_expression_c *symbol) {
   400   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   400   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   401   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   401   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
       
   402   if ((typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_integer_type(right_type)) ||
       
   403       (typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_real_type(right_type)))
       
   404     return print_binary_function("__TIME_DIV", symbol->l_exp, symbol->r_exp);
   402   if (!search_expression_type->is_same_type(left_type, right_type))
   405   if (!search_expression_type->is_same_type(left_type, right_type))
   403       ERROR;
   406       ERROR;
   404   if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
   407   if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
   405     return print_binary_expression(symbol->l_exp, symbol->r_exp, " / ");
   408     return print_binary_expression(symbol->l_exp, symbol->r_exp, " / ");
   406   ERROR;
   409   ERROR;
   442   return NULL;
   445   return NULL;
   443 }
   446 }
   444 
   447 
   445 void *visit(function_invocation_c *symbol) {
   448 void *visit(function_invocation_c *symbol) {
   446   function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
   449   function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
       
   450   
       
   451   symbol_c* function_type_prefix = NULL;
       
   452   symbol_c* function_name = NULL;
       
   453   symbol_c* function_type_suffix = NULL;
       
   454   std::list<FUNCTION_PARAM> param_list;
       
   455   FUNCTION_PARAM *param;
   447   
   456   
   448   if (f_decl == function_symtable.end_value()) {
   457   if (f_decl == function_symtable.end_value()) {
   449     /* The function called is not in the symtable, so we test if it is a
   458     /* The function called is not in the symtable, so we test if it is a
   450      * standard function defined in standard */
   459      * standard function defined in standard */
   451     
   460     
   454     
   463     
   455     symbol_c *function_return_type = search_expression_type->get_type(symbol);
   464     symbol_c *function_return_type = search_expression_type->get_type(symbol);
   456     
   465     
   457     function_call_param_iterator_c function_call_param_iterator(symbol);
   466     function_call_param_iterator_c function_call_param_iterator(symbol);
   458     
   467     
       
   468     identifier_c en_param_name("EN");
       
   469     /* Get the value from EN param */
       
   470     symbol_c *EN_param_value = function_call_param_iterator.search(&en_param_name);
       
   471     if (EN_param_value == NULL)
       
   472       EN_param_value = (symbol_c*)(new boolean_literal_c((symbol_c*)(new bool_type_name_c()), new boolean_true_c()));
       
   473     ADD_PARAM_LIST(EN_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_in)
       
   474     
       
   475     identifier_c eno_param_name("EN0");
       
   476     /* Get the value from ENO param */
       
   477     symbol_c *ENO_param_value = function_call_param_iterator.search(&eno_param_name);
       
   478     ADD_PARAM_LIST(ENO_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_out)
       
   479     
   459     int nb_param = ((list_c *)symbol->parameter_assignment_list)->n;
   480     int nb_param = ((list_c *)symbol->parameter_assignment_list)->n;
   460 
   481 
   461     #include "st_code_gen.c"
   482     #include "st_code_gen.c"
   462 
   483 
   463   }
   484   }
   465     /* loop through each function parameter, find the value we should pass
   486     /* loop through each function parameter, find the value we should pass
   466      * to it, and then output the c equivalent...
   487      * to it, and then output the c equivalent...
   467      */
   488      */
   468     function_param_iterator_c fp_iterator(f_decl);
   489     function_param_iterator_c fp_iterator(f_decl);
   469   
   490   
   470     symbol->function_name->accept(*this);
   491     function_name = symbol->function_name;
   471     s4o.print("(");
       
   472     s4o.indent_right();
       
   473   
   492   
   474     identifier_c *param_name;
   493     identifier_c *param_name;
   475     function_call_param_iterator_c function_call_param_iterator(symbol);
   494     function_call_param_iterator_c function_call_param_iterator(symbol);
   476     for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
   495     for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
   477       if (i != 1)
   496       
   478         s4o.print(",\n"+s4o.indent_spaces);
       
   479   
       
   480       function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
   497       function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
   481   
   498       
   482       /* Get the value from a foo(<param_name> = <param_value>) style call */
   499       /* Get the value from a foo(<param_name> = <param_value>) style call */
   483       symbol_c *param_value = function_call_param_iterator.search(param_name);
   500       symbol_c *param_value = function_call_param_iterator.search(param_name);
   484   
   501   
   485       /* Get the value from a foo(<param_value>) style call */
   502       /* Get the value from a foo(<param_value>) style call */
   486       if (param_value == NULL)
   503       if (param_value == NULL)
   487         param_value = function_call_param_iterator.next();
   504         param_value = function_call_param_iterator.next();
   488   
   505       
       
   506       if (param_value == NULL && param_direction == function_param_iterator_c::direction_in) {
       
   507         /* No value given for parameter, so we must use the default... */
       
   508         /* First check whether default value specified in function declaration...*/
       
   509         param_value = fp_iterator.default_value();
       
   510       }
       
   511       
   489       symbol_c *param_type = fp_iterator.param_type();
   512       symbol_c *param_type = fp_iterator.param_type();
   490       if (param_type == NULL) ERROR;
   513       if (param_type == NULL) ERROR;
   491         
   514       
   492       switch (param_direction) {
   515       ADD_PARAM_LIST(param_value, param_type, param_direction)
   493         case function_param_iterator_c::direction_in:
       
   494           if (param_value == NULL) {
       
   495             /* No value given for parameter, so we must use the default... */
       
   496             /* First check whether default value specified in function declaration...*/
       
   497             param_value = fp_iterator.default_value();
       
   498           }
       
   499           if (param_value == NULL) {
       
   500             /* If not, get the default value of this variable's type */
       
   501             param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
       
   502           }
       
   503           if (param_value == NULL) ERROR;
       
   504           if (search_base_type.type_is_subrange(param_type)) {
       
   505             s4o.print("__CHECK_");
       
   506             param_type->accept(*this);
       
   507             s4o.print("(");
       
   508           }
       
   509           param_value->accept(*this);
       
   510           if (search_base_type.type_is_subrange(param_type))
       
   511             s4o.print(")");
       
   512           break;
       
   513         case function_param_iterator_c::direction_out:
       
   514         case function_param_iterator_c::direction_inout:
       
   515           current_param_is_pointer = true;
       
   516           if (param_value == NULL) {
       
   517             s4o.print("NULL");
       
   518           } else {
       
   519             param_value->accept(*this);
       
   520           }
       
   521           current_param_is_pointer = false;
       
   522           break;
       
   523         case function_param_iterator_c::direction_extref:
       
   524           /* TODO! */
       
   525           ERROR;
       
   526           break;
       
   527       } /* switch */
       
   528     } /* for(...) */
   516     } /* for(...) */
   529     // symbol->parameter_assignment->accept(*this);
   517     // symbol->parameter_assignment->accept(*this);
       
   518   }
       
   519   
       
   520   if (function_type_prefix != NULL) {
       
   521     s4o.print("(");
       
   522     function_type_prefix->accept(*this);
   530     s4o.print(")");
   523     s4o.print(")");
   531     s4o.indent_left();
   524   }
   532   }
   525   if (function_name != NULL)
       
   526     function_name->accept(*this);
       
   527   if (function_type_suffix != NULL)
       
   528     function_type_suffix->accept(*this);
       
   529   s4o.print("(");
       
   530   s4o.indent_right();
       
   531   
       
   532   std::list<FUNCTION_PARAM>::iterator pt;
       
   533   for(pt = param_list.begin(); pt != param_list.end(); pt++) {
       
   534     if (pt != param_list.begin())
       
   535       s4o.print(",\n"+s4o.indent_spaces);
       
   536     symbol_c *param_value = pt->param_value;
       
   537     symbol_c *param_type = pt->param_type;
       
   538           
       
   539     switch (pt->param_direction) {
       
   540       case function_param_iterator_c::direction_in:
       
   541         if (param_value == NULL) {
       
   542           /* If not, get the default value of this variable's type */
       
   543           param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
       
   544         }
       
   545         if (param_value == NULL) ERROR;
       
   546         if (search_base_type.type_is_subrange(param_type)) {
       
   547           s4o.print("__CHECK_");
       
   548           param_type->accept(*this);
       
   549           s4o.print("(");
       
   550         }
       
   551         param_value->accept(*this);
       
   552         if (search_base_type.type_is_subrange(param_type))
       
   553           s4o.print(")");
       
   554         break;
       
   555       case function_param_iterator_c::direction_out:
       
   556       case function_param_iterator_c::direction_inout:
       
   557         current_param_is_pointer = true;
       
   558         if (param_value == NULL) {
       
   559           s4o.print("NULL");
       
   560         } else {
       
   561           param_value->accept(*this);
       
   562         }
       
   563         current_param_is_pointer = false;
       
   564         break;
       
   565       case function_param_iterator_c::direction_extref:
       
   566         /* TODO! */
       
   567         ERROR;
       
   568         break;
       
   569     } /* switch */
       
   570   }  
       
   571 
       
   572   s4o.print(")");
       
   573   s4o.indent_left();
   533 
   574 
   534   return NULL;
   575   return NULL;
   535 }
   576 }
   536 
   577 
   537 /********************/
   578 /********************/