create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
authorMario de Sousa <msousa@fe.up.pt>
Wed, 06 Jun 2012 13:28:50 +0100
changeset 567 e5deeb6d4d2f
parent 566 5688fa07f89a
child 568 5f79478142d7
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
absyntax_utils/absyntax_utils.cc
absyntax_utils/absyntax_utils.hh
stage3/constant_folding.cc
--- a/absyntax_utils/absyntax_utils.cc	Wed Jun 06 00:20:06 2012 +0200
+++ b/absyntax_utils/absyntax_utils.cc	Wed Jun 06 13:28:50 2012 +0100
@@ -116,6 +116,9 @@
   return atoll(str.c_str());
 }
 
+
+/* extract the value of an hex integer from an hex_integer_c object !! */
+/* NOTE: it must ignore underscores! */
 uint64_t extract_hex_value(symbol_c *sym) {
   std::string str = "";
   char *endptr;
@@ -125,7 +128,7 @@
   if ((hex_integer = dynamic_cast<hex_integer_c *>(sym)) == NULL) ERROR;
   for(unsigned int i = 3; i < strlen(hex_integer->value); i++)
     if (hex_integer->value[i] != '_') str += hex_integer->value[i];
-  errno = 0;    /* To distinguish success/failure after call */
+    
   ret = strtoull(str.c_str(), &endptr, 16);
   if (errno != 0) ERROR;
 
@@ -133,6 +136,25 @@
 }
 
 
+/* extract the value of a real from an real_c object !! */
+/* NOTE: it must ignore underscores! */
+double extract_real_value(symbol_c *sym) {
+  std::string str = "";
+  char *endptr;
+  real_c * real_sym;
+  uint64_t ret;
+
+  if ((real_sym = dynamic_cast<real_c *>(sym)) == NULL) ERROR;
+  for(unsigned int i = 3; i < strlen(real_sym->value); i++)
+    if (real_sym->value[i] != '_') str += real_sym->value[i];
+    
+  ret = strtod(str.c_str(), NULL);
+  if (errno != 0) ERROR;
+
+  return ret;
+}
+
+
 
 /***********************************************************************/
 /***********************************************************************/
--- a/absyntax_utils/absyntax_utils.hh	Wed Jun 06 00:20:06 2012 +0200
+++ b/absyntax_utils/absyntax_utils.hh	Wed Jun 06 13:28:50 2012 +0100
@@ -55,10 +55,10 @@
 /* returns 0 if the names are equal!! Case is ignored. */
 int compare_identifiers(symbol_c *ident1, symbol_c *ident2);
 
-/* extract the value of an integer from an integer_c object !! */
-long long extract_integer_value(symbol_c *integer);
-
-uint64_t extract_hex_value(symbol_c *sym);
+/* extract the value of an integer/hex_integer/real from an integer_c/hex_integer_c/real_c symbol !! */
+long long extract_integer_value(symbol_c *sym);
+uint64_t  extract_hex_value    (symbol_c *sym);
+double    extract_real_value   (symbol_c *sym);
   
 /* A symbol table with all globally declared functions... */
 extern function_declaration_c null_symbol1;
--- a/stage3/constant_folding.cc	Wed Jun 06 00:20:06 2012 +0200
+++ b/stage3/constant_folding.cc	Wed Jun 06 13:28:50 2012 +0100
@@ -72,6 +72,12 @@
 
 
 
