Fixed some issues on:
authorlbessard
Wed, 25 Jun 2008 18:47:07 +0200
changeset 123 a9b4af71cfa4
parent 122 9e57c6d79398
child 124 4debc565109d
Fixed some issues on:
- parsing string compare symbols
- retricting operation available on enumerated types
stage4/generate_c/generate_c_il.cc
stage4/generate_c/generate_c_st.cc
stage4/generate_c/generate_c_typedecl.cc
stage4/generate_c/search_base_type.cc
stage4/generate_c/search_expression_type.cc
--- a/stage4/generate_c/generate_c_il.cc	Mon Jun 23 18:02:09 2008 +0200
+++ b/stage4/generate_c/generate_c_il.cc	Wed Jun 25 18:47:07 2008 +0200
@@ -821,9 +821,7 @@
     symbol_c *param_type = fp_iterator.param_type();
     if (param_type == NULL) ERROR;
     
-    search_base_type.explore_type(param_type);    
-
-    /* now output the value assignment */
+        /* now output the value assignment */
     if (param_value != NULL)
       if ((param_direction == function_param_iterator_c::direction_in) ||
           (param_direction == function_param_iterator_c::direction_inout)) {
@@ -831,13 +829,13 @@
         s4o.print(".");
         param_name->accept(*this);
         s4o.print(" = ");
-        if (search_base_type.base_is_subrange()) {
+        if (search_base_type.type_is_subrange(param_type)) {
           s4o.print("__CHECK_");
           param_type->accept(*this);
           s4o.print("(");
         }
         param_value->accept(*this);
-        if (search_base_type.base_is_subrange())
+        if (search_base_type.type_is_subrange(param_type))
           s4o.print(")");
         s4o.print(";\n" + s4o.indent_spaces);
       }
@@ -870,12 +868,11 @@
       if ((param_direction == function_param_iterator_c::direction_out) ||
           (param_direction == function_param_iterator_c::direction_inout)) {
         symbol_c *param_type = search_varfb_instance_type->get_type(param_value, false);
-        search_base_type.explore_type(param_type);
         
         s4o.print(";\n"+ s4o.indent_spaces);
         param_value->accept(*this);
         s4o.print(" = ");
-        if (search_base_type.base_is_subrange()) {
+        if (search_base_type.type_is_subrange(param_type)) {
           s4o.print("__CHECK_");
           param_type->accept(*this);
           s4o.print("(");
@@ -883,7 +880,7 @@
         symbol->fb_name->accept(*this);
         s4o.print(".");
         param_name->accept(*this);
-        if (search_base_type.base_is_subrange())
+        if (search_base_type.type_is_subrange(param_type))
           s4o.print(")");
       }
   } /* for(...) */
@@ -944,8 +941,6 @@
     if (param_value == NULL)
       param_value = function_call_param_iterator.next();
 
-    search_base_type.explore_type(param_type);
-
     switch (param_direction) {
       case function_param_iterator_c::direction_in:
         if (param_value == NULL) {
@@ -958,13 +953,13 @@
           param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
         }
         if (param_value == NULL) ERROR;
-        if (search_base_type.base_is_subrange()) {
+        if (search_base_type.type_is_subrange(param_type)) {
           s4o.print("__CHECK_");
           param_type->accept(*this);
           s4o.print("(");
         }
         param_value->accept(*this);
-        if (search_base_type.base_is_subrange())
+        if (search_base_type.type_is_subrange(param_type))
           s4o.print(")");
 	break;
       case function_param_iterator_c::direction_out:
@@ -1132,17 +1127,16 @@
 
 void *visit(ST_operator_c *symbol)	{
   symbol_c *operand_type = search_varfb_instance_type->get_type(this->current_operand, false);
-  search_base_type.explore_type(operand_type);
-
+  
   this->current_operand->accept(*this);
   s4o.print(" = ");
-  if (search_base_type.base_is_subrange()) {
+  if (search_base_type.type_is_subrange(operand_type)) {
     s4o.print("__CHECK_");
     operand_type->accept(*this);
     s4o.print("(");
   }
   this->default_variable_name.accept(*this);
-  if (search_base_type.base_is_subrange())
+  if (search_base_type.type_is_subrange(operand_type))
     s4o.print(")");
   /* the data type resulting from this operation is unchamged. */
   return NULL;
@@ -1150,11 +1144,10 @@
 
 void *visit(STN_operator_c *symbol)	{
   symbol_c *operand_type = search_varfb_instance_type->get_type(this->current_operand, false);
-  search_base_type.explore_type(operand_type);
-
+  
   this->current_operand->accept(*this);
   s4o.print(" = ");
-  if (search_base_type.base_is_subrange()) {
+  if (search_base_type.type_is_subrange(operand_type)) {
     s4o.print("__CHECK_");
     operand_type->accept(*this);
     s4o.print("(");
@@ -1164,7 +1157,7 @@
   else
     s4o.print("~");
   this->default_variable_name.accept(*this);
-  if (search_base_type.base_is_subrange())
+  if (search_base_type.type_is_subrange(operand_type))
     s4o.print(")");
   /* the data type resulting from this operation is unchamged. */
   return NULL;
@@ -1362,27 +1355,49 @@
 }
 
 void *visit(GT_operator_c *symbol)	{
-  return CMP_operator(this->current_operand, "__gt_");
+  if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
+    return CMP_operator(this->current_operand, "__gt_");
+  ERROR;
+  return NULL;
 }
 
 void *visit(GE_operator_c *symbol)	{
-  return CMP_operator(this->current_operand, "__ge_");
+  if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
+    return CMP_operator(this->current_operand, "__ge_");
+  ERROR;
+  return NULL;
 }
 
 void *visit(EQ_operator_c *symbol)	{
-  return CMP_operator(this->current_operand, "__eq_");
+  if (search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
+    return CMP_operator(this->current_operand, "__eq_");
+  ERROR;
+  return NULL;
 }
 
 void *visit(LT_operator_c *symbol)	{
-  return CMP_operator(this->current_operand, "__lt_");
+  if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
+    return CMP_operator(this->current_operand, "__lt_");
+  ERROR;
+  return NULL;
 }
 
 void *visit(LE_operator_c *symbol)	{
-  return CMP_operator(this->current_operand, "__le_");
+  if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
+      search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
+    return CMP_operator(this->current_operand, "__le_");
+  ERROR;
+  return NULL;
 }
 
 void *visit(NE_operator_c *symbol)	{
-  return CMP_operator(this->current_operand, "__ne_");
+  if (search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type))
+    return CMP_operator(this->current_operand, "__ne_");
+  ERROR;
+  return NULL;
 }
 
 
--- a/stage4/generate_c/generate_c_st.cc	Mon Jun 23 18:02:09 2008 +0200
+++ b/stage4/generate_c/generate_c_st.cc	Wed Jun 25 18:47:07 2008 +0200
@@ -252,7 +252,8 @@
   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))
+  if (search_expression_type->is_time_type(left_type) ||
+      search_expression_type->is_string_type(left_type))
     return print_compare_function("__eq_", left_type, symbol->l_exp, symbol->r_exp);
   return print_binary_expression(symbol->l_exp, symbol->r_exp, " == ");
 }
@@ -262,7 +263,8 @@
   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))
+  if (search_expression_type->is_time_type(left_type) ||
+      search_expression_type->is_string_type(left_type))
     return print_compare_function("__ne_", left_type, symbol->l_exp, symbol->r_exp);
   return print_binary_expression(symbol->l_exp, symbol->r_exp, " != ");
 }
@@ -272,9 +274,13 @@
   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))
