+#include <cstring>
#include <msp/core/raii.h>
#include <msp/strings/format.h>
#include "validate.h"
void ExpressionValidator::visit(Assignment &assign)
{
- if(assign.left->type && !assign.left->lvalue)
- error(assign, "Target of assignment is not an lvalue");
- if(assign.left->type && assign.right->type && assign.left->type!=assign.right->type)
- error(assign, format("Assignment to variable of type '%s' from expression of type '%s'",
- assign.left->type->name, assign.right->type->name));
+ if(assign.left->type)
+ {
+ if(!assign.left->lvalue)
+ error(assign, "Target of assignment is not an lvalue");
+ if(assign.right->type)
+ {
+ if(assign.oper->token[0]!='=')
+ {
+ if(!assign.type)
+ error(assign, format("No matching operator '%s' found for '%s' and '%s'",
+ string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
+ }
+ else if(assign.left->type!=assign.right->type)
+ error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
+ assign.left->type->name, assign.right->type->name));
+ }
+ }
TraversingVisitor::visit(assign);
}
void ExpressionValidator::visit(VariableDeclaration &var)
{
if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
- error(var, format("Initializing a variable of type '%s' with an expression of type '%s'",
+ error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
var.type_declaration->name, var.init_expression->type->name));
TraversingVisitor::visit(var);
}