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

View file

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

View file

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

View file

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

View file

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

View file

@ -309,7 +309,9 @@ impl Evaluator {
level: usize, level: usize,
) -> EvalResult<Type> { ) -> EvalResult<Type> {
match substituted { 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) => { Type::Subr(mut subr) => {
let kind = match subr.kind { let kind = match subr.kind {
SubrKind::FuncMethod(self_t) => { 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()) { if let Some(impls) = self.poly_trait_impls.get_mut(impl_trait.name()) {
impls.push(impl_trait.clone()); impls.push(impl_trait.clone());
} else { } else {
self.poly_trait_impls.insert( self.poly_trait_impls
Str::rc(impl_trait.name()), .insert(Str::rc(impl_trait.name()), vec![impl_trait.clone()]);
vec![impl_trait.clone()],
);
} }
} }
} }
@ -147,10 +145,15 @@ impl Context {
let (r_bound, o_bound) = (static_instance("R", Type), static_instance("O", Type)); 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 params = vec![PS::t("R", WithDefault), PS::t("O", WithDefault)];
let ty_params = vec![mono_q_tp("R"), mono_q_tp("O")]; 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("R"))]),
poly("Output", vec![ty_tp(mono_q("O"))]), poly("Output", vec![ty_tp(mono_q("O"))]),
], Self::TOP_LEVEL); ],
Self::TOP_LEVEL,
);
let self_bound = subtype( let self_bound = subtype(
poly_q("Self", ty_params.clone()), poly_q("Self", ty_params.clone()),
poly("Add", 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 = 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}); let op_t = quant(op_t, set! {r_bound.clone(), o_bound.clone(), self_bound});
add.register_decl("__add__", op_t, Public); 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("R"))]),
poly("Output", vec![ty_tp(mono_q("O"))]), poly("Output", vec![ty_tp(mono_q("O"))]),
], Self::TOP_LEVEL); ],
Self::TOP_LEVEL,
);
let self_bound = subtype( let self_bound = subtype(
poly_q("Self", ty_params.clone()), poly_q("Self", ty_params.clone()),
poly("Sub", 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 = 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}); let op_t = quant(op_t, set! {r_bound, o_bound, self_bound});
sub.register_decl("__sub__", op_t, Public); 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("R"))]),
poly("Output", vec![ty_tp(mono_q("O"))]), 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()); let op_t = fn1_met(poly("Mul", ty_params.clone()), r.clone(), o.clone());
mul.register_decl("__mul__", op_t, Public); 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("R"))]),
poly("Output", vec![ty_tp(mono_q("O"))]), 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); let op_t = fn1_met(poly("Div", ty_params.clone()), r, o);
div.register_decl("__div__", op_t, Public); div.register_decl("__div__", op_t, Public);
/*let sup = poly( /*let sup = poly(
@ -380,7 +398,12 @@ impl Context {
let mut str_ = Self::mono_class( let mut str_ = Self::mono_class(
"Str", "Str",
vec![Obj], 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, Self::TOP_LEVEL,
); );
str_.register_impl("__add__", fn1_met(Str, Str, Str), Const, Public); str_.register_impl("__add__", fn1_met(Str, Str, Str), Const, Public);

View file

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

View file

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

View file

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