+
+#define MALLOC(variable, data_type) \
+ variable = (data_type *)malloc(sizeof(data_type)); \
+ if (variable == NULL) ERROR;
+
+
 #define DO_OPER(dtype)\
     (NULL != symbol->r_exp->const_value_##dtype ) && \
     (NULL != symbol->l_exp->const_value_##dtype )
@@ -96,9 +102,11 @@
     current_display_error_level = 0;
 }
 
+
 constant_folding_c::~constant_folding_c(void) {
 }
 
+
 int constant_folding_c::get_error_count() {
 	return error_count;
 }
@@ -111,14 +119,12 @@
 /* B 1.2.1 - Numeric Literals */
 /******************************/
 void *constant_folding_c::visit(real_c *symbol) {
-	double *real_value;
-
-	real_value = (double *)malloc(sizeof(double));
-	sscanf(symbol->value, "%lf", real_value);
-	symbol->const_value_real = real_value;
-
-	return NULL;
-}
+	MALLOC(symbol->const_value_real, double);
+	*symbol->const_value_real = extract_real_value(symbol);
+
+	return NULL;
+}
+
 
 void *constant_folding_c::visit(integer_c *symbol) {
 	int64_t *integer_value;
@@ -130,6 +136,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(neg_real_c *symbol) {
 	symbol->exp->accept(*this);
 	if (NULL == symbol->exp->const_value_real)
@@ -139,6 +146,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(neg_integer_c *symbol) {
 	int64_t *integer_value;
 
@@ -149,16 +157,19 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(binary_integer_c *symbol) {
 
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(octal_integer_c *symbol) {
 
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(hex_integer_c *symbol) {
 	symbol->const_value_integer = (int64_t*) malloc(sizeof(int64_t));
 	*(symbol->const_value_integer) = extract_hex_value(symbol);
@@ -166,6 +177,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(integer_literal_c *symbol) {
 	symbol->value->accept(*this);
 	if (NULL == symbol->value->const_value_integer) ERROR;
@@ -175,6 +187,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(real_literal_c *symbol) {
 	symbol->value->accept(*this);
 	if (NULL == symbol->value->const_value_real) ERROR;
@@ -184,11 +197,13 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(bit_string_literal_c *symbol) {
 
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(boolean_literal_c *symbol) {
 	symbol->value->accept(*this);
 	if (NULL == symbol->value->const_value_bool) ERROR;
@@ -198,6 +213,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(boolean_true_c *symbol) {
 	symbol->const_value_bool = (bool *)malloc(sizeof(bool));
 	*(symbol->const_value_bool) = true;
@@ -205,6 +221,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(boolean_false_c *symbol) {
 	symbol->const_value_bool = (bool *)malloc(sizeof(bool));
 	*(symbol->const_value_bool) = false;
@@ -212,6 +229,8 @@
 	return NULL;
 }
 
+
+
 /***************************************/
 /* B.3 - Language ST (Structured Text) */
 /***************************************/
@@ -233,6 +252,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(xor_expression_c *symbol) {
 	symbol->l_exp->accept(*this);
 	symbol->r_exp->accept(*this);
@@ -244,6 +264,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(and_expression_c *symbol) {
 	symbol->l_exp->accept(*this);
 	symbol->r_exp->accept(*this);
@@ -259,6 +280,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(equ_expression_c *symbol) {
 	symbol->l_exp->accept(*this);
 	symbol->r_exp->accept(*this);
@@ -278,6 +300,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(notequ_expression_c *symbol) {
 	symbol->l_exp->accept(*this);
 	symbol->r_exp->accept(*this);
@@ -297,6 +320,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(lt_expression_c *symbol) {
 	symbol->l_exp->accept(*this);
 	symbol->r_exp->accept(*this);
@@ -312,6 +336,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(gt_expression_c *symbol) {
 	symbol->l_exp->accept(*this);
 	symbol->r_exp->accept(*this);
@@ -327,6 +352,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(le_expression_c *symbol) {
 	symbol->l_exp->accept(*this);
 	symbol->r_exp->accept(*this);
@@ -342,6 +368,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(ge_expression_c *symbol) {
 	symbol->l_exp->accept(*this);
 	symbol->r_exp->accept(*this);
@@ -357,6 +384,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(add_expression_c *symbol) {
 	symbol->l_exp->accept(*this);
 	symbol->r_exp->accept(*this);
@@ -383,6 +411,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(sub_expression_c *symbol) {
 	symbol->l_exp->accept(*this);
 	symbol->r_exp->accept(*this);
@@ -409,6 +438,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(mul_expression_c *symbol) {
 	symbol->l_exp->accept(*this);
 	symbol->r_exp->accept(*this);
@@ -433,6 +463,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(div_expression_c *symbol) {
 	symbol->l_exp->accept(*this);
 	symbol->r_exp->accept(*this);
@@ -461,6 +492,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(mod_expression_c *symbol) {
 	symbol->l_exp->accept(*this);
 	symbol->r_exp->accept(*this);
@@ -475,6 +507,7 @@
 	return NULL;
 }
 
+
 void *constant_folding_c::visit(power_expression_c *symbol) {
 	symbol->l_exp->accept(*this);
 	symbol->r_exp->accept(*this);
@@ -494,6 +527,8 @@
 
 	return NULL;
 }
+
+
 void *constant_folding_c::visit(neg_expression_c *symbol) {
 	symbol->exp->accept(*this);
 	if (NULL != symbol->exp->const_value_integer) {
@@ -507,6 +542,8 @@
 	return NULL;
 }
 
+
+
 void *constant_folding_c::visit(not_expression_c *symbol) {
 	symbol->exp->accept(*this);
 	if (NULL != symbol->exp->const_value_bool) {