Clean code for printing errors in expressions.
authorMario de Sousa <msousa@fe.up.pt>
Wed, 14 Mar 2012 11:31:07 +0000
changeset 485 835697564c6d
parent 484 f78750994a82
child 486 e22150ad75fd
Clean code for printing errors in expressions.
stage3/print_datatypes_error.cc
stage3/print_datatypes_error.hh
--- a/stage3/print_datatypes_error.cc	Wed Mar 14 11:14:41 2012 +0000
+++ b/stage3/print_datatypes_error.cc	Wed Mar 14 11:31:07 2012 +0000
@@ -975,165 +975,33 @@
 /* B 3.1 - Expressions */
 /***********************/
 
-void *print_datatypes_error_c::visit(or_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
+void *print_datatypes_error_c::print_binary_expression_errors(const char *operation, symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr, bool deprecated_operation) {
+	l_expr->accept(*this);
+	r_expr->accept(*this);
 	if ((symbol->candidate_datatypes.size() == 0) 		&&
-		(symbol->l_exp->candidate_datatypes.size() > 0)	&&
-		(symbol->r_exp->candidate_datatypes.size() > 0))
-		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for 'OR' expression.");
-	return NULL;
-}
-
-
-void *print_datatypes_error_c::visit(xor_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-	if ((symbol->candidate_datatypes.size() == 0) 		&&
-		(symbol->l_exp->candidate_datatypes.size() > 0)	&&
-		(symbol->r_exp->candidate_datatypes.size() > 0))
-		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for 'XOR' expression.");
-	return NULL;
-}
-
-
-void *print_datatypes_error_c::visit(and_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-	if ((symbol->candidate_datatypes.size() == 0) 		&&
-		(symbol->l_exp->candidate_datatypes.size() > 0)	&&
-		(symbol->r_exp->candidate_datatypes.size() > 0))
-		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for 'AND' expression.");
-	return NULL;
-}
-
-
-void *print_datatypes_error_c::visit(equ_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-	if ((symbol->candidate_datatypes.size() == 0) 		&&
-		(symbol->l_exp->candidate_datatypes.size() > 0)	&&
-		(symbol->r_exp->candidate_datatypes.size() > 0))
-		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for '=' expression.");
-	return NULL;
-}
-
-
-void *print_datatypes_error_c::visit(notequ_expression_c *symbol)  {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-	if ((symbol->candidate_datatypes.size() == 0) 		&&
-		(symbol->l_exp->candidate_datatypes.size() > 0)	&&
-		(symbol->r_exp->candidate_datatypes.size() > 0))
-		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for '<>' expression.");
-	return NULL;
-}
-
-
-void *print_datatypes_error_c::visit(lt_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-	if ((symbol->candidate_datatypes.size() == 0) 		&&
-		(symbol->l_exp->candidate_datatypes.size() > 0)	&&
-		(symbol->r_exp->candidate_datatypes.size() > 0))
-		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for '<' expression.");
-	return NULL;
-}
-
-
-void *print_datatypes_error_c::visit(gt_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-	if ((symbol->candidate_datatypes.size() == 0) 		&&
-		(symbol->l_exp->candidate_datatypes.size() > 0)	&&
-		(symbol->r_exp->candidate_datatypes.size() > 0))
-		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for '>' expression.");
-	return NULL;
-}
-
-
-void *print_datatypes_error_c::visit(le_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-	if ((symbol->candidate_datatypes.size() == 0) 		&&
-		(symbol->l_exp->candidate_datatypes.size() > 0)	&&
-		(symbol->r_exp->candidate_datatypes.size() > 0))
-		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for '<=' expression.");
-	return NULL;
-}
-
-
-void *print_datatypes_error_c::visit(ge_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-	if ((symbol->candidate_datatypes.size() == 0) 		&&
-		(symbol->l_exp->candidate_datatypes.size() > 0)	&&
-		(symbol->r_exp->candidate_datatypes.size() > 0))
-		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for '>=' expression.");
-	return NULL;
-}
-
-
-void *print_datatypes_error_c::visit(add_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-	if ((symbol->candidate_datatypes.size() == 0) 		&&
-		(symbol->l_exp->candidate_datatypes.size() > 0)	&&
-		(symbol->r_exp->candidate_datatypes.size() > 0))
-		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for '+' expression.");
-        if (symbol->deprecated_operation)
-                STAGE3_WARNING(symbol, symbol, "Deprecated operation for '+' expression.");	
-	return NULL;
-}
-
-
-
-void *print_datatypes_error_c::visit(sub_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-	if ((symbol->candidate_datatypes.size() == 0) 		&&
-		(symbol->l_exp->candidate_datatypes.size() > 0)	&&
-		(symbol->r_exp->candidate_datatypes.size() > 0))
-		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for '-' expression.");
-        if (symbol->deprecated_operation)
-                STAGE3_WARNING(symbol, symbol, "Deprecated operation for '-' expression.");
-	return NULL;
-}
-
-void *print_datatypes_error_c::visit(mul_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-	if ((symbol->candidate_datatypes.size() == 0) 		&&
-		(symbol->l_exp->candidate_datatypes.size() > 0)	&&
-		(symbol->r_exp->candidate_datatypes.size() > 0))
-		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for '*' expression.");
-        if (symbol->deprecated_operation)
-                STAGE3_WARNING(symbol, symbol, "Deprecated operation for '*' expression.");
-	return NULL;
-}
-
-void *print_datatypes_error_c::visit(div_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-	if ((symbol->candidate_datatypes.size() == 0) 		&&
-		(symbol->l_exp->candidate_datatypes.size() > 0)	&&
-		(symbol->r_exp->candidate_datatypes.size() > 0))
-		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for '/' expression.");
-        if (symbol->deprecated_operation)
-                STAGE3_WARNING(symbol, symbol, "Deprecated operation for '/' expression.");
-	return NULL;
-}
-
-
-void *print_datatypes_error_c::visit(mod_expression_c *symbol) {
-	symbol->l_exp->accept(*this);
-	symbol->r_exp->accept(*this);
-	if ((symbol->candidate_datatypes.size() == 0) 		&&
-		(symbol->l_exp->candidate_datatypes.size() > 0)	&&
-		(symbol->r_exp->candidate_datatypes.size() > 0))
-		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for 'MOD' expression.");
-	return NULL;
-}
+		(l_expr->candidate_datatypes.size() > 0)	&&
+		(r_expr->candidate_datatypes.size() > 0))
+		STAGE3_ERROR(0, symbol, symbol, "Data type mismatch for '%s' expression.", operation);
+        if (deprecated_operation)
+                STAGE3_WARNING(symbol, symbol, "Deprecated operation for '%s' expression.", operation);
+	return NULL;
+}
+
+
+void *print_datatypes_error_c::visit(    or_expression_c *symbol) {return print_binary_expression_errors( "OR", symbol, symbol->l_exp, symbol->r_exp);}
+void *print_datatypes_error_c::visit(   xor_expression_c *symbol) {return print_binary_expression_errors("XOR", symbol, symbol->l_exp, symbol->r_exp);}
+void *print_datatypes_error_c::visit(   and_expression_c *symbol) {return print_binary_expression_errors("AND", symbol, symbol->l_exp, symbol->r_exp);}
+void *print_datatypes_error_c::visit(   equ_expression_c *symbol) {return print_binary_expression_errors( "=" , symbol, symbol->l_exp, symbol->r_exp);}
+void *print_datatypes_error_c::visit(notequ_expression_c *symbol) {return print_binary_expression_errors( "<>", symbol, symbol->l_exp, symbol->r_exp);}
+void *print_datatypes_error_c::visit(    lt_expression_c *symbol) {return print_binary_expression_errors( "<" , symbol, symbol->l_exp, symbol->r_exp);}
+void *print_datatypes_error_c::visit(    gt_expression_c *symbol) {return print_binary_expression_errors( ">" , symbol, symbol->l_exp, symbol->r_exp);}
+void *print_datatypes_error_c::visit(    le_expression_c *symbol) {return print_binary_expression_errors( "<=", symbol, symbol->l_exp, symbol->r_exp);}
+void *print_datatypes_error_c::visit(    ge_expression_c *symbol) {return print_binary_expression_errors( ">=", symbol, symbol->l_exp, symbol->r_exp);}
+void *print_datatypes_error_c::visit(   add_expression_c *symbol) {return print_binary_expression_errors( "+" , symbol, symbol->l_exp, symbol->r_exp, symbol->deprecated_operation);}
+void *print_datatypes_error_c::visit(   sub_expression_c *symbol) {return print_binary_expression_errors( "-" , symbol, symbol->l_exp, symbol->r_exp, symbol->deprecated_operation);}
+void *print_datatypes_error_c::visit(   mul_expression_c *symbol) {return print_binary_expression_errors( "*" , symbol, symbol->l_exp, symbol->r_exp, symbol->deprecated_operation);}
+void *print_datatypes_error_c::visit(   div_expression_c *symbol) {return print_binary_expression_errors( "/" , symbol, symbol->l_exp, symbol->r_exp, symbol->deprecated_operation);}
+void *print_datatypes_error_c::visit(   mod_expression_c *symbol) {return print_binary_expression_errors("MOD", symbol, symbol->l_exp, symbol->r_exp);}
 
 
 void *print_datatypes_error_c::visit(power_expression_c *symbol) {
--- a/stage3/print_datatypes_error.hh	Wed Mar 14 11:14:41 2012 +0000
+++ b/stage3/print_datatypes_error.hh	Wed Mar 14 11:31:07 2012 +0000
@@ -92,8 +92,8 @@
     void *handle_implicit_il_fb_invocation(const char *param_name, symbol_c *il_operator, symbol_c *called_fb_declaration);  
     void *handle_conditional_flow_control_IL_instruction(symbol_c *symbol, const char *oper);
 
-    void *print_binary_operator_errors(const char *il_operator, symbol_c *symbol, bool deprecated_operation = false);
-
+    void *print_binary_operator_errors  (const char *il_operator, symbol_c *symbol,                                     bool deprecated_operation = false);
+    void *print_binary_expression_errors(const char *operation  , symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr, bool deprecated_operation = false);
 
     
   public: