Adding support for standard function block parsing
authorlbessard
Thu, 14 Jun 2007 08:58:10 +0200
changeset 32 289256ec66f1
parent 31 c6959b0f539d
child 33 4507beb2aac6
Adding support for standard function block parsing
Need to be extend to all standard function blocks defined
.cvsignore
absyntax/absyntax.def
lib/ieclib.txt
stage4/generate_cc/function_call_param_iterator.cc
stage4/generate_cc/generate_cc.cc
stage4/generate_cc/generate_cc_il.cc
stage4/generate_cc/generate_cc_st.cc
stage4/generate_cc/generate_cc_tempvardecl.cc
stage4/generate_cc/search_base_type.cc
stage4/generate_cc/search_constant_type.cc
stage4/generate_cc/search_expression_type.cc
stage4/generate_iec/generate_iec.cc
--- a/.cvsignore	Tue May 29 09:45:50 2007 +0200
+++ b/.cvsignore	Thu Jun 14 08:58:10 2007 +0200
@@ -2,3 +2,4 @@
 iec2iec
 iec2cc
 *.txt
+*.c
--- a/absyntax/absyntax.def	Tue May 29 09:45:50 2007 +0200
+++ b/absyntax/absyntax.def	Thu Jun 14 08:58:10 2007 +0200
@@ -155,7 +155,8 @@
 SYM_REF0(lword_type_name_c)
 SYM_REF0(string_type_name_c)
 SYM_REF0(wstring_type_name_c)
-
+SYM_REF0(constant_int_type_name_c)
+SYM_REF0(constant_real_type_name_c)
 
 
 /********************************/
--- a/lib/ieclib.txt	Tue May 29 09:45:50 2007 +0200
+++ b/lib/ieclib.txt	Thu Jun 14 08:58:10 2007 +0200
@@ -27,10 +27,11 @@
  * and function blocks defined in the standard.
  *)
 
