absyntax_utils/search_expression_type.cc
changeset 205 96d8b6e006f0
parent 202 da1a8186f86f
child 208 c72748a12ae3
child 257 90782e241346
equal deleted inserted replaced
200:fde2d08ebaee 205:96d8b6e006f0
    63 }
    63 }
    64 
    64 
    65 /* A helper function... */
    65 /* A helper function... */
    66 bool search_expression_type_c::is_string_type(symbol_c *type_symbol) {
    66 bool search_expression_type_c::is_string_type(symbol_c *type_symbol) {
    67   if (type_symbol == NULL) {return true;}
    67   if (type_symbol == NULL) {return true;}
    68   if (typeid(*type_symbol) == typeid(string_type_name_c)) {return true;}
    68   if (typeid(*type_symbol) == typeid(string_type_name_c))  {return true;}
    69   if (typeid(*type_symbol) == typeid(wstring_type_name_c)) {return true;}
    69   if (typeid(*type_symbol) == typeid(wstring_type_name_c)) {return true;}
    70   return false;
    70   return false;
    71 }
    71 }
    72 
    72 
    73 /* A helper function... */
    73 /* A helper function... */
       
    74 bool search_expression_type_c::is_literal_integer_type(symbol_c *type_symbol) {
       
    75   if (type_symbol == NULL) {return true;}
       
    76   if (typeid(*type_symbol) == typeid(integer_c))        {return true;}
       
    77   if (typeid(*type_symbol) == typeid(binary_integer_c)) {return true;}
       
    78   if (typeid(*type_symbol) == typeid(octal_integer_c))  {return true;}
       
    79   if (typeid(*type_symbol) == typeid(hex_integer_c))    {return true;}
       
    80   return false;
       
    81 }
       
    82 
       
    83 /* A helper function... */
    74 bool search_expression_type_c::is_integer_type(symbol_c *type_symbol) {
    84 bool search_expression_type_c::is_integer_type(symbol_c *type_symbol) {
    75   if (type_symbol == NULL) {return true;}
    85   if (type_symbol == NULL) {return true;}
    76   if (typeid(*type_symbol) == typeid(sint_type_name_c)) {return true;}
    86   if (typeid(*type_symbol) == typeid(sint_type_name_c))  {return true;}
    77   if (typeid(*type_symbol) == typeid(int_type_name_c)) {return true;}
    87   if (typeid(*type_symbol) == typeid(int_type_name_c))   {return true;}
    78   if (typeid(*type_symbol) == typeid(dint_type_name_c)) {return true;}
    88   if (typeid(*type_symbol) == typeid(dint_type_name_c))  {return true;}
    79   if (typeid(*type_symbol) == typeid(lint_type_name_c)) {return true;}
    89   if (typeid(*type_symbol) == typeid(lint_type_name_c))  {return true;}
    80   if (typeid(*type_symbol) == typeid(usint_type_name_c)) {return true;}
    90   if (typeid(*type_symbol) == typeid(usint_type_name_c)) {return true;}
    81   if (typeid(*type_symbol) == typeid(uint_type_name_c)) {return true;}
    91   if (typeid(*type_symbol) == typeid(uint_type_name_c))  {return true;}
    82   if (typeid(*type_symbol) == typeid(udint_type_name_c)) {return true;}
    92   if (typeid(*type_symbol) == typeid(udint_type_name_c)) {return true;}
    83   if (typeid(*type_symbol) == typeid(ulint_type_name_c)) {return true;}
    93   if (typeid(*type_symbol) == typeid(ulint_type_name_c)) {return true;}
    84   if (typeid(*type_symbol) == typeid(constant_int_type_name_c)) {return true;}
    94   return is_literal_integer_type(type_symbol);
    85   return false;
    95 }
    86 }
    96 
    87 
    97 /* A helper function... */
       
    98 bool search_expression_type_c::is_literal_real_type(symbol_c *type_symbol) {
       
    99   if (type_symbol == NULL) {return true;}
       
   100   if (typeid(*type_symbol) == typeid(real_c)) {return true;}
       
   101   return false;
       
   102 }
       
   103 
       
   104 /* A helper function... */
    88 bool search_expression_type_c::is_real_type(symbol_c *type_symbol) {
   105 bool search_expression_type_c::is_real_type(symbol_c *type_symbol) {
    89   if (type_symbol == NULL) {return true;}
   106   if (type_symbol == NULL) {return true;}
    90   if (typeid(*type_symbol) == typeid(real_type_name_c)) {return true;}
   107   if (typeid(*type_symbol) == typeid(real_type_name_c)) {return true;}
    91   if (typeid(*type_symbol) == typeid(lreal_type_name_c)) {return true;}
   108   if (typeid(*type_symbol) == typeid(lreal_type_name_c)) {return true;}
    92   if (typeid(*type_symbol) == typeid(constant_real_type_name_c)) {return true;}
   109   return is_literal_real_type(type_symbol);
    93   return false;
       
    94 }
   110 }
    95 
   111 
    96 bool search_expression_type_c::is_num_type(symbol_c *type_symbol) {
   112 bool search_expression_type_c::is_num_type(symbol_c *type_symbol) {
    97   if (type_symbol == NULL) {return true;}
   113   if (type_symbol == NULL) {return true;}
    98   return is_real_type(type_symbol) || is_integer_type(type_symbol);
   114   return is_real_type(type_symbol) || is_integer_type(type_symbol);
   102   if (type_symbol == NULL) {return true;}
   118   if (type_symbol == NULL) {return true;}
   103   if (typeid(*type_symbol) == typeid(byte_type_name_c)) {return true;}
   119   if (typeid(*type_symbol) == typeid(byte_type_name_c)) {return true;}
   104   if (typeid(*type_symbol) == typeid(word_type_name_c)) {return true;}
   120   if (typeid(*type_symbol) == typeid(word_type_name_c)) {return true;}
   105   if (typeid(*type_symbol) == typeid(dword_type_name_c)) {return true;}
   121   if (typeid(*type_symbol) == typeid(dword_type_name_c)) {return true;}
   106   if (typeid(*type_symbol) == typeid(lword_type_name_c)) {return true;}
   122   if (typeid(*type_symbol) == typeid(lword_type_name_c)) {return true;}
   107   if (typeid(*type_symbol) == typeid(constant_int_type_name_c)) {return true;}
   123   return is_literal_integer_type(type_symbol);
   108   return false;
       
   109 }
   124 }
   110 
   125 
   111 bool search_expression_type_c::is_binary_type(symbol_c *type_symbol) {
   126 bool search_expression_type_c::is_binary_type(symbol_c *type_symbol) {
   112   if (type_symbol == NULL) {return true;}
   127   if (type_symbol == NULL) {return true;}
   113   if (typeid(*type_symbol) == typeid(bool_type_name_c)) {return true;}
   128   if (typeid(*type_symbol) == typeid(bool_type_name_c)) {return true;}
   115 }
   130 }
   116 
   131 
   117 bool search_expression_type_c::is_same_type(symbol_c *first_type, symbol_c *second_type) {
   132 bool search_expression_type_c::is_same_type(symbol_c *first_type, symbol_c *second_type) {
   118   if (first_type == NULL || second_type == NULL) {return true;}
   133   if (first_type == NULL || second_type == NULL) {return true;}
   119   if (typeid(*first_type) == typeid(*second_type)) {return true;}
   134   if (typeid(*first_type) == typeid(*second_type)) {return true;}
   120   if (is_integer_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return true;}
   135   if (is_integer_type(first_type) && is_literal_integer_type(second_type)) {return true;}
   121   if ((typeid(*first_type) == typeid(constant_int_type_name_c) && is_integer_type(second_type))) {return true;}
   136   if (is_literal_integer_type(first_type) && is_integer_type(second_type)) {return true;}
   122   if (is_binary_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return true;}
   137   if (is_binary_type(first_type) && is_literal_integer_type(second_type))  {return true;}
   123   if ((typeid(*first_type) == typeid(constant_int_type_name_c) && is_binary_type(second_type))) {return true;}
   138   if (is_literal_integer_type(first_type) && is_binary_type(second_type))  {return true;}
   124   if (is_real_type(first_type) && (typeid(*second_type) == typeid(constant_real_type_name_c))) {return true;}
   139   if (is_real_type(first_type) && is_literal_real_type(second_type)) {return true;}
   125   if ((typeid(*first_type) == typeid(constant_real_type_name_c) && is_real_type(second_type))) {return true;}
   140   if (is_literal_real_type(first_type) && is_real_type(second_type)) {return true;}
   126   return false;
   141   return false;
   127 }
   142 }
   128 
   143 
   129 symbol_c* search_expression_type_c::common_type(symbol_c *first_type, symbol_c *second_type) {
   144 symbol_c* search_expression_type_c::common_type(symbol_c *first_type, symbol_c *second_type) {
   130   if (first_type == NULL && second_type == NULL) {return NULL;}
   145   if (first_type == NULL && second_type == NULL) {return NULL;}
   131   if (first_type == NULL) {return second_type;}
   146   if (first_type == NULL) {return second_type;}
   132   if (second_type == NULL) {return first_type;}
   147   if (second_type == NULL) {return first_type;}
   133   if (typeid(*first_type) == typeid(*second_type)) {return first_type;}
   148   if (typeid(*first_type) == typeid(*second_type)) {return first_type;}
   134   if (is_integer_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c))) {return first_type;}
   149   if (is_integer_type(first_type) && is_literal_integer_type(second_type)) {return first_type;}
   135   if ((typeid(*first_type) == typeid(constant_int_type_name_c)) && is_integer_type(second_type)) {return second_type;}
   150   if (is_literal_integer_type(first_type) && is_integer_type(second_type)) {return second_type;}
   136   if (is_binary_type(first_type) && (typeid(*second_type) == typeid(constant_int_type_name_c)))  {return first_type;}
   151   if (is_binary_type(first_type) && is_literal_integer_type(second_type))  {return first_type;}
   137   if ((typeid(*first_type) == typeid(constant_int_type_name_c)) && is_binary_type(second_type)) {return second_type;}
   152   if (is_literal_integer_type(first_type) && is_binary_type(second_type))  {return second_type;}
   138   if (is_real_type(first_type) && (typeid(*second_type) == typeid(constant_real_type_name_c))) {return first_type;}
   153   if (is_real_type(first_type) && is_literal_real_type(second_type)) {return first_type;}
   139   if ((typeid(*first_type) == typeid(constant_real_type_name_c)) && is_real_type(second_type)) {return second_type;}
   154   if (is_literal_real_type(first_type) && is_real_type(second_type)) {return second_type;}
   140   return NULL;
   155   return NULL;
   141 }
   156 }
   142 
   157 
       
   158 
       
   159 integer_c search_expression_type_c::integer("1"); // what default value should we use here ???
   143 #define compute_standard_function_default search_expression_type_c::compute_standard_function_default
   160 #define compute_standard_function_default search_expression_type_c::compute_standard_function_default
   144 #define compute_standard_function_il search_expression_type_c::compute_standard_function_il
   161 #define compute_standard_function_il search_expression_type_c::compute_standard_function_il
       
   162 #define search(x) search_f(x)
       
   163 #define next() next_nf()
       
   164 //     #define search_constant_type_c::constant_int_type_name  search_expression_type_c::integer
       
   165 #define constant_int_type_name  integer
   145 #include "search_type_code.c"
   166 #include "search_type_code.c"
       
   167 #undef constant_int_type_name
       
   168 //     #undef search_constant_type_c::constant_int_type_name
       
   169 #undef next
       
   170 #undef search
   146 #undef compute_standard_function_default
   171 #undef compute_standard_function_default
   147 #undef compute_standard_function_il
   172 #undef compute_standard_function_il
   148 
   173 
   149 /*static bool_type_name_c bool_type_name;*/
   174 /*static bool_type_name_c bool_type_name;*/
   150 
   175 
   152 void *search_expression_type_c::compute_boolean_expression(symbol_c *left_type, symbol_c *right_type) {
   177 void *search_expression_type_c::compute_boolean_expression(symbol_c *left_type, symbol_c *right_type) {
   153   if (!is_same_type(left_type, right_type))
   178   if (!is_same_type(left_type, right_type))
   154     ERROR;
   179     ERROR;
   155   if (!is_bool_type(left_type) && !is_binary_type(left_type))
   180   if (!is_bool_type(left_type) && !is_binary_type(left_type))
   156     ERROR;
   181     ERROR;
   157   if (typeid(*left_type) == typeid(constant_int_type_name_c)) {return (void *)right_type;}
   182   if (is_literal_integer_type(left_type)) {return (void *)right_type;}
   158   else {return (void *)left_type;}
   183   else {return (void *)left_type;}
   159 }
   184 }
   160 
   185 
   161 /* A helper function... */
   186 /* A helper function... */
   162 void *search_expression_type_c::compute_numeric_expression(symbol_c *left_type, symbol_c *right_type) {
   187 void *search_expression_type_c::compute_numeric_expression(symbol_c *left_type, symbol_c *right_type) {
   163   if (!is_same_type(left_type, right_type))
   188   if (!is_same_type(left_type, right_type))
   164     ERROR;
   189     ERROR;
   165   if (!is_integer_type(left_type) && !is_real_type(left_type))
   190   if (!is_integer_type(left_type) && !is_real_type(left_type))
   166     ERROR;
   191     ERROR;
   167   if ((typeid(*left_type) == typeid(constant_int_type_name_c)) || (typeid(*left_type) == typeid(constant_real_type_name_c))) {return (void *)right_type;}
   192   if (is_literal_integer_type(left_type) || is_literal_real_type(left_type)) {return (void *)right_type;}
   168   else {return (void *)left_type;}
   193   else {return (void *)left_type;}
   169   return NULL;
   194   return NULL;
   170 }
   195 }
   171 
   196 
   172 /* a helper function... */
   197 /* a helper function... */