2019-07-04 15:05:17 -05:00
|
|
|
use super::{InferTy, Ty, TypeCtor};
|
2019-08-17 09:42:41 -05:00
|
|
|
use crate::{
|
|
|
|
expr::{BinaryOp, CmpOp},
|
|
|
|
ty::ApplicationTy,
|
|
|
|
};
|
2019-02-23 08:24:07 -06:00
|
|
|
|
|
|
|
pub(super) fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty {
|
|
|
|
match op {
|
2019-08-17 09:42:41 -05:00
|
|
|
BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => Ty::simple(TypeCtor::Bool),
|
|
|
|
BinaryOp::Assignment { .. } => Ty::unit(),
|
|
|
|
BinaryOp::ArithOp(_) => match rhs_ty {
|
2019-03-21 16:29:12 -05:00
|
|
|
Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
|
|
|
|
match op {
|
2019-08-17 09:42:41 -05:00
|
|
|
BinaryOp::LogicOp(..) => Ty::simple(TypeCtor::Bool),
|
|
|
|
BinaryOp::Assignment { op: None }
|
|
|
|
| BinaryOp::CmpOp(CmpOp::Equal)
|
|
|
|
| BinaryOp::CmpOp(CmpOp::NotEqual) => match lhs_ty {
|
2019-03-21 16:29:12 -05:00
|
|
|
Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
|
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,
|
|
|
|
},
|
2019-08-17 09:42:41 -05:00
|
|
|
BinaryOp::CmpOp(_) | BinaryOp::Assignment { op: Some(_) } | BinaryOp::ArithOp(_) => {
|
|
|
|
match lhs_ty {
|
|
|
|
Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
|
|
|
|
TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty,
|
|
|
|
_ => Ty::Unknown,
|
|
|
|
},
|
|
|
|
Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
|
2019-03-17 13:37:09 -05:00
|
|
|
_ => Ty::Unknown,
|
2019-08-17 09:42:41 -05:00
|
|
|
}
|
|
|
|
}
|
2019-02-23 08:24:07 -06:00
|
|
|
}
|
|
|
|
}
|