cl-ast: Add syntax support for generics

This commit is contained in:
2025-04-22 07:22:44 -04:00
parent 681fbc88d3
commit 8ff17fd475
14 changed files with 124 additions and 36 deletions

View File

@@ -112,6 +112,16 @@ impl Display for ItemKind {
}
}
impl Display for Generics {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Generics { vars } = self;
if !vars.is_empty() {
separate(vars, ", ")(f.delimit_with("<", ">"))?
}
Ok(())
}
}
impl Display for Alias {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, from } = self;
@@ -152,7 +162,7 @@ impl Display for Module {
impl Display for Function {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, sign: sign @ TyFn { args, rety }, bind, body } = self;
let Self { name, gens, sign: sign @ TyFn { args, rety }, bind, body } = self;
let types = match **args {
TyKind::Tuple(TyTuple { ref types }) => types.as_slice(),
TyKind::Empty => Default::default(),
@@ -170,7 +180,7 @@ impl Display for Function {
};
debug_assert_eq!(bind.len(), types.len());
write!(f, "fn {name} ")?;
write!(f, "fn {name}{gens} ")?;
{
let mut f = f.delimit(INLINE_PARENS);
@@ -193,8 +203,8 @@ impl Display for Function {
impl Display for Struct {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, kind } = self;
write!(f, "struct {name}{kind}")
let Self { name, gens, kind } = self;
write!(f, "struct {name}{gens}{kind}")
}
}
@@ -217,8 +227,8 @@ impl Display for StructMember {
impl Display for Enum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { name, variants: kind } = self;
write!(f, "enum {name}")?;
let Self { name, gens, variants: kind } = self;
write!(f, "enum {name}{gens}")?;
match kind {
Some(v) => separate(v, ",\n")(f.delimit(SPACED_BRACES)),
None => ";".fmt(f),

View File

@@ -65,6 +65,13 @@ impl WeightOf for ItemKind {
}
}
impl WeightOf for Generics {
fn weight_of(&self) -> usize {
let Self { vars } = self;
vars.iter().map(|v| v.weight_of()).sum()
}
}
impl WeightOf for Module {
fn weight_of(&self) -> usize {
let Self { name, file } = self;
@@ -95,15 +102,15 @@ impl WeightOf for Static {
impl WeightOf for Function {
fn weight_of(&self) -> usize {
let Self { name, sign, bind, body } = self;
name.weight_of() + sign.weight_of() + bind.weight_of() + body.weight_of()
let Self { name, gens, sign, bind, body } = self;
name.weight_of() + gens.weight_of() + sign.weight_of() + bind.weight_of() + body.weight_of()
}
}
impl WeightOf for Struct {
fn weight_of(&self) -> usize {
let Self { name, kind } = self;
name.weight_of() + kind.weight_of()
let Self { name, gens, kind } = self;
name.weight_of() + gens.weight_of() + kind.weight_of()
}
}
@@ -126,8 +133,9 @@ impl WeightOf for StructMember {
impl WeightOf for Enum {
fn weight_of(&self) -> usize {
let Self { name, variants } = self;
let Self { name, gens, variants } = self;
name.weight_of()
+ gens.weight_of()
+ variants
.as_ref()
.map_or(size_of_val(variants), |v| v.weight_of())