Impl subroutine type with default parameters

This commit is contained in:
Shunsuke Shibayama 2022-10-18 10:50:41 +09:00
parent eadfe9794a
commit 4bb367f258
3 changed files with 55 additions and 14 deletions

View file

@ -1591,13 +1591,31 @@ impl ParamTySpec {
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DefaultParamTySpec {
pub param: ParamTySpec,
pub default: TypeSpec,
}
impl fmt::Display for DefaultParamTySpec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} := {}", self.param, self.default)
}
}
impl DefaultParamTySpec {
pub const fn new(param: ParamTySpec, default: TypeSpec) -> Self {
Self { param, default }
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SubrTypeSpec {
pub bounds: TypeBoundSpecs,
pub lparen: Option<Token>,
pub non_defaults: Vec<ParamTySpec>,
pub var_args: Option<Box<ParamTySpec>>,
pub defaults: Vec<ParamTySpec>,
pub defaults: Vec<DefaultParamTySpec>,
pub arrow: Token,
pub return_t: Box<TypeSpec>,
}
@ -1638,7 +1656,7 @@ impl SubrTypeSpec {
lparen: Option<Token>,
non_defaults: Vec<ParamTySpec>,
var_args: Option<ParamTySpec>,
defaults: Vec<ParamTySpec>,
defaults: Vec<DefaultParamTySpec>,
arrow: Token,
return_t: TypeSpec,
) -> Self {

View file

@ -2813,12 +2813,18 @@ impl Parser {
for param in lambda.sig.params.defaults.into_iter() {
let param = match (param.sig.pat, param.sig.t_spec) {
(ParamPattern::VarName(name), Some(t_spec_with_op)) => {
ParamTySpec::new(Some(name.into_token()), t_spec_with_op.t_spec)
let param_spec =
ParamTySpec::new(Some(name.into_token()), t_spec_with_op.t_spec);
let default_spec = Self::expr_to_type_spec(param.default_val)?;
DefaultParamTySpec::new(param_spec, default_spec)
}
(ParamPattern::VarName(name), None) => ParamTySpec::anonymous(TypeSpec::PreDeclTy(
PreDeclTypeSpec::Simple(SimpleTypeSpec::new(name, ConstArgs::empty())),
)),
_ => todo!(),
(ParamPattern::VarName(name), None) => {
let default_spec = Self::expr_to_type_spec(param.default_val)?;
let param_spec =
ParamTySpec::new(Some(name.into_token()), default_spec.clone());
DefaultParamTySpec::new(param_spec, default_spec)
}
(l, r) => todo!("{:?} {:?}", l, r),
};
defaults.push(param);
}