-{#include "bcd_to_int.txt" }
-{#include "int_to_bcd.txt" }
+
+(*{#include "bcd_to_int.txt" }*)
+(*{#include "int_to_bcd.txt" }*)
 {#include "edge_detection.txt" }
 {#include "bistable.txt" }
 {#include "counter.txt" }
 {#include "timer.txt" }
-{#include "time_math.txt" }
+(*{#include "time_math.txt" }*)
\ No newline at end of file
--- a/stage4/generate_cc/function_call_param_iterator.cc	Tue May 29 09:45:50 2007 +0200
+++ b/stage4/generate_cc/function_call_param_iterator.cc	Thu Jun 14 08:58:10 2007 +0200
@@ -88,30 +88,30 @@
         case iterate_op:
           for(int i = 0; i < list->n; i++) {
             void *res = list->elements[i]->accept(*this);
-	    if (NULL != res) {
-	      /* It went through the handle_parameter_assignment() function,
-	       * and is therefore a parameter assignment (<param> = <value>),
-	       * and not a simple expression (<value>).
-	       */
-	       /* we do nothing... */
-	    } else {
+            if (NULL != res) {
+              /* It went through the handle_parameter_assignment() function,
+               * and is therefore a parameter assignment (<param> = <value>),
+               * and not a simple expression (<value>).
+               */
+              /* we do nothing... */
+            } else {
               param_count++;
               if (param_count == next_param) {
                 return list->elements[i];
-	      }
-	    }
+              }
+            }
           }
           return NULL;
-	  break;
-
-	case search_op:
+          break;
+
+        case search_op:
           for(int i = 0; i < list->n; i++) {
             void *res = list->elements[i]->accept(*this);
             if (res != NULL)
               return res;
           }
           return NULL;
-	  break;
+          break;
       } /* switch */
       return NULL;
     }
@@ -121,18 +121,18 @@
     void *handle_parameter_assignment(symbol_c *variable_name, symbol_c *expression) {
       switch (current_operation) {
         case iterate_op:
-	       /* UGLY HACK -> this will be detected in the search_list() function */
-	  return (void *)this; /* anything, as long as it is not NULL!! */
-	  break;
-
-	case search_op:
+	        /* UGLY HACK -> this will be detected in the search_list() function */
+          return (void *)this; /* anything, as long as it is not NULL!! */
+          break;
+
+        case search_op:
           identifier_c *variable_name2 = dynamic_cast<identifier_c *>(variable_name);
           if (variable_name2 == NULL) ERROR;
           if (strcasecmp(search_param_name->value, variable_name2->value) == 0)
             /* FOUND! This is the same parameter!! */
-	    return (void *)expression;
+            return (void *)expression;
           return NULL;
-	  break;
+          break;
       }
 
       ERROR;
@@ -466,7 +466,10 @@
 */
     void *visit(function_invocation_c *symbol) {
       TRACE("function_invocation_c");
-      return symbol->parameter_assignment_list->accept(*this);
+      if ((symbol_c *)symbol == f_call)
+        return symbol->parameter_assignment_list->accept(*this);
+      else
+        return NULL;
     }
 
 
--- a/stage4/generate_cc/generate_cc.cc	Tue May 29 09:45:50 2007 +0200
+++ b/stage4/generate_cc/generate_cc.cc	Thu Jun 14 08:58:10 2007 +0200
@@ -627,7 +627,7 @@
    * function block declaration symbol table...
    */
   function_block_type_symtable.insert(symbol->fblock_name, symbol);
-
+  
   /* (A) Function Block data structure declaration... */
   /* (A.1) Data structure declaration */
   s4o.print("// FUNCTION_BLOCK ");
@@ -1403,7 +1403,7 @@
       generate_location_list_c generate_location_list(&s4o);
       symbol->accept(generate_location_list);
       
-      for(int i = 1; i < symbol->n; i++) {
+      for(int i = 0; i < symbol->n; i++) {
         symbol->elements[i]->accept(*this);
       }
       return NULL;
--- a/stage4/generate_cc/generate_cc_il.cc	Tue May 29 09:45:50 2007 +0200
+++ b/stage4/generate_cc/generate_cc_il.cc	Thu Jun 14 08:58:10 2007 +0200
@@ -402,8 +402,8 @@
       this->default_variable_name.accept(*this);
       this->default_variable_name.current_type = backup;
 
-      if (search_expression_type->is_time_compatible(this->default_variable_name.current_type) &&
-          search_expression_type->is_time_compatible(this->current_operand_type)) {
+      if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
+          search_expression_type->is_time_type(this->current_operand_type)) {
         s4o.print(" = __compare_timespec(");
         s4o.print(operation);
         s4o.print(", ");
@@ -427,7 +427,7 @@
 
     /* A helper function... */
     void C_modifier(void) {
-      if (search_expression_type->is_numeric_compatible(default_variable_name.current_type)) {
+      if (search_expression_type->is_bool_type(default_variable_name.current_type)) {
         s4o.print("if (");
         this->default_variable_name.accept(*this);
         s4o.print(") ");
@@ -437,7 +437,7 @@
 
     /* A helper function... */
     void CN_modifier(void) {
-      if (search_expression_type->is_numeric_compatible(default_variable_name.current_type)) {
+      if (search_expression_type->is_bool_type(default_variable_name.current_type)) {
         s4o.print("if (!");
         this->default_variable_name.accept(*this);
         s4o.print(") ");
@@ -550,101 +550,156 @@
 void *visit(il_function_call_c *symbol) {
   function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
 
-  if (f_decl == function_symtable.end_value())
+  if (f_decl == function_symtable.end_value()) {
     /* should never occur. The function being called MUST be in the symtable... */
-    ERROR;
-
-  /* determine the base data type returned by the function being called... */
-  search_base_type_c search_base_type;
-  symbol_c *return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type);
-  symbol_c *param_data_type = default_variable_name.current_type;
-  if (NULL == return_data_type) ERROR;
-
-  default_variable_name.current_type = return_data_type;
-  this->default_variable_name.accept(*this);
-  default_variable_name.current_type = param_data_type;
-  s4o.print(" = ");
-
-  symbol->function_name->accept(*this);
-  s4o.print("(");
-
-  /* loop through each function parameter, find the value we should pass
-   * to it, and then output the c equivalent...
-   */
-
-  function_param_iterator_c fp_iterator(f_decl);
-  identifier_c *param_name;
-  function_call_param_iterator_c function_call_param_iterator(symbol);
-  for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
-    if (i != 1)
-      s4o.print(", ");
-
-    symbol_c *param_type = fp_iterator.param_type();
-    if (param_type == NULL) ERROR;
-
-    function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
-
-
-    symbol_c *param_value = NULL;
-
-    /* if it is the first parameter, semantics specifies that we should
-     * get the value off the IL default variable!
+    function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
+    if (current_function_type == function_none) ERROR;
+    
+    symbol_c *param_data_type = default_variable_name.current_type;
+    symbol_c *return_data_type = (symbol_c *)search_expression_type->compute_standard_function_il(symbol, param_data_type);
+    if (NULL == return_data_type) ERROR;
+    
+    default_variable_name.current_type = return_data_type;
+    this->default_variable_name.accept(*this);
+    default_variable_name.current_type = param_data_type;
+    s4o.print(" = ");
+    
+    function_call_param_iterator_c function_call_param_iterator(symbol);
+    
+    int nb_param = 1;
+    if (symbol->il_operand_list != NULL)
+      nb_param += ((list_c *)symbol->il_operand_list)->n;
+    
+    for(int current_param = 0; current_param < nb_param; current_param++) {
+      symbol_c *param_value;
+      if (current_param == 0)
+        param_value = &this->default_variable_name;
+      else {
+        symbol_c *param_name = NULL;
+        switch (current_function_type) {
+          default: ERROR;
+        }
+        
+         
+        /* Get the value from a foo(<param_name> = <param_value>) style call */
+        param_value = function_call_param_iterator.search(param_name);
+        delete param_name;
+        
+        /* Get the value from a foo(<param_value>) style call */
+        if (param_value == NULL)
+          param_value = function_call_param_iterator.next();
+        
+        if (param_value == NULL) ERROR;
+      }
+      
+      switch (current_function_type) {
+        case (function_sqrt):
+          if (current_param == 0) {
+            s4o.print("sqrt(");
+            param_value->accept(*this);
+            s4o.print(")");
+          }
+          else ERROR;
+          break;
+        default: ERROR;
+      }
+    } /* for(...) */
+    
+    /* the data type returned by the function, and stored in the il default variable... */
+    default_variable_name.current_type = return_data_type;
+  }
+  else {
+    /* determine the base data type returned by the function being called... */
+    search_base_type_c search_base_type;
+    symbol_c *return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type);
+    symbol_c *param_data_type = default_variable_name.current_type;
+    if (NULL == return_data_type) ERROR;
+  
+    default_variable_name.current_type = return_data_type;
+    this->default_variable_name.accept(*this);
+    default_variable_name.current_type = param_data_type;
+    s4o.print(" = ");
+  
+    symbol->function_name->accept(*this);
+    s4o.print("(");
+  
+    /* loop through each function parameter, find the value we should pass
+     * to it, and then output the c equivalent...
      */
-   if (1 == i)
-     param_value = &this->default_variable_name;
-
-    /* Get the value from a foo(<param_name> = <param_value>) style call */
-    /* NOTE: the following line of code is not required in this case, but it doesn't
-     * harm to leave it in, as in the case of a non-formal syntax function call,
-     * it will always return NULL.
-     * We leave it in in case we later decide to merge this part of the code together
-     * with the function calling code in generate_cc_st_c, which does require
-     * the following line...
-     */
-    if (param_value == NULL)
-      param_value = function_call_param_iterator.search(param_name);
-
-    /* Get the value from a foo(<param_value>) style call */
-    if (param_value == NULL)
-      param_value = function_call_param_iterator.next();
-
-    switch (param_direction) {
-      case function_param_iterator_c::direction_in:
-        if (param_value == NULL) {
-          /* No value given for parameter, so we must use the default... */
-          /* First check whether default value specified in function declaration...*/
-          param_value = fp_iterator.default_value();
-        }
-        if (param_value == NULL) {
-          /* If not, get the default value of this variable's type */
-          param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
-        }
-        if (param_value == NULL) ERROR;
-        param_value->accept(*this);
-	break;
-      case function_param_iterator_c::direction_out:
-      case function_param_iterator_c::direction_inout:
-        if (param_value == NULL) {
-	  /* no parameter value given, so we pass a previously declared temporary variable. */
-          std::string *temp_var_name = temp_var_name_factory.new_name();
-          s4o.print(*temp_var_name);
-          delete temp_var_name;
-	} else {
+  
+    function_param_iterator_c fp_iterator(f_decl);
+    identifier_c *param_name;
+    function_call_param_iterator_c function_call_param_iterator(symbol);
+    for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
+      if (i != 1)
+        s4o.print(", ");
+  
+      symbol_c *param_type = fp_iterator.param_type();
+      if (param_type == NULL) ERROR;
+  
+      function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
+  
+  
+      symbol_c *param_value = NULL;
+  
+      /* if it is the first parameter, semantics specifies that we should
+       * get the value off the IL default variable!
+       */
+     if (1 == i)
+       param_value = &this->default_variable_name;
+  
+      /* Get the value from a foo(<param_name> = <param_value>) style call */
+      /* NOTE: the following line of code is not required in this case, but it doesn't
+       * harm to leave it in, as in the case of a non-formal syntax function call,
+       * it will always return NULL.
+       * We leave it in in case we later decide to merge this part of the code together
+       * with the function calling code in generate_cc_st_c, which does require
+       * the following line...
+       */
+      if (param_value == NULL)
+        param_value = function_call_param_iterator.search(param_name);
+  
+      /* Get the value from a foo(<param_value>) style call */
+      if (param_value == NULL)
+        param_value = function_call_param_iterator.next();
+  
+      switch (param_direction) {
+        case function_param_iterator_c::direction_in:
+          if (param_value == NULL) {
+            /* No value given for parameter, so we must use the default... */
+            /* First check whether default value specified in function declaration...*/
+            param_value = fp_iterator.default_value();
+          }
+          if (param_value == NULL) {
+            /* If not, get the default value of this variable's type */
+            param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
+          }
+          if (param_value == NULL) ERROR;
           param_value->accept(*this);
-	}
-	break;
-      case function_param_iterator_c::direction_extref:
-        /* TODO! */
-        ERROR;
-	break;
-    } /* switch */
-  } /* for(...) */
-
-  s4o.print(")");
-
-  /* the data type returned by the function, and stored in the il default variable... */
-  default_variable_name.current_type = return_data_type;
-
+          break;
+        case function_param_iterator_c::direction_out:
+        case function_param_iterator_c::direction_inout:
+          if (param_value == NULL) {
+  	  /* no parameter value given, so we pass a previously declared temporary variable. */
+            std::string *temp_var_name = temp_var_name_factory.new_name();
+            s4o.print(*temp_var_name);
+            delete temp_var_name;
+          } else {
+            param_value->accept(*this);
+          }
+          break;
+        case function_param_iterator_c::direction_extref:
+          /* TODO! */
+          ERROR;
+          break;
+      } /* switch */
+    } /* for(...) */
+  
+    s4o.print(")");
+    /* the data type returned by the function, and stored in the il default variable... */
+    default_variable_name.current_type = return_data_type;
+  }
+  
   return NULL;
 }
 
@@ -1080,8 +1135,8 @@
 void *visit(PT_operator_c *symbol)	{return XXX_CAL_operator("PT", this->current_operand);}
 
 void *visit(AND_operator_c *symbol)	{
-  if (search_expression_type->is_numeric_compatible(this->default_variable_name.current_type) &&
-      search_expression_type->is_numeric_compatible(this->current_operand_type)) {
+  if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
     XXX_operator(&(this->default_variable_name), " &= ", this->current_operand);
     /* the data type resulting from this operation... */
     this->default_variable_name.current_type = this->current_operand_type;
@@ -1091,8 +1146,8 @@
 }
 
 void *visit(OR_operator_c *symbol)	{
-  if (search_expression_type->is_numeric_compatible(this->default_variable_name.current_type) &&
-      search_expression_type->is_numeric_compatible(this->current_operand_type)) {
+  if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
     XXX_operator(&(this->default_variable_name), " |= ", this->current_operand);
     /* the data type resulting from this operation... */
     this->default_variable_name.current_type = this->current_operand_type;
@@ -1102,8 +1157,8 @@
 }
 
 void *visit(XOR_operator_c *symbol)	{
-  if (search_expression_type->is_numeric_compatible(this->default_variable_name.current_type) &&
-      search_expression_type->is_numeric_compatible(this->current_operand_type)) {
+  if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
     // '^' is a bit by bit exclusive OR !! Also seems to work with boolean types!
     XXX_operator(&(this->default_variable_name), " ^= ", this->current_operand);
     /* the data type resulting from this operation... */
@@ -1114,8 +1169,8 @@
 }
 
 void *visit(ANDN_operator_c *symbol)	{
-  if (search_expression_type->is_numeric_compatible(this->default_variable_name.current_type) &&
-      search_expression_type->is_numeric_compatible(this->current_operand_type)) {
+  if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
     XXX_operator(&(this->default_variable_name),
                  search_expression_type->is_bool_type(this->current_operand_type)?" &= !":" &= ~",
                  this->current_operand);
@@ -1127,8 +1182,8 @@
 }
 
 void *visit(ORN_operator_c *symbol)	{
-  if (search_expression_type->is_numeric_compatible(this->default_variable_name.current_type) &&
-      search_expression_type->is_numeric_compatible(this->current_operand_type)) {
+  if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
     XXX_operator(&(this->default_variable_name),
                  search_expression_type->is_bool_type(this->current_operand_type)?" |= !":" |= ~",
                  this->current_operand);
@@ -1140,8 +1195,8 @@
 }
 
 void *visit(XORN_operator_c *symbol)	{
-  if (search_expression_type->is_numeric_compatible(this->default_variable_name.current_type) &&
-      search_expression_type->is_numeric_compatible(this->current_operand_type)) {
+  if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
     XXX_operator(&(this->default_variable_name),
                  // bit by bit exclusive OR !! Also seems to work with boolean types!
                  search_expression_type->is_bool_type(this->current_operand_type)?" ^= !":" ^= ~",
@@ -1154,15 +1209,15 @@
 }
 
 void *visit(ADD_operator_c *symbol)	{
-  if (search_expression_type->is_time_compatible(this->default_variable_name.current_type) &&
-      search_expression_type->is_time_compatible(this->current_operand_type)) {
+  if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_time_type(this->current_operand_type)) {
     XXX_function("__add_timespec", &(this->default_variable_name), this->current_operand);
     /* the data type resulting from this operation... */
     this->default_variable_name.current_type = this->current_operand_type;
     return NULL;
   }
-  if (search_expression_type->is_numeric_compatible(this->default_variable_name.current_type) &&
-      search_expression_type->is_numeric_compatible(this->current_operand_type)) {
+  if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
     XXX_operator(&(this->default_variable_name), " += ", this->current_operand);
     /* the data type resulting from this operation... */
     this->default_variable_name.current_type = this->current_operand_type;
@@ -1173,15 +1228,15 @@
 }
 
 void *visit(SUB_operator_c *symbol)	{
-  if (search_expression_type->is_time_compatible(this->default_variable_name.current_type) &&
-      search_expression_type->is_time_compatible(this->current_operand_type)) {
+  if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_time_type(this->current_operand_type)) {
     XXX_function("__sub_timespec", &(this->default_variable_name), this->current_operand);
     /* the data type resulting from this operation... */
     this->default_variable_name.current_type = this->current_operand_type;
     return NULL;
   }
-  if (search_expression_type->is_numeric_compatible(this->default_variable_name.current_type) &&
-      search_expression_type->is_numeric_compatible(this->current_operand_type)) {
+  if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
     XXX_operator(&(this->default_variable_name), " -= ", this->current_operand);
     /* the data type resulting from this operation... */
     this->default_variable_name.current_type = this->current_operand_type;
@@ -1192,15 +1247,15 @@
 }
 
 void *visit(MUL_operator_c *symbol)	{
-  if (search_expression_type->is_time_compatible(this->default_variable_name.current_type) &&
-      search_expression_type->is_time_compatible(this->current_operand_type)) {
+  if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_integer_type(this->current_operand_type)) {
     XXX_function("__mul_timespec", &(this->default_variable_name), this->current_operand);
     /* the data type resulting from this operation... */
     this->default_variable_name.current_type = this->current_operand_type;
     return NULL;
   }
-  if (search_expression_type->is_numeric_compatible(this->default_variable_name.current_type) &&
-      search_expression_type->is_numeric_compatible(this->current_operand_type)) {
+  if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
     XXX_operator(&(this->default_variable_name), " *= ", this->current_operand);
     /* the data type resulting from this operation... */
     this->default_variable_name.current_type = this->current_operand_type;
@@ -1211,8 +1266,8 @@
 }
 
 void *visit(DIV_operator_c *symbol)	{
-  if (search_expression_type->is_numeric_compatible(this->default_variable_name.current_type) &&
-      search_expression_type->is_numeric_compatible(this->current_operand_type)) {
+  if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
     XXX_operator(&(this->default_variable_name), " /= ", this->current_operand);
     /* the data type resulting from this operation... */
     this->default_variable_name.current_type = this->current_operand_type;
@@ -1222,8 +1277,8 @@
 }
 
 void *visit(MOD_operator_c *symbol)	{
-  if (search_expression_type->is_numeric_compatible(this->default_variable_name.current_type) &&
-      search_expression_type->is_numeric_compatible(this->current_operand_type)) {
+  if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
     XXX_operator(&(this->default_variable_name), " %= ", this->current_operand);
     /* the data type resulting from this operation... */
     this->default_variable_name.current_type = this->current_operand_type;
--- a/stage4/generate_cc/generate_cc_st.cc	Tue May 29 09:45:50 2007 +0200
+++ b/stage4/generate_cc/generate_cc_st.cc	Thu Jun 14 08:58:10 2007 +0200
@@ -151,12 +151,12 @@
 void *visit(or_expression_c *symbol) {
   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
-  if (search_expression_type->is_bool_type(left_type) && search_expression_type->is_bool_type(right_type)) {
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_bool_type(left_type))
     return print_binary_expression(symbol->l_exp, symbol->r_exp, " || ");
-  }
-  if (search_expression_type->is_numeric_compatible(left_type) && search_expression_type->is_numeric_compatible(right_type)) {
+  if (search_expression_type->is_binary_type(left_type))
     return print_binary_expression(symbol->l_exp, symbol->r_exp, " | ");
-  }
   ERROR;
   return NULL;
 }
@@ -164,7 +164,9 @@
 void *visit(xor_expression_c *symbol) {
   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
-  if (search_expression_type->is_bool_type(left_type) && search_expression_type->is_bool_type(right_type)) {
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_bool_type(left_type)) {
     s4o.print("(");
     symbol->l_exp->accept(*this);
     s4o.print(" && !");
@@ -175,117 +177,160 @@
     symbol->r_exp->accept(*this);
     s4o.print(")");
   }
-  if (search_expression_type->is_numeric_compatible(left_type) && search_expression_type->is_numeric_compatible(right_type)) {
+  if (search_expression_type->is_binary_type(left_type))
     return print_binary_expression(symbol->l_exp, symbol->r_exp, " ^ ");
+  ERROR;
+  return NULL;
+}
+
+void *visit(and_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_bool_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " && ");
+  if (search_expression_type->is_binary_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " & ");
+  ERROR;
+  return NULL;
+}
+
+void *visit(equ_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_time_type(left_type))
+    return print_compare_function("__compare_timespec", "==", symbol->l_exp, symbol->r_exp);
+  return print_binary_expression(symbol->l_exp, symbol->r_exp, " == ");
+}
+
+void *visit(notequ_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_time_type(left_type))
+    return print_compare_function("__compare_timespec", "!=", symbol->l_exp, symbol->r_exp);
+  return print_binary_expression(symbol->l_exp, symbol->r_exp, " != ");
+}
+
+void *visit(lt_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_time_type(left_type))
+    return print_compare_function("__compare_timespec", "<", symbol->l_exp, symbol->r_exp);
+  return print_binary_expression(symbol->l_exp, symbol->r_exp, " < ");
+}
+
+void *visit(gt_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_time_type(left_type))
+    return print_compare_function("__compare_timespec", ">", symbol->l_exp, symbol->r_exp);
+  return print_binary_expression(symbol->l_exp, symbol->r_exp, " > ");
+}
+
+void *visit(le_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_time_type(left_type))
+    return print_compare_function("__compare_timespec", "<=", symbol->l_exp, symbol->r_exp);
+  return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= ");
+}
+
+void *visit(ge_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_time_type(left_type))
+    return print_compare_function("__compare_timespec", ">=", symbol->l_exp, symbol->r_exp);
+  return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= ");
+}
+
+void *visit(add_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+	symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+	if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
+      (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
+      (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)))
+    return print_binary_function("__add_timespec", symbol->l_exp, symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " + ");
+  ERROR;
+  return NULL;
+}
+
+void *visit(sub_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
+      (typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) ||
+      (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
+      (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) ||
+      (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
+      (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c)))
+    return print_binary_function("__sub_timespec", symbol->l_exp, symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " - ");
+  ERROR;
+  return NULL;
+}
+
+void *visit(mul_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if ((typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_integer_type(right_type)) ||
+      (typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_real_type(right_type)))
+    return print_binary_function("__mul_timespec", symbol->l_exp, symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " * ");
+  ERROR;
+  return NULL;
+}
+
+void *visit(div_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " / ");
+  ERROR;
+  return NULL;
+}
+
+void *visit(mod_expression_c *symbol) {
+  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
+  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
+  if (!search_expression_type->is_same_type(left_type, right_type))
+      ERROR;
+  if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) {
+    s4o.print("((");
+    symbol->r_exp->accept(*this);
+    s4o.print(" == 0)?0:");
+    print_binary_expression(symbol->l_exp, symbol->r_exp, " % ");
+    s4o.print(")");
+    return NULL;
   }
   ERROR;
   return NULL;
 }
 
-void *visit(and_expression_c *symbol) {
-  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
-  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
-  if (search_expression_type->is_bool_type(left_type) && search_expression_type->is_bool_type(right_type)) {
-    return print_binary_expression(symbol->l_exp, symbol->r_exp, " && ");
-  }
-  if (search_expression_type->is_numeric_compatible(left_type) && search_expression_type->is_numeric_compatible(right_type)) {
-    return print_binary_expression(symbol->l_exp, symbol->r_exp, " & ");
-  }
-  ERROR;
-  return NULL;
-}
-
-void *visit(equ_expression_c *symbol) {
-  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
-  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
-  if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
-    return print_compare_function("__compare_timespec", "==", symbol->l_exp, symbol->r_exp);
-  }
-  return print_binary_expression(symbol->l_exp, symbol->r_exp, " == ");
-}
-
-void *visit(notequ_expression_c *symbol) {
-  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
-  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
-  if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
-    return print_compare_function("__compare_timespec", "!=", symbol->l_exp, symbol->r_exp);
-  }
-  return print_binary_expression(symbol->l_exp, symbol->r_exp, " != ");
-}
-
-void *visit(lt_expression_c *symbol) {
-  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
-  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
-  if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
-    return print_compare_function("__compare_timespec", "<", symbol->l_exp, symbol->r_exp);
-  }
-  return print_binary_expression(symbol->l_exp, symbol->r_exp, " < ");
-}
-
-void *visit(gt_expression_c *symbol) {
-  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
-  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
-  if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
-    return print_compare_function("__compare_timespec", ">", symbol->l_exp, symbol->r_exp);
-  }
-  return print_binary_expression(symbol->l_exp, symbol->r_exp, " > ");
-}
-
-void *visit(le_expression_c *symbol) {
-  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
-  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
-  if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
-    return print_compare_function("__compare_timespec", "<=", symbol->l_exp, symbol->r_exp);
-  }
-  return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= ");
-}
-
-void *visit(ge_expression_c *symbol) {
-  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
-  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
-  if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
-    return print_compare_function("__compare_timespec", ">=", symbol->l_exp, symbol->r_exp);
-  }
-  return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= ");
-}
-
-void *visit(add_expression_c *symbol) {
-  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
-	symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
-	if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
-		return print_binary_function("__add_timespec", symbol->l_exp, symbol->r_exp);
-	}
-	return print_binary_expression(symbol->l_exp, symbol->r_exp, " + ");
-}
-
-void *visit(sub_expression_c *symbol) {
-  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
-  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
-  if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
-    return print_binary_function("__sub_timespec", symbol->l_exp, symbol->r_exp);
-  }
-  return print_binary_expression(symbol->l_exp, symbol->r_exp, " - ");
-}
-
-void *visit(mul_expression_c *symbol) {
-  symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
-  symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
-  if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
-    return print_binary_function("__mul_timespec", symbol->l_exp, symbol->r_exp);
-  }
-  return print_binary_expression(symbol->l_exp, symbol->r_exp, " * ");
-}
-
-void *visit(div_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " / ");}
-void *visit(mod_expression_c *symbol) {
-  s4o.print("((");
-  symbol->r_exp->accept(*this);
-  s4o.print(" == 0)?0:");
-  print_binary_expression(symbol->l_exp, symbol->r_exp, " % ");
-  s4o.print(")");
-  return NULL;
-}
-
 /* TODO: power expression... */
 void *visit(power_expression_c *symbol) {ERROR; return print_binary_expression(symbol->l_exp, symbol->r_exp, " ** ");}
 void *visit(neg_expression_c *symbol) {return print_unary_expression(symbol->exp, " -");}
@@ -293,71 +338,186 @@
 
 void *visit(function_invocation_c *symbol) {
   function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
-
-  if (f_decl == function_symtable.end_value())
-    /* should never occur. The function being called MUST be in the symtable... */
-    ERROR;
-
-  symbol->function_name->accept(*this);
-  s4o.print("(");
-
-  /* loop through each function parameter, find the value we should pass
-   * to it, and then output the c equivalent...
-   */
-
-  function_param_iterator_c fp_iterator(f_decl);
-  identifier_c *param_name;
-  function_call_param_iterator_c function_call_param_iterator(symbol);
-  for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
-    if (i != 1)
-      s4o.print(", ");
-
-    symbol_c *param_type = fp_iterator.param_type();
-    if (param_type == NULL) ERROR;
-
-    function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
-
-    /* Get the value from a foo(<param_name> = <param_value>) style call */
-    symbol_c *param_value = function_call_param_iterator.search(param_name);
-
-    /* Get the value from a foo(<param_value>) style call */
-    if (param_value == NULL)
-      param_value = function_call_param_iterator.next();
-
-    switch (param_direction) {
-      case function_param_iterator_c::direction_in:
-        if (param_value == NULL) {
-          /* No value given for parameter, so we must use the default... */
-          /* First check whether default value specified in function declaration...*/
-          param_value = fp_iterator.default_value();
-        }
-        if (param_value == NULL) {
-          /* If not, get the default value of this variable's type */
-          param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
-        }
-        if (param_value == NULL) ERROR;
-        param_value->accept(*this);
-	break;
-      case function_param_iterator_c::direction_out:
-      case function_param_iterator_c::direction_inout:
-        if (param_value == NULL) {
-	  /* no parameter value given, so we pass a previously declared temporary variable. */
-          std::string *temp_var_name = temp_var_name_factory.new_name();
-          s4o.print(*temp_var_name);
-          delete temp_var_name;
-	} else {
+  
+  if (f_decl == function_symtable.end_value()) {
+    /* The function called is not in the symtable, so we test if it is a
+     * standard function defined in standard */
+    
+    function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
+    if (current_function_type == function_none) ERROR;
+    
+    symbol_c *function_return_type = search_expression_type->get_type(symbol);
+    
+    function_call_param_iterator_c function_call_param_iterator(symbol);
+    
+    int nb_param = ((list_c *)symbol->parameter_assignment_list)->n;
+    for(int current_param = 0; current_param < nb_param; current_param++) {
+      symbol_c *param_name = NULL;
+      switch (current_function_type) {
+        case function_add:
+        case function_and:
+        case function_or:
+          param_name = generate_param_name("IN%d", current_param + 1);
+          break;
+        case function_sub:
+          if (current_param < 2)
+            param_name = generate_param_name("IN%d", current_param + 1);
+          else
+            ERROR;
+          break;
+        default: ERROR;
+      }
+      
+      /* Get the value from a foo(<param_name> = <param_value>) style call */
+      symbol_c *param_value = function_call_param_iterator.search(param_name);
+      delete param_name;
+      
+      /* Get the value from a foo(<param_value>) style call */
+      if (param_value == NULL)
+        param_value = function_call_param_iterator.next();
+      
+      if (param_value == NULL) ERROR;
+      
+      switch (current_function_type) {
+        case function_add:
+          if (search_expression_type->is_time_type(function_return_type)) {
+            if (current_param == 0) {
+              s4o.print("__add_timespec(");
+              param_value->accept(*this);
+            }
+            else if (current_param == 1) {
+              s4o.print(", ");
+              param_value->accept(*this);
+              s4o.print(")");
+            }
+            else ERROR;
+          }
+          else {
+            if (current_param == 0)
+              s4o.print("(");
+            else
+              s4o.print(" + ");
+            param_value->accept(*this);
+            if (current_param == nb_param - 1)
+              s4o.print(")");
+          }
+          break;
+        case function_sub:
+          if (search_expression_type->is_time_type(function_return_type)) {
+            if (current_param == 0) {
+              s4o.print("__sub_timespec(");
+              param_value->accept(*this);
+            }
+            else if (current_param == 1) {
+              s4o.print(", ");
+              param_value->accept(*this);
+              s4o.print(")");
+            }
+            else ERROR;
+          }
+          else {
+            if (current_param == 0) {
+              s4o.print("(");
+              param_value->accept(*this);
+            }
+            else if (current_param == 1) {
+              s4o.print(" - ");
+              param_value->accept(*this);
+              s4o.print(")");
+            }
+            else ERROR;
+          }
+          break;
+        case function_and:
+          if (current_param == 0)
+            s4o.print("(");
+          else
+            if (search_expression_type->is_bool_type(function_return_type))
+              s4o.print(" && ");
+            else
+              s4o.print(" & ");
           param_value->accept(*this);
-	}
-	break;
-      case function_param_iterator_c::direction_extref:
-        /* TODO! */
-        ERROR;
-        break;
-    } /* switch */
-  } /* for(...) */
-
-  // symbol->parameter_assignment->accept(*this);
-  s4o.print(")");
+          if (current_param == nb_param - 1)
+            s4o.print(")");
+          break;
+        case function_or:
+          if (current_param == 0)
+            s4o.print("(");
+          else
+            if (search_expression_type->is_bool_type(function_return_type))
+              s4o.print(" || ");
+            else
+              s4o.print(" | ");
+          param_value->accept(*this);
+          if (current_param == nb_param - 1)
+            s4o.print(")");
+          break;
+        default: ERROR;
+      }
+    } /* for(...) */
+  }
+  else {
+    /* loop through each function parameter, find the value we should pass
+     * to it, and then output the c equivalent...
+     */
+    function_param_iterator_c fp_iterator(f_decl);
+  
+    symbol->function_name->accept(*this);
+    s4o.print("(");
+  
+    identifier_c *param_name;
+    function_call_param_iterator_c function_call_param_iterator(symbol);
+    for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
+      if (i != 1)
+        s4o.print(", ");
+  
+      function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
+  
+      /* Get the value from a foo(<param_name> = <param_value>) style call */
+      symbol_c *param_value = function_call_param_iterator.search(param_name);
+  
+      /* Get the value from a foo(<param_value>) style call */
+      if (param_value == NULL)
+        param_value = function_call_param_iterator.next();
+  
+      symbol_c *param_type = fp_iterator.param_type();
+      if (param_type == NULL) ERROR;
+  
+      switch (param_direction) {
+        case function_param_iterator_c::direction_in:
+          if (param_value == NULL) {
+            /* No value given for parameter, so we must use the default... */
+            /* First check whether default value specified in function declaration...*/
+            param_value = fp_iterator.default_value();
+          }
+          if (param_value == NULL) {
+            /* If not, get the default value of this variable's type */
+            param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
+          }
+          if (param_value == NULL) ERROR;
+          param_value->accept(*this);
+          break;
+        case function_param_iterator_c::direction_out:
+        case function_param_iterator_c::direction_inout:
+          if (param_value == NULL) {
+            /* no parameter value given, so we pass a previously declared temporary variable. */
+            std::string *temp_var_name = temp_var_name_factory.new_name();
+            s4o.print(*temp_var_name);
+            delete temp_var_name;
+          } else {
+            param_value->accept(*this);
+          }
+          break;
+        case function_param_iterator_c::direction_extref:
+          /* TODO! */
+          ERROR;
+          break;
+      } /* switch */
+    } /* for(...) */
+    // symbol->parameter_assignment->accept(*this);
+    s4o.print(")");
+  }
+
   return NULL;
 }
 
--- a/stage4/generate_cc/generate_cc_tempvardecl.cc	Tue May 29 09:45:50 2007 +0200
+++ b/stage4/generate_cc/generate_cc_tempvardecl.cc	Thu Jun 14 08:58:10 2007 +0200
@@ -114,7 +114,11 @@
         identifier_c *fcalled_name = fcall_iterator.fname();
         /* get that function's declaration... */
         function_declaration_c *fdecl = function_symtable.find_value(fcalled_name);
-        if (fdecl == function_symtable.end_value()) ERROR;
+        if (fdecl == function_symtable.end_value()) {
+          function_type_t function_type = get_function_type(fcalled_name);
+          if (function_type == function_none) ERROR;
+          return;
+        }
         /* create iterator to iterate through each of the called function's parameters... */
         function_param_iterator_c fp_iterator(fdecl);
 
--- a/stage4/generate_cc/search_base_type.cc	Tue May 29 09:45:50 2007 +0200
+++ b/stage4/generate_cc/search_base_type.cc	Thu Jun 14 08:58:10 2007 +0200
@@ -49,7 +49,7 @@
       symbol_c *type_decl = type_symtable.find_value(type_name);
       if (type_decl == type_symtable.end_value())
         /* Type declaration not found!! */
-	ERROR;
+        ERROR;
 
       return type_decl->accept(*this);
     }
--- a/stage4/generate_cc/search_constant_type.cc	Tue May 29 09:45:50 2007 +0200
+++ b/stage4/generate_cc/search_constant_type.cc	Thu Jun 14 08:58:10 2007 +0200
@@ -51,6 +51,8 @@
   static dt_type_name_c       dt_type_name;
   static tod_type_name_c      tod_type_name;
 
+  static constant_real_type_name_c     constant_real_type_name;
+  static constant_int_type_name_c      constant_int_type_name;
 
   public:
     symbol_c *get_type(symbol_c *constant) {
@@ -65,11 +67,11 @@
 /******************************/
 /* B 1.2.1 - Numeric Literals */
 /******************************/
-    void *visit(real_c *symbol) {return (void *)&real_type_name;}
-    void *visit(integer_c *symbol) {return (void *)&int_type_name;}
-    void *visit(binary_integer_c *symbol) {return (void *)&int_type_name;}
-    void *visit(octal_integer_c *symbol) {return (void *)&int_type_name;}
-    void *visit(hex_integer_c *symbol) {return (void *)&int_type_name;}
+    void *visit(real_c *symbol) {return (void *)&constant_real_type_name;}
+    void *visit(integer_c *symbol) {return (void *)&constant_int_type_name;}
+    void *visit(binary_integer_c *symbol) {return (void *)&constant_int_type_name;}
+    void *visit(octal_integer_c *symbol) {return (void *)&constant_int_type_name;}
+    void *visit(hex_integer_c *symbol) {return (void *)&constant_int_type_name;}
 
     void *visit(numeric_literal_c *symbol)
       {return (void *)((symbol->type!=NULL)?symbol->type:symbol->value->accept(*this));}
@@ -122,6 +124,8 @@
 dt_type_name_c       search_constant_type_c::dt_type_name;
 tod_type_name_c      search_constant_type_c::tod_type_name;
 
+constant_real_type_name_c     search_constant_type_c::constant_real_type_name;
+constant_int_type_name_c      search_constant_type_c::constant_int_type_name;
 
 
 
--- a/stage4/generate_cc/search_expression_type.cc	Tue May 29 09:45:50 2007 +0200
+++ b/stage4/generate_cc/search_expression_type.cc	Thu Jun 14 08:58:10 2007 +0200
@@ -31,6 +31,44 @@
  *       22.2 - 5    -> returns reference to a real_type_name_c object.
  *       etc...
  */
+
+typedef enum {function_add,
+              function_sub,
+              function_and,
+              function_or,
+              function_sqrt,
+              function_none} function_type_t;
+
+
+function_type_t get_function_type(identifier_c *function_name) {
+  if (!strcasecmp(function_name->value, "ADD"))
+    return function_add;
+  else if (!strcasecmp(function_name->value, "SUB"))
+    return function_sub;
+  else if (!strcasecmp(function_name->value, "AND"))
+    return function_and;
+  else if (!strcasecmp(function_name->value, "OR"))
+    return function_or;
+  else if (!strcasecmp(function_name->value, "SQRT"))
+    return function_sqrt;
+  else return function_none;
+}
+
+
+symbol_c *generate_param_name(const char *name) {
+  symbol_c *param_name = new identifier_c(name);
+  return param_name;
+}
+
+
+symbol_c *generate_param_name(const char *format, int number) {
+  char name[10];
+  sprintf(name, format, number);
+  symbol_c *param_name = new identifier_c(name);
+  return param_name;
+}
+
+
 class search_expression_type_c: public search_constant_type_c {
   private:
     search_varfb_instance_type_c *search_varfb_instance_type;
@@ -51,7 +89,7 @@
     }
 
     /* A helper function... */
-    bool is_time_compatible(symbol_c *type_symbol) {
+    bool is_time_type(symbol_c *type_symbol) {
       if (typeid(*type_symbol) == typeid(time_type_name_c)) {return true;}
       if (typeid(*type_symbol) == typeid(date_type_name_c)) {return true;}
       if (typeid(*type_symbol) == typeid(tod_type_name_c)) {return true;}
@@ -60,7 +98,7 @@
     }
 
     /* A helper function... */
-    bool is_numeric_compatible(symbol_c *type_symbol) {
+    bool is_integer_type(symbol_c *type_symbol) {
       if (typeid(*type_symbol) == typeid(sint_type_name_c)) {return true;}
       if (typeid(*type_symbol) == typeid(int_type_name_c)) {return true;}
       if (typeid(*type_symbol) == typeid(dint_type_name_c)) {return true;}
@@ -69,32 +107,219 @@
       if (typeid(*type_symbol) == typeid(uint_type_name_c)) {return true;}
       if (typeid(*type_symbol) == typeid(udint_type_name_c)) {return true;}
       if (typeid(*type_symbol) == typeid(ulint_type_name_c)) {return true;}
+      if (typeid(*type_symbol) == typeid(constant_int_type_name_c)) {return true;}
+      return false;
+    }
+
+    bool is_real_type(symbol_c *type_symbol) {
       if (typeid(*type_symbol) == typeid(real_type_name_c)) {return true;}
       if (typeid(*type_symbol) == typeid(lreal_type_name_c)) {return true;}
+      if (typeid(*type_symbol) == typeid(constant_real_type_name_c)) {return true;}
+      return false;
+    }
+
+    bool is_num_type(symbol_c *type_symbol) {
+      return is_real_type(type_symbol) || is_integer_type(type_symbol);
+    }
+
+    bool is_binary_type(symbol_c *type_symbol) {
       if (typeid(*type_symbol) == typeid(bool_type_name_c)) {return true;}
       if (typeid(*type_symbol) == typeid(byte_type_name_c)) {return true;}
       if (typeid(*type_symbol) == typeid(word_type_name_c)) {return true;}
       if (typeid(*type_symbol) == typeid(dword_type_name_c)) {return true;}
       if (typeid(*type_symbol) == typeid(lword_type_name_c)) {return true;}
+      if (typeid(*type_symbol) == typeid(constant_int_type_name_c)) {return true;}
       return false;
     }
     
+    bool is_same_type(symbol_c *first_type, symbol_c *second_type) {
+      if (typeid(*first_type) == typeid(*second_type)) {return true;}
+      if (is_integer_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return true;}
+      if ((typeid(*first_type) == typeid(constant_int_type_name_c) && is_integer_type(second_type))) {return true;}
+      if (is_binary_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return true;}
+      if ((typeid(*first_type) == typeid(constant_int_type_name_c) && is_binary_type(second_type))) {return true;}
+      if (is_real_type(first_type) && (typeid(*second_type) == typeid(constant_real_type_name_c))) {return true;}
+      if ((typeid(*first_type) == typeid(constant_real_type_name_c) && is_real_type(second_type))) {return true;}
+      return false;
+    }
+
+    symbol_c* common_type(symbol_c *first_type, symbol_c *second_type) {
+      if (typeid(*first_type) == typeid(*second_type)) {return first_type;}
+      if (is_integer_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return first_type;}
+      if ((typeid(*first_type) == typeid(constant_int_type_name_c) && is_integer_type(second_type))) {return second_type;}
+      if (is_binary_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return first_type;}
+      if ((typeid(*first_type) == typeid(constant_int_type_name_c) && is_binary_type(second_type))) {return second_type;}
+      if (is_real_type(first_type) && (typeid(*second_type) == typeid(constant_real_type_name_c))) {return first_type;}
+      if ((typeid(*first_type) == typeid(constant_real_type_name_c) && is_real_type(second_type))) {return second_type;}
+      return NULL;
+    }
+    
   private:
 
+  void *compute_standard_function_st(function_invocation_c *symbol) {
+    symbol_c *current_type = NULL;
+    symbol_c *return_type = NULL;
+    function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
+    
+    function_call_param_iterator_c function_call_param_iterator(symbol);
+    
+    for(int current_param = 0; current_param < ((list_c *)symbol->parameter_assignment_list)->n; current_param++) {
+      symbol_c *param_name = NULL;
+      switch (current_function_type) {
+        case function_add:
+        case function_and:
+        case function_or:
+          param_name = generate_param_name("IN%d", current_param + 1);
+          break;
+        case function_sub:
+          if (current_param < 2)
+            param_name = generate_param_name("IN%d", current_param + 1);
+          else ERROR;
+          break;
+        default: ERROR;
+      }
+      
+      /* Get the value from a foo(<param_name> = <param_value>) style call */
+      symbol_c *param_value = function_call_param_iterator.search(param_name);
+      delete param_name;
+      
+      /* Get the value from a foo(<param_value>) style call */
+      if (param_value == NULL)
+        param_value = function_call_param_iterator.next();
+      
+      if (param_value == NULL) ERROR;
+      
+      symbol_c *param_type = (symbol_c *)param_value->accept(*this);
+      
+      switch (current_function_type) {
+        case function_add:
+          if (current_param == 0)
+            current_type = param_type;
+          else if (current_param == 1) {
+            if ((is_integer_type(current_type) && is_same_type(current_type, param_type)) ||
+                (is_real_type(current_type) && is_same_type(current_type, param_type))) {
+              current_type = common_type(current_type, param_type);
+              return_type = current_type;
+            }
+            else if (is_time_type(current_type)) {
+              if ((typeid(*current_type) == typeid(time_type_name_c)) && (typeid(*param_type) == typeid(time_type_name_c))) {return_type = (symbol_c *)&time_type_name;}
+              else if (typeid(*current_type) == typeid(tod_type_name_c) && typeid(*param_type) == typeid(time_type_name_c)) {return_type = (symbol_c *)&tod_type_name;}
+              else if (typeid(*current_type) == typeid(dt_type_name_c) && typeid(*param_type) == typeid(time_type_name_c)) {return_type = (symbol_c *)&dt_type_name;}
+              else ERROR;
+            }
+            else ERROR;
+          }
+          else if (!is_time_type(current_type) && is_same_type(current_type, param_type)) {
+            current_type = common_type(current_type, param_type);
+            return_type = current_type;
+          }
+          else ERROR;
+          break;
+        case function_sub:
+          if (current_param == 0)
+            current_type = param_type;
+          else if (current_param == 1) {
+            if ((is_integer_type(current_type) && is_same_type(current_type, param_type)) ||
+                (is_real_type(current_type) && is_same_type(current_type, param_type)))
+              return_type = common_type(current_type, param_type);
+            else if (is_time_type(current_type)) {
+              if (typeid(*current_type) == typeid(time_type_name_c) && typeid(*param_type) == typeid(time_type_name_c)) {return_type = (symbol_c *)&time_type_name;}
+              else if (typeid(*current_type) == typeid(date_type_name_c) && typeid(*param_type) == typeid(date_type_name_c)) {return_type = (symbol_c *)&time_type_name;}
+              else if (typeid(*current_type) == typeid(tod_type_name_c) && typeid(*param_type) == typeid(time_type_name_c)) {return_type = (symbol_c *)&tod_type_name;}
+              else if (typeid(*current_type) == typeid(tod_type_name_c) && typeid(*param_type) == typeid(tod_type_name_c)) {return_type = (symbol_c *)&time_type_name;}
+              else if (typeid(*current_type) == typeid(dt_type_name_c) && typeid(*param_type) == typeid(time_type_name_c)) {return_type = (symbol_c *)&dt_type_name;}
+              else if (typeid(*current_type) == typeid(dt_type_name_c) && typeid(*param_type) == typeid(dt_type_name_c)) {return_type = (symbol_c *)&time_type_name;}
+              else ERROR;
+            }
+            else ERROR;
+          }
+          else ERROR;
+          break;
+        case function_and:
+        case function_or:
+          if (current_param == 0)
+            if (is_binary_type(param_type))
+              current_type = param_type;
+            else ERROR;
+          else if (is_same_type(current_type, param_type))
+            return_type = common_type(current_type, param_type);
+          else ERROR;
+          break;
+        default: ERROR;
+      }
+    }
+    
+    return (void *)return_type;
+  }
+
+  public:
+
+  void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_type) {
+    symbol_c *current_type = NULL;
+    symbol_c *return_type = NULL;
+    function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
+    if (current_function_type == function_none) ERROR;
+    
+    function_call_param_iterator_c function_call_param_iterator(symbol);
+    
+    int nb_param = 1;
+    if (symbol->il_operand_list != NULL)
+      nb_param += ((list_c *)symbol->il_operand_list)->n;
+
+    for(int current_param = 0; current_param < nb_param; current_param++) {
+      
+      if (current_param != 0) {
+        symbol_c *param_name = NULL;
+        switch (current_function_type) {
+          default: ERROR;
+        }
+        
+        /* Get the value from a foo(<param_name> = <param_value>) style call */
+        symbol_c *param_value = function_call_param_iterator.search(param_name);
+        delete param_name;
+        
+        /* Get the value from a foo(<param_value>) style call */
+        if (param_value == NULL)
+          param_value = function_call_param_iterator.next();
+        
+        if (param_value == NULL) ERROR;
+        
+        param_type = (symbol_c *)param_value->accept(*this);
+      }
+      
+      switch (current_function_type) {
+        case function_sqrt:
+          if (current_param == 0 && is_real_type(param_type))
+            return_type = param_type;
+          else ERROR;
+          break;
+        default: ERROR;
+      }
+    }
+    
+    return (void *)return_type;
+  }
+
   static bool_type_name_c bool_type_name;
 
   /* A helper function... */
   void *compute_boolean_expression(symbol_c *left_type, symbol_c *right_type) {
-    if (typeid(*left_type) == typeid(bool_type_name_c) and typeid(*right_type) == typeid(bool_type_name_c)) {return (void *)left_type;}
-    if (is_numeric_compatible(left_type) && is_numeric_compatible(right_type)) {return (void *)left_type;}
-    ERROR;
-    return NULL;
+    if (!is_same_type(left_type, right_type))
+      ERROR;
+    if (!is_bool_type(left_type) && !is_binary_type(left_type))
+      ERROR;
+    if (typeid(*left_type) == typeid(constant_int_type_name_c)) {return (void *)right_type;}
+    else {return (void *)left_type;}
   }
 
   /* A helper function... */
   void *compute_numeric_expression(symbol_c *left_type, symbol_c *right_type) {
-    if (is_numeric_compatible(left_type) && is_numeric_compatible(right_type)) {return (void *)left_type;}
-    ERROR;
+    if (!is_same_type(left_type, right_type))
+      ERROR;
+    if (!is_integer_type(left_type) && !is_real_type(left_type))
+      ERROR;
+    if ((typeid(*left_type) == typeid(constant_int_type_name_c)) || (typeid(*left_type) == typeid(constant_real_type_name_c))) {return (void *)right_type;}
+    else {return (void *)left_type;}
     return NULL;
   }
 
@@ -175,27 +400,37 @@
   void *visit(add_expression_c *symbol) {
     symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
     symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
-    if (is_time_compatible(left_type) && is_time_compatible(right_type)) {return left_type;}
+    if (typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&time_type_name;}
+    if (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&tod_type_name;}
+    if (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&dt_type_name;}
     return compute_numeric_expression(left_type, right_type);
   }
 
   void *visit(sub_expression_c *symbol) {
     symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
     symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
-    if (is_time_compatible(left_type) && is_time_compatible(right_type)) {return left_type;}
+    if (typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&time_type_name;}
+    if (typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) {return (void *)&time_type_name;}
+    if (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&tod_type_name;}
+    if (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) {return (void *)&time_type_name;}
+    if (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) {return (void *)&dt_type_name;}
+    if (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c)) {return (void *)&time_type_name;}
     return compute_numeric_expression(left_type, right_type);
   }
   
   void *visit(mul_expression_c *symbol) {
     symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
     symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
-    if (is_time_compatible(left_type) && is_time_compatible(right_type)) {return left_type;}
+    if (typeid(*left_type) == typeid(time_type_name_c) && is_integer_type(right_type)) {return (void *)&time_type_name;}
+    if (typeid(*left_type) == typeid(time_type_name_c) && is_real_type(right_type)) {return (void *)&time_type_name;}
     return compute_numeric_expression(left_type, right_type);
   }
   
   void *visit(div_expression_c *symbol) {
     symbol_c *left_type = base_type((symbol_c *)symbol->l_exp->accept(*this));
     symbol_c *right_type = base_type((symbol_c *)symbol->r_exp->accept(*this));
+    if (typeid(*left_type) == typeid(time_type_name_c) && is_integer_type(right_type)) {return (void *)&time_type_name;}
+    if (typeid(*left_type) == typeid(time_type_name_c) && is_real_type(right_type)) {return (void *)&time_type_name;}
     return compute_numeric_expression(left_type, right_type);
   }
 
@@ -255,9 +490,12 @@
   void *visit(function_invocation_c *symbol) {
 	  function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
 	
-	  if (f_decl == function_symtable.end_value())
-	    /* should never occur. The function being called MUST be in the symtable... */
-	    ERROR;
+	  if (f_decl == function_symtable.end_value()) {
+      void *res = compute_standard_function_st(symbol);
+	    if (res == NULL)
+        ERROR;
+      return res;
+    }
 
     return base_type(f_decl->type_name);
   }
--- a/stage4/generate_iec/generate_iec.cc	Tue May 29 09:45:50 2007 +0200
+++ b/stage4/generate_iec/generate_iec.cc	Thu Jun 14 08:58:10 2007 +0200
@@ -292,7 +292,8 @@
 void *visit(dword_type_name_c *symbol) {s4o.print("DWORD"); return NULL;}
 void *visit(string_type_name_c *symbol) {s4o.print("STRING"); return NULL;}
 void *visit(wstring_type_name_c *symbol) {s4o.print("WSTRING"); return NULL;}
-
+void *visit(constant_int_type_name_c *symbol) {return NULL;}
+void *visit(constant_real_type_name_c *symbol) {return NULL;}
 
 
 /********************************/