stage3/constant_folding.cc
changeset 567 e5deeb6d4d2f
parent 566 5688fa07f89a
child 568 5f79478142d7
--- 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) {