+  if (search_expression_type->is_time_type(left_type) ||
+      search_expression_type->is_string_type(left_type))
     return print_compare_function("__lt_", left_type, symbol->l_exp, symbol->r_exp);
-  return print_binary_expression(symbol->l_exp, symbol->r_exp, " < ");
+  if (!search_base_type.type_is_enumerated(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " < ");
+  ERROR;
+  return NULL;
 }
 
 void *visit(gt_expression_c *symbol) {
@@ -282,9 +288,13 @@
   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))
+  if (search_expression_type->is_time_type(left_type) ||
+      search_expression_type->is_string_type(left_type))
     return print_compare_function("__gt_", left_type, symbol->l_exp, symbol->r_exp);
-  return print_binary_expression(symbol->l_exp, symbol->r_exp, " > ");
+  if (!search_base_type.type_is_enumerated(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " > ");
+  ERROR;
+  return NULL;
 }
 
 void *visit(le_expression_c *symbol) {
@@ -292,9 +302,13 @@
   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))
+  if (search_expression_type->is_time_type(left_type) ||
+      search_expression_type->is_string_type(left_type))
     return print_compare_function("__le_", left_type, symbol->l_exp, symbol->r_exp);
-  return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= ");
+  if (!search_base_type.type_is_enumerated(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= ");
+  ERROR;
+  return NULL;
 }
 
 void *visit(ge_expression_c *symbol) {
@@ -302,9 +316,13 @@
   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))
+  if (search_expression_type->is_time_type(left_type) ||
+      search_expression_type->is_string_type(left_type))
     return print_compare_function("__ge_", left_type, symbol->l_exp, symbol->r_exp);
