Implements binding binary expressions
This commit is contained in:
@@ -42,6 +42,9 @@ BoundExpression* Binder::BindExpression(ParsedExpression* expression){
|
||||
case ParsedExpressionKind ::LiteralBool:
|
||||
return new BoundLiteralBoolExpression(((LiteralBoolExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength());
|
||||
|
||||
case ParsedExpressionKind ::Binary:
|
||||
return this -> BindBinaryOperator((BinaryExpression*)expression);
|
||||
|
||||
case ParsedExpressionKind ::Parenthesized:
|
||||
return BindExpression(((ParenthesizedExpression*)expression)->GetInnerExpression());
|
||||
|
||||
@@ -50,3 +53,80 @@ BoundExpression* Binder::BindExpression(ParsedExpression* expression){
|
||||
}
|
||||
}
|
||||
|
||||
BoundExpression* Binder::BindBinaryOperator(BinaryExpression* expression){
|
||||
auto boundLeft = this -> BindExpression(expression->GetLeft());
|
||||
auto boundRight = this -> BindExpression(expression->GetRight());
|
||||
|
||||
auto boundLeftType = boundLeft->GetType();
|
||||
auto boundRightType = boundRight->GetType();
|
||||
|
||||
switch (expression->GetOperatorKind()){
|
||||
case BinaryOperatorKind ::Addition:
|
||||
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
|
||||
auto leftNumeric = (NumericScriptType*)boundLeftType;
|
||||
auto rightNumeric = (NumericScriptType*)boundRightType;
|
||||
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
|
||||
return new BoundBinaryExpression(boundLeft, boundRight,
|
||||
BoundBinaryOperator::Addition, new NumericScriptType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()));
|
||||
}
|
||||
else{
|
||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperator::Addition, new NumericScriptType(false, false));
|
||||
}
|
||||
}
|
||||
//TODO: String Concatenation
|
||||
break;
|
||||
case BinaryOperatorKind ::Subtraction:
|
||||
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
|
||||
auto leftNumeric = (NumericScriptType*)boundLeftType;
|
||||
auto rightNumeric = (NumericScriptType*)boundRightType;
|
||||
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
|
||||
return new BoundBinaryExpression(boundLeft, boundRight,
|
||||
BoundBinaryOperator::Subtraction, new NumericScriptType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()));
|
||||
}
|
||||
else{
|
||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperator::Subtraction, new NumericScriptType(false, false));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case BinaryOperatorKind ::Multiplication:
|
||||
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
|
||||
auto leftNumeric = (NumericScriptType*)boundLeftType;
|
||||
auto rightNumeric = (NumericScriptType*)boundRightType;
|
||||
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
|
||||
return new BoundBinaryExpression(boundLeft, boundRight,
|
||||
BoundBinaryOperator::Multiplication, new NumericScriptType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()));
|
||||
}
|
||||
else{
|
||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperator::Multiplication, new NumericScriptType(false, false));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case BinaryOperatorKind ::Division:
|
||||
if (boundLeftType->GetClass() == TypeClass::Number && boundRightType->GetClass() == TypeClass::Number){
|
||||
auto leftNumeric = (NumericScriptType*)boundLeftType;
|
||||
auto rightNumeric = (NumericScriptType*)boundRightType;
|
||||
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()){
|
||||
return new BoundBinaryExpression(boundLeft, boundRight,
|
||||
BoundBinaryOperator::Division, new NumericScriptType(true, leftNumeric->IsFloat() || rightNumeric->IsFloat()));
|
||||
}
|
||||
else{
|
||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperator::Division, new NumericScriptType(false, false));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case BinaryOperatorKind ::Equality:
|
||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperator::Equality, new ScriptType(TypeClass::Bool));
|
||||
case BinaryOperatorKind ::LogicalAnd:
|
||||
if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool)
|
||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperator::LogicalAnd, new ScriptType(TypeClass::Bool));
|
||||
break;
|
||||
case BinaryOperatorKind ::LogicalOr:
|
||||
if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool)
|
||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperator::LogicalOr, new ScriptType(TypeClass::Bool));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
//TODO: Log error
|
||||
return new BoundBadExpression(boundLeft->GetStartPosition(), boundRight->GetEndPosition() - boundLeft->GetStartPosition());
|
||||
}
|
||||
Reference in New Issue
Block a user