Apply formatting

This commit is contained in:
Félix Saparelli 2022-08-15 17:44:15 +12:00
parent 7d3116f546
commit 8efffbfbc0
No known key found for this signature in database
GPG key ID: B948C4BAE44FC474
10 changed files with 171 additions and 119 deletions

View file

@ -103,7 +103,8 @@ impl From<u8> for DataTypePrefix {
impl DataTypePrefix {
pub const fn is_sized(&self) -> bool {
matches!(self,
matches!(
self,
Self::Long
| Self::Str
| Self::ShortAscii
@ -113,7 +114,8 @@ impl DataTypePrefix {
| Self::SmallTuple
| Self::Tuple
| Self::Code
| Self::Builtin)
| Self::Builtin
)
}
}

View file

@ -1546,11 +1546,13 @@ impl SubrKind {
}
pub fn same_kind_as(&self, other: &Self) -> bool {
matches!((self, other),
matches!(
(self, other),
(Self::Func, Self::Func)
| (Self::Proc, Self::Proc)
| (Self::FuncMethod(_), Self::FuncMethod(_))
| (Self::ProcMethod { .. }, Self::ProcMethod { .. }))
| (Self::ProcMethod { .. }, Self::ProcMethod { .. })
)
}
pub fn self_t(&self) -> Option<&SelfType> {

View file

@ -286,11 +286,7 @@ impl ValueObj {
pub fn into_bytes(self) -> Vec<u8> {
match self {
Self::Int(i) => [
vec![DataTypePrefix::Int32 as u8],
i.to_le_bytes().to_vec(),
]
.concat(),
Self::Int(i) => [vec![DataTypePrefix::Int32 as u8], i.to_le_bytes().to_vec()].concat(),
// TODO: Natとしてシリアライズ
Self::Nat(n) => [
vec![DataTypePrefix::Int32 as u8],

View file

@ -290,8 +290,8 @@ impl CodeGenerator {
} else {
Some(Name::fast(idx))
}
} else { self
.cur_block_codeobj()
} else {
self.cur_block_codeobj()
.freevars
.iter()
.position(|f| &**f == name)

View file

@ -178,7 +178,10 @@ impl TyVarContext {
}),
Type::VarArgs(t) => Self::rec_t_inner_qvars(t, dep),
Type::Subr(_subr) => todo!(),
Type::Callable { param_ts: _, return_t: _ } => todo!(),
Type::Callable {
param_ts: _,
return_t: _,
} => todo!(),
Type::And(_) | Type::Or(_) | Type::Not(_) => todo!(),
Type::Record(_) => todo!(),
Type::Quantified(_) => todo!(),
@ -209,67 +212,66 @@ impl TyVarContext {
match bound {
TyBound::Subtype { sub, sup } => {
let sup = match sup {
Type::Poly { name, params } => {
Type::poly(
Type::Poly { name, params } => Type::poly(
name,
params.into_iter().map(|p| self.instantiate_tp(p)).collect(),
)
}
),
Type::MonoProj { lhs, rhs } => Type::mono_proj(self.instantiate_t(*lhs), rhs),
sup => sup,
};
let constraint = Constraint::SubtypeOf(sup);
let name = Str::rc(sub.name());
self.push_tyvar(name.clone(), Type::named_free_var(name, self.level, constraint));
self.push_tyvar(
name.clone(),
Type::named_free_var(name, self.level, constraint),
);
}
TyBound::Supertype { sup, sub } => {
let sub = match sub {
Type::Poly { name, params } => {
Type::poly(
Type::Poly { name, params } => Type::poly(
name,
params.into_iter().map(|p| self.instantiate_tp(p)).collect(),
)
}
),
Type::MonoProj { lhs, rhs } => Type::mono_proj(self.instantiate_t(*lhs), rhs),
sub => sub,
};
let constraint = Constraint::SupertypeOf(sub);
let name = Str::rc(sup.name());
self.push_tyvar(name.clone(), Type::named_free_var(name, self.level, constraint));
self.push_tyvar(
name.clone(),
Type::named_free_var(name, self.level, constraint),
);
}
TyBound::Sandwiched { sub, mid, sup } => {
let sub = match sub {
Type::Poly { name, params } => {
Type::poly(
Type::Poly { name, params } => Type::poly(
name,
params.into_iter().map(|p| self.instantiate_tp(p)).collect(),
)
}
),
Type::MonoProj { lhs, rhs } => Type::mono_proj(self.instantiate_t(*lhs), rhs),
sub => sub,
};
let sup = match sup {
Type::Poly { name, params } => {
Type::poly(
Type::Poly { name, params } => Type::poly(
name,
params.into_iter().map(|p| self.instantiate_tp(p)).collect(),
)
}
),
Type::MonoProj { lhs, rhs } => Type::mono_proj(self.instantiate_t(*lhs), rhs),
sup => sup,
};
let constraint = Constraint::Sandwiched { sub, sup };
let name = Str::rc(mid.name());
self.push_tyvar(name.clone(), Type::named_free_var(name, self.level, constraint));
self.push_tyvar(
name.clone(),
Type::named_free_var(name, self.level, constraint),
);
}
TyBound::Instance { name, t } => {
let t = match t {
Type::Poly { name, params } => {
Type::poly(
Type::Poly { name, params } => Type::poly(
name,
params.into_iter().map(|p| self.instantiate_tp(p)).collect(),
)
}
),
t => t,
};
// TODO: type-like types
@ -1552,18 +1554,19 @@ impl Context {
// in toplevel: ?T(<: Int)[n] should replaced to Int (if n > 0)
Type::FreeVar(fv) if fv.is_unbound() => {
match fv.borrow().constraint().unwrap() {
Constraint::SubtypeOf(sup)
| Constraint::Sandwiched { sup, .. } if self.level <= fv.level().unwrap() => {
Constraint::SubtypeOf(sup) | Constraint::Sandwiched { sup, .. }
if self.level <= fv.level().unwrap() =>
{
return Ok(sup.clone());
},
}
// REVIEW: really?
Constraint::SupertypeOf(sub) if self.level <= fv.level().unwrap() => {
return Ok(sub.clone());
},
_ => {},
}
_ => {}
}
Ok(Type::FreeVar(fv))
},
}
Type::FreeVar(fv) if fv.is_linked() => Ok(fv.unwrap()),
// 未連携型変数のチェックはモジュール全体の型検査が終わった後にやる
// Type::FreeVar(_) =>
@ -1624,7 +1627,9 @@ impl Context {
if impls.is_empty() {
panic!("{} is not implemented", name);
}
let min = self.smallest_t(impls.clone().into_iter()).unwrap_or_else(move || {
let min = self
.smallest_t(impls.clone().into_iter())
.unwrap_or_else(move || {
panic!("cannot determine the smallest type: {}", fmt_vec(&impls))
});
dbg!(&min);
@ -1633,7 +1638,7 @@ impl Context {
}
dbg!(&params);
Ok(min)
},
}
_ => Ok(maybe_poly),
}
}
@ -1641,9 +1646,12 @@ impl Context {
/// 可変依存型の変更を伝搬させる
fn propagate(&self, t: &Type, callee: &hir::Expr) -> TyCheckResult<()> {
if let Type::Subr(SubrType {
kind: SubrKind::ProcMethod { after: Some(after), .. },
kind: SubrKind::ProcMethod {
after: Some(after), ..
},
..
}) = t {
}) = t
{
let receiver_t = callee.receiver_t().unwrap();
self.reunify(receiver_t, after, Some(callee.loc()), None)?;
}
@ -2120,7 +2128,7 @@ impl Context {
}
Constraint::TypeOf(_t) => {
*constraint = Constraint::SupertypeOf(l.clone());
},
}
_ => {}
},
_ => {}
@ -2639,11 +2647,7 @@ impl Context {
} else {
expr_t.clone()
};
let param_ts = [
vec![ParamTy::anonymous(expr_t)],
branch_ts.to_vec(),
]
.concat();
let param_ts = [vec![ParamTy::anonymous(expr_t)], branch_ts.to_vec()].concat();
let t = Type::func(param_ts, vec![], return_t);
Ok(t)
}
@ -2908,7 +2912,9 @@ impl Context {
}
}
for (patch_name, sub, sup) in self.glue_patch_and_types.iter() {
let patch = self.rec_get_patch(patch_name).unwrap_or_else(|| panic!("{patch_name} not found"));
let patch = self
.rec_get_patch(patch_name)
.unwrap_or_else(|| panic!("{patch_name} not found"));
let bounds = patch.type_params_bounds();
let variance = patch.type_params_variance();
if self.formal_supertype_of(sub, rhs, Some(&bounds), Some(&variance))
@ -3757,7 +3763,9 @@ impl Context {
fn rec_get_trait_impls(&self, name: &Str) -> Vec<Type> {
let impls = if let Some(impls) = self.poly_trait_impls.get(name) {
impls.clone()
} else { vec![] };
} else {
vec![]
};
if let Some(outer) = &self.outer {
[impls, outer.rec_get_trait_impls(name)].concat()
} else {

View file

@ -309,7 +309,9 @@ impl Evaluator {
level: usize,
) -> EvalResult<Type> {
match substituted {
Type::FreeVar(fv) if fv.is_linked() => self.eval_t_params(fv.crack().clone(), ctx, level),
Type::FreeVar(fv) if fv.is_linked() => {
self.eval_t_params(fv.crack().clone(), ctx, level)
}
Type::Subr(mut subr) => {
let kind = match subr.kind {
SubrKind::FuncMethod(self_t) => {

View file

@ -59,10 +59,8 @@ impl Context {
if let Some(impls) = self.poly_trait_impls.get_mut(impl_trait.name()) {
impls.push(impl_trait.clone());
} else {
self.poly_trait_impls.insert(
Str::rc(impl_trait.name()),
vec![impl_trait.clone()],
);
self.poly_trait_impls
.insert(Str::rc(impl_trait.name()), vec![impl_trait.clone()]);
}
}
}
@ -147,10 +145,15 @@ impl Context {
let (r_bound, o_bound) = (static_instance("R", Type), static_instance("O", Type));
let params = vec![PS::t("R", WithDefault), PS::t("O", WithDefault)];
let ty_params = vec![mono_q_tp("R"), mono_q_tp("O")];
let mut add = Self::poly_trait("Add", params.clone(), vec![
let mut add = Self::poly_trait(
"Add",
params.clone(),
vec![
poly("Output", vec![ty_tp(mono_q("R"))]),
poly("Output", vec![ty_tp(mono_q("O"))]),
], Self::TOP_LEVEL);
],
Self::TOP_LEVEL,
);
let self_bound = subtype(
poly_q("Self", ty_params.clone()),
poly("Add", ty_params.clone()),
@ -158,10 +161,15 @@ impl Context {
let op_t = fn1_met(poly_q("Self", ty_params.clone()), r.clone(), o.clone());
let op_t = quant(op_t, set! {r_bound.clone(), o_bound.clone(), self_bound});
add.register_decl("__add__", op_t, Public);
let mut sub = Self::poly_trait("Sub", params.clone(), vec![
let mut sub = Self::poly_trait(
"Sub",
params.clone(),
vec![
poly("Output", vec![ty_tp(mono_q("R"))]),
poly("Output", vec![ty_tp(mono_q("O"))]),
], Self::TOP_LEVEL);
],
Self::TOP_LEVEL,
);
let self_bound = subtype(
poly_q("Self", ty_params.clone()),
poly("Sub", ty_params.clone()),
@ -169,16 +177,26 @@ impl Context {
let op_t = fn1_met(poly_q("Self", ty_params.clone()), r.clone(), o.clone());
let op_t = quant(op_t, set! {r_bound, o_bound, self_bound});
sub.register_decl("__sub__", op_t, Public);
let mut mul = Self::poly_trait("Mul", params.clone(), vec![
let mut mul = Self::poly_trait(
"Mul",
params.clone(),
vec![
poly("Output", vec![ty_tp(mono_q("R"))]),
poly("Output", vec![ty_tp(mono_q("O"))]),
], Self::TOP_LEVEL);
],
Self::TOP_LEVEL,
);
let op_t = fn1_met(poly("Mul", ty_params.clone()), r.clone(), o.clone());
mul.register_decl("__mul__", op_t, Public);
let mut div = Self::poly_trait("Div", params, vec![
let mut div = Self::poly_trait(
"Div",
params,
vec![
poly("Output", vec![ty_tp(mono_q("R"))]),
poly("Output", vec![ty_tp(mono_q("O"))]),
], Self::TOP_LEVEL);
],
Self::TOP_LEVEL,
);
let op_t = fn1_met(poly("Div", ty_params.clone()), r, o);
div.register_decl("__div__", op_t, Public);
/*let sup = poly(
@ -380,7 +398,12 @@ impl Context {
let mut str_ = Self::mono_class(
"Str",
vec![Obj],
vec![mono("Eq"), mono("Mutate"), poly("Seq", vec![ty_tp(Str)]), poly("Add", vec![ty_tp(Str), ty_tp(Str)])],
vec![
mono("Eq"),
mono("Mutate"),
poly("Seq", vec![ty_tp(Str)]),
poly("Add", vec![ty_tp(Str), ty_tp(Str)]),
],
Self::TOP_LEVEL,
);
str_.register_impl("__add__", fn1_met(Str, Str, Str), Const, Public);

View file

@ -51,13 +51,7 @@ impl ASTLowerer {
found: &Type,
) -> LowerResult<()> {
self.ctx.unify(expect, found, Some(loc), None).map_err(|_| {
LowerError::type_mismatch_error(
loc,
self.ctx.caused_by(),
name,
expect,
found,
)
LowerError::type_mismatch_error(loc, self.ctx.caused_by(), name, expect, found)
})
}

View file

@ -179,9 +179,31 @@ impl Lexer /*<'a>*/ {
#[inline]
fn is_definable_operator(s: &str) -> bool {
matches!(s,
"+" | "-" | "*" | "/" | "//" | "**" | "%" | ".." | "..=" | "~" | "&&" | "||" | "^^"
| ">>" | "<<" | "==" | "!=" | ">" | "<" | ">=" | "<=" | "dot" | "cross")
matches!(
s,
"+" | "-"
| "*"
| "/"
| "//"
| "**"
| "%"
| ".."
| "..="
| "~"
| "&&"
| "||"
| "^^"
| ">>"
| "<<"
| "=="
| "!="
| ">"
| "<"
| ">="
| "<="
| "dot"
| "cross"
)
}
// +, -, * etc. may be pre/bin

View file

@ -293,7 +293,10 @@ impl TokenKind {
}
pub const fn is_right_associative(&self) -> bool {
matches!(self, FuncArrow | ProcArrow | Equal /* | PreDollar | PreAt */)
matches!(
self,
FuncArrow | ProcArrow | Equal /* | PreDollar | PreAt */
)
}
}