cl-ast: Separate function *signature* from function bindings, for cl-typeck

Note: this breaks cl-typeck
This commit is contained in:
2024-04-16 20:31:23 -05:00
parent d0ed8309f4
commit 75adbd6473
5 changed files with 72 additions and 51 deletions

View File

@@ -166,9 +166,30 @@ mod display {
}
impl Display for Function {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, args, body, rety } = self;
let Self { name, sign: sign @ TyFn { args, rety }, bind, body } = self;
let types = match **args {
TyKind::Tuple(TyTuple { ref types }) => types.as_slice(),
TyKind::Empty => Default::default(),
_ => {
write!(f, "Invalid function signature: {sign}")?;
Default::default()
}
};
debug_assert_eq!(bind.len(), types.len());
write!(f, "fn {name} ")?;
delimit(separate(args, ", "), INLINE_PARENS)(f)?;
delimit(
|f| {
for (idx, (arg, ty)) in bind.iter().zip(types.iter()).enumerate() {
if idx != 0 {
f.write_str(", ")?;
}
write!(f, "{arg}: {ty}")?;
}
Ok(())
},
INLINE_PARENS,
)(f)?;
if let Some(rety) = rety {
write!(f, " -> {rety}")?;
}
@@ -180,8 +201,8 @@ mod display {
}
impl Display for Param {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { mutability, name, ty } = self;
write!(f, "{mutability}{name}: {ty}")
let Self { mutability, name } = self;
write!(f, "{mutability}{name}")
}
}
impl Display for Struct {

View File

@@ -135,9 +135,9 @@ pub enum ModuleKind {
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Function {
pub name: Identifier,
pub args: Vec<Param>,
pub sign: TyFn,
pub bind: Vec<Param>,
pub body: Option<Block>,
pub rety: Option<Box<Ty>>,
}
/// A single parameter for a [Function]
@@ -145,7 +145,6 @@ pub struct Function {
pub struct Param {
pub mutability: Mutability,
pub name: Identifier,
pub ty: Box<Ty>,
}
/// A user-defined product type