-  return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= ");
+  if (!search_base_type.type_is_enumerated(left_type))
+    return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= ");
+  ERROR;
+  return NULL;
 }
 
 void *visit(add_expression_c *symbol) {
@@ -384,13 +402,20 @@
 
 /* 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, " -");}
+void *visit(neg_expression_c *symbol) {
+  symbol_c *exp_type = search_expression_type->get_type(symbol->exp);
+  if (search_expression_type->is_integer_type(exp_type) || search_expression_type->is_real_type(exp_type))
+    return print_unary_expression(symbol->exp, " -");
+  ERROR;
+  return NULL;
+}
 
 void *visit(not_expression_c *symbol) {
   symbol_c *exp_type = search_expression_type->get_type(symbol->exp);
-  if (!search_expression_type->is_binary_type(exp_type))
-     ERROR;
-  return print_unary_expression(symbol->exp, search_expression_type->is_bool_type(exp_type)?"!":"~");
+  if (search_expression_type->is_binary_type(exp_type))
+    return print_unary_expression(symbol->exp, search_expression_type->is_bool_type(exp_type)?"!":"~");
+  ERROR;
+  return NULL;
 }
 
 void *visit(function_invocation_c *symbol) {
@@ -545,8 +570,6 @@
   
       symbol_c *param_type = fp_iterator.param_type();
       if (param_type == NULL) ERROR;
-  
-      search_base_type.explore_type(param_type);
         
       switch (param_direction) {
         case function_param_iterator_c::direction_in:
@@ -560,13 +583,13 @@
             param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
           }
           if (param_value == NULL) ERROR;
-          if (search_base_type.base_is_subrange()) {
+          if (search_base_type.type_is_subrange(param_type)) {
             s4o.print("__CHECK_");
             param_type->accept(*this);
             s4o.print("(");
           }
           param_value->accept(*this);
-          if (search_base_type.base_is_subrange())
+          if (search_base_type.type_is_subrange(param_type))
             s4o.print(")");
           break;
         case function_param_iterator_c::direction_out:
@@ -606,17 +629,16 @@
 /*********************************/
 void *visit(assignment_statement_c *symbol) {
   symbol_c *left_type = search_varfb_instance_type->get_type(symbol->l_exp, false);
-  symbol_c *base_type = (symbol_c *)search_base_type.explore_type(left_type);
   
   symbol->l_exp->accept(*this);
   s4o.print(" = ");
-  if (search_base_type.base_is_subrange()) {
+  if (search_base_type.type_is_subrange(left_type)) {
     s4o.print("__CHECK_");
     left_type->accept(*this);
     s4o.print("(");
   }
   symbol->r_exp->accept(*this);
-  if (search_base_type.base_is_subrange())
+  if (search_base_type.type_is_subrange(left_type))
     s4o.print(")");
   return NULL;
 }
@@ -659,8 +681,6 @@
     symbol_c *param_type = fp_iterator.param_type();
     if (param_type == NULL) ERROR;
     
-    search_base_type.explore_type(param_type);
-
     /* now output the value assignment */
     if (param_value != NULL)
       if ((param_direction == function_param_iterator_c::direction_in) ||
@@ -670,13 +690,13 @@
         s4o.print(".");
         param_name->accept(*this);
         s4o.print(" = ");
-        if (search_base_type.base_is_subrange()) {
+        if (search_base_type.type_is_subrange(param_type)) {
           s4o.print("__CHECK_");
           param_type->accept(*this);
           s4o.print("(");
         }
         param_value->accept(*this);
-        if (search_base_type.base_is_subrange())
+        if (search_base_type.type_is_subrange(param_type))
           s4o.print(")");
         s4o.print(";\n" + s4o.indent_spaces);
       }
@@ -710,12 +730,11 @@
       if ((param_direction == function_param_iterator_c::direction_out) ||
           (param_direction == function_param_iterator_c::direction_inout)) {
         symbol_c *param_type = search_varfb_instance_type->get_type(param_value, false);
-        search_base_type.explore_type(param_type);
         
         s4o.print(";\n"+ s4o.indent_spaces);
         param_value->accept(*this);
         s4o.print(" = ");
-        if (search_base_type.base_is_subrange()) {
+        if (search_base_type.type_is_subrange(param_type)) {
           s4o.print("__CHECK_");
           param_type->accept(*this);
           s4o.print("(");
@@ -724,7 +743,7 @@
         symbol->fb_name->accept(*this);
         s4o.print(".");
         param_name->accept(*this);
-        if (search_base_type.base_is_subrange())
+        if (search_base_type.type_is_subrange(param_type))
           s4o.print(")");
       }
   } /* for(...) */
--- a/stage4/generate_c/generate_c_typedecl.cc	Mon Jun 23 18:02:09 2008 +0200
+++ b/stage4/generate_c/generate_c_typedecl.cc	Wed Jun 25 18:47:07 2008 +0200
@@ -83,7 +83,6 @@
     typedef enum {
       none_bd,
       subrangebasetype_bd,
-      subrangebasetypeexploration_bd,
       subrangetest_bd,
       arraybasetype_bd,
       arraybasetypeincl_bd,
@@ -206,10 +205,6 @@
   symbol->subrange_type_name->accept(*basedecl);
   s4o_incl.print(";\n\n");
   
-  current_basetypedeclaration = subrangebasetypeexploration_bd;
-  symbol->subrange_spec_init->accept(*this);
-  current_basetypedeclaration = none_bd;
-  
   current_type_name = symbol->subrange_type_name;
   
   current_basetypedeclaration = subrangetest_bd;
@@ -234,9 +229,6 @@
     case subrangebasetype_bd:
       symbol->integer_type_name->accept(*basedecl);
       break;
-    case subrangebasetypeexploration_bd:
-      search_base_type.explore_type(symbol->integer_type_name);
-      break;
     case subrangetest_bd:
       if (symbol->subrange != NULL) {
         current_type_name->accept(*this);
@@ -247,7 +239,7 @@
         s4o.print(" value) {\n");
         s4o.indent_right();
         
-        if (search_base_type.base_is_subrange()) {
+        if (search_base_type.type_is_subrange(symbol->integer_type_name)) {
           s4o.print(s4o.indent_spaces + "value = __CHECK_");
           symbol->integer_type_name->accept(*this);
           s4o.print("(value);\n");
@@ -365,8 +357,7 @@
   current_basetypedeclaration = none_bd;
   s4o_incl.print(";\n");
   
-  search_base_type.explore_type(symbol->array_spec_init);
-  if (search_base_type.base_is_subrange()) {
+  if (search_base_type.type_is_subrange(current_type_name)) {
     s4o.print("#define __CHECK_");
     symbol->identifier->accept(*this);
     s4o.print(" __CHECK_");
--- a/stage4/generate_c/search_base_type.cc	Mon Jun 23 18:02:09 2008 +0200
+++ b/stage4/generate_c/search_base_type.cc	Wed Jun 25 18:47:07 2008 +0200
@@ -39,11 +39,11 @@
   private:
     symbol_c *current_type_name;
     bool is_subrange;
+    bool is_enumerated;
 
   public:
     search_base_type_c(void) {current_type_name = NULL;}
-    bool base_is_subrange() {return this->is_subrange;}
-
+    
   public:
     void *visit(identifier_c *type_name) {
       this->current_type_name = type_name;
@@ -56,11 +56,18 @@
       return type_decl->accept(*this);
     }
     
-    void *explore_type(symbol_c* type_decl) {
+    bool type_is_subrange(symbol_c* type_decl) {
       this->is_subrange = false;
-      return type_decl->accept(*this);
-    }
-
+      type_decl->accept(*this);
+      return this->is_subrange;
+    }
+
+    bool type_is_enumerated(symbol_c* type_decl) {
+      this->is_enumerated = false;
+      type_decl->accept(*this);
+      return this->is_enumerated;
+    }
+    
 /***********************************/
 /* B 1.3.1 - Elementary Data Types */
 /***********************************/
@@ -127,6 +134,7 @@
 
 /* enumerated_specification ASSIGN enumerated_value */
     void *visit(enumerated_spec_init_c *symbol) {
+      this->is_enumerated = true;
       return symbol->enumerated_specification->accept(*this);
     }
 
--- a/stage4/generate_c/search_expression_type.cc	Mon Jun 23 18:02:09 2008 +0200
+++ b/stage4/generate_c/search_expression_type.cc	Wed Jun 25 18:47:07 2008 +0200
@@ -103,6 +103,13 @@
     }
 
     /* A helper function... */
+    bool is_string_type(symbol_c *type_symbol) {
+      if (typeid(*type_symbol) == typeid(string_type_name_c)) {return true;}
+      if (typeid(*type_symbol) == typeid(wstring_type_name_c)) {return true;}
+      return false;
+    }
+
+    /* A helper function... */
     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;}
@@ -464,8 +471,8 @@
   void *visit(neg_expression_c *symbol) {
     symbol_c *exp_type = base_type((symbol_c *)symbol->exp->accept(*this));
     if (is_num_type(exp_type) || typeid(*exp_type) == typeid(time_type_name_c)){
-            return (void *)exp_type;
-         }
+        return (void *)exp_type;
+    }
     ERROR;
     return NULL;
   }