2019-03-17 13:37:09 -05:00
|
|
|
use crate::{ ty::ApplicationTy, expr::BinaryOp};
|
2019-03-21 16:20:03 -05:00
|
|
|
use super::{Ty, TypeCtor, InferTy};
|
2019-02-23 08:24:07 -06:00
|
|
|
|
|
|
|
pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty {
|
|
|
|
match op {
|
|
|
|
BinaryOp::BooleanOr
|
|
|
|
| BinaryOp::BooleanAnd
|
|
|
|
| BinaryOp::EqualityTest
|
|
|
|
| BinaryOp::NegatedEqualityTest
|
|
|
|
| BinaryOp::LesserEqualTest
|
|
|
|
| BinaryOp::GreaterEqualTest
|
|
|
|
| BinaryOp::LesserTest
|
2019-03-21 16:20:03 -05:00
|
|
|
| BinaryOp::GreaterTest => Ty::simple(TypeCtor::Bool),
|
2019-02-23 08:24:07 -06:00
|
|
|
BinaryOp::Assignment
|
|
|
|
| BinaryOp::AddAssign
|
|
|
|
| BinaryOp::SubAssign
|
|
|
|
| BinaryOp::DivAssign
|
|
|
|
| BinaryOp::MulAssign
|
|
|
|
| BinaryOp::RemAssign
|
|
|
|
| BinaryOp::ShrAssign
|
|
|
|
| BinaryOp::ShlAssign
|
|
|
|
| BinaryOp::BitAndAssign
|
|
|
|
| BinaryOp::BitOrAssign
|
|
|
|
| BinaryOp::BitXorAssign => Ty::unit(),
|
|
|
|
BinaryOp::Addition
|
|
|
|
| BinaryOp::Subtraction
|
|
|
|
| BinaryOp::Multiplication
|
|
|
|
| BinaryOp::Division
|
|
|
|
| BinaryOp::Remainder
|
|
|
|
| BinaryOp::LeftShift
|
|
|
|
| BinaryOp::RightShift
|
|
|
|
| BinaryOp::BitwiseAnd
|
|
|
|
| BinaryOp::BitwiseOr
|
|
|
|
| BinaryOp::BitwiseXor => match rhs_ty {
|
2019-03-17 13:37:09 -05:00
|
|
|
Ty::Apply(ApplicationTy { name, .. }) => match name {
|
2019-03-21 16:20:03 -05:00
|
|
|
TypeCtor::Int(..) | TypeCtor::Float(..) => rhs_ty,
|
2019-03-17 13:37:09 -05:00
|
|
|
_ => Ty::Unknown,
|
|
|
|
},
|
|
|
|
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty,
|
2019-02-23 08:24:07 -06:00
|
|
|
_ => Ty::Unknown,
|
|
|
|
},
|
|
|
|
BinaryOp::RangeRightOpen | BinaryOp::RangeRightClosed => Ty::Unknown,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
|
|
|
|
match op {
|
2019-03-21 16:20:03 -05:00
|
|
|
BinaryOp::BooleanAnd | BinaryOp::BooleanOr => Ty::simple(TypeCtor::Bool),
|
2019-02-23 08:24:07 -06:00
|
|
|
BinaryOp::Assignment | BinaryOp::EqualityTest => match lhs_ty {
|
2019-03-17 13:37:09 -05:00
|
|
|
Ty::Apply(ApplicationTy { name, .. }) => match name {
|
2019-03-21 16:20:03 -05:00
|
|
|
TypeCtor::Int(..)
|
|
|
|
| TypeCtor::Float(..)
|
|
|
|
| TypeCtor::Str
|
|
|
|
| TypeCtor::Char
|
|
|
|
| TypeCtor::Bool => lhs_ty,
|
2019-03-17 13:37:09 -05:00
|
|
|
_ => Ty::Unknown,
|
|
|
|
},
|
|
|
|
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
|
2019-02-23 08:24:07 -06:00
|
|
|
_ => Ty::Unknown,
|
|
|
|
},
|
|
|
|
BinaryOp::LesserEqualTest
|
|
|
|
| BinaryOp::GreaterEqualTest
|
|
|
|
| BinaryOp::LesserTest
|
|
|
|
| BinaryOp::GreaterTest
|
|
|
|
| BinaryOp::AddAssign
|
|
|
|
| BinaryOp::SubAssign
|
|
|
|
| BinaryOp::DivAssign
|
|
|
|
| BinaryOp::MulAssign
|
|
|
|
| BinaryOp::RemAssign
|
|
|
|
| BinaryOp::ShrAssign
|
|
|
|
| BinaryOp::ShlAssign
|
|
|
|
| BinaryOp::BitAndAssign
|
|
|
|
| BinaryOp::BitOrAssign
|
|
|
|
| BinaryOp::BitXorAssign
|
|
|
|
| BinaryOp::Addition
|
|
|
|
| BinaryOp::Subtraction
|
|
|
|
| BinaryOp::Multiplication
|
|
|
|
| BinaryOp::Division
|
|
|
|
| BinaryOp::Remainder
|
|
|
|
| BinaryOp::LeftShift
|
|
|
|
| BinaryOp::RightShift
|
|
|
|
| BinaryOp::BitwiseAnd
|
|
|
|
| BinaryOp::BitwiseOr
|
|
|
|
| BinaryOp::BitwiseXor => match lhs_ty {
|
2019-03-17 13:37:09 -05:00
|
|
|
Ty::Apply(ApplicationTy { name, .. }) => match name {
|
2019-03-21 16:20:03 -05:00
|
|
|
TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty,
|
2019-03-17 13:37:09 -05:00
|
|
|
_ => Ty::Unknown,
|
|
|
|
},
|
|
|
|
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
|
2019-02-23 08:24:07 -06:00
|
|
|
_ => Ty::Unknown,
|
|
|
|
},
|
|
|
|
_ => Ty::Unknown,
|
|
|
|
}
|
|
|
|
}
|