Add IntersectionTypeObj

This commit is contained in:
Shunsuke Shibayama 2022-12-14 18:03:49 +09:00
parent 9b319379d1
commit d5b05c8a28
3 changed files with 49 additions and 2 deletions

View file

@ -642,6 +642,15 @@ impl Context {
line!(),
))),
},
And => match (lhs, rhs) {
(ValueObj::Bool(l), ValueObj::Bool(r)) => Ok(ValueObj::Bool(l && r)),
(ValueObj::Type(lhs), ValueObj::Type(rhs)) => Ok(self.eval_and_type(lhs, rhs)),
_ => Err(EvalErrors::from(EvalError::unreachable(
self.cfg.input.clone(),
fn_name!(),
line!(),
))),
},
_other => Err(EvalErrors::from(EvalError::unreachable(
self.cfg.input.clone(),
fn_name!(),
@ -661,6 +670,19 @@ impl Context {
}
}
fn eval_and_type(&self, lhs: TypeObj, rhs: TypeObj) -> ValueObj {
match (lhs, rhs) {
(TypeObj::Builtin(l), TypeObj::Builtin(r)) => {
ValueObj::builtin_t(self.intersection(&l, &r))
}
(lhs, rhs) => ValueObj::gen_t(GenTypeObj::intersection(
self.intersection(lhs.typ(), rhs.typ()),
lhs,
rhs,
)),
}
}
pub(crate) fn eval_bin_tp(
&self,
op: OpKind,