adding spart fmt to struct ctors

This commit is contained in:
mlokr 2024-06-25 21:51:41 +02:00
parent 2099fc8949
commit 0e91c87a61

View file

@ -397,11 +397,13 @@ impl<'a, 'b> Parser<'a, 'b> {
let val = s.expr(); let val = s.expr();
(Some(s.move_str(name)), val) (Some(s.move_str(name)), val)
}), }),
trailing_comma: std::mem::take(&mut self.trailing_sep),
}, },
T::Tupl => E::Ctor { T::Tupl => E::Ctor {
pos: token.start, pos: token.start,
ty: Some(self.arena.alloc(expr)), ty: Some(self.arena.alloc(expr)),
fields: self.collect_list(T::Comma, T::RParen, |s| (None, s.expr())), fields: self.collect_list(T::Comma, T::RParen, |s| (None, s.expr())),
trailing_comma: std::mem::take(&mut self.trailing_sep),
}, },
T::Dot => E::Field { T::Dot => E::Field {
target: self.arena.alloc(expr), target: self.arena.alloc(expr),
@ -652,6 +654,7 @@ generate_expr! {
pos: Pos, pos: Pos,
ty: Option<&'a Self>, ty: Option<&'a Self>,
fields: &'a [(Option<&'a str>, Self)], fields: &'a [(Option<&'a str>, Self)],
trailing_comma: bool,
}, },
Field { Field {
target: &'a Self, target: &'a Self,
@ -799,28 +802,33 @@ impl<'a> std::fmt::Display for Expr<'a> {
write!(f, "struct {{")?; write!(f, "struct {{")?;
fmt_list(f, "}", fields, |(name, val), f| write!(f, "{name}: {val}",)) fmt_list(f, "}", fields, |(name, val), f| write!(f, "{name}: {val}",))
} }
Self::Ctor { ty, fields, .. } => { Self::Ctor {
ty,
fields,
trailing_comma,
..
} => {
let (left, rith) = if fields.iter().any(|(name, _)| name.is_some()) { let (left, rith) = if fields.iter().any(|(name, _)| name.is_some()) {
('{', '}') ('{', "}")
} else { } else {
('(', ')') ('(', ")")
}; };
if let Some(ty) = ty { if let Some(ty) = ty {
write!(f, "{}", Unary(ty))?; write!(f, "{}", Unary(ty))?;
} }
write!(f, ".{left}")?; write!(f, ".{left}")?;
let first = &mut true; let fmt_field = |(name, val): &_, f: &mut std::fmt::Formatter| {
for (name, val) in fields {
if !std::mem::take(first) {
write!(f, ", ")?;
}
if let Some(name) = name { if let Some(name) = name {
write!(f, "{name}: ")?; write!(f, "{name}: ")?;
} }
write!(f, "{val}")?; write!(f, "{val}")
};
if trailing_comma {
fmt_trailing_list(f, rith, fields, fmt_field)
} else {
fmt_list(f, rith, fields, fmt_field)
} }
write!(f, "{rith}")
} }
Self::UnOp { op, val, .. } => write!(f, "{op}{}", Unary(val)), Self::UnOp { op, val, .. } => write!(f, "{op}{}", Unary(val)),
Self::Break { .. } => write!(f, "break;"), Self::Break { .. } => write!(f, "break;"),
@ -1206,5 +1214,8 @@ mod test {
some_ordinary_code => "loft := fn(): int return loft(1, 2, 3);\n"; some_ordinary_code => "loft := fn(): int return loft(1, 2, 3);\n";
some_arg_per_line_code => "loft := fn(): int return loft(\ some_arg_per_line_code => "loft := fn(): int return loft(\
\n\t1,\n\t2,\n\t3,\n);\n"; \n\t1,\n\t2,\n\t3,\n);\n";
some_ordinary_struct => "loft := fn(): int return loft.{a: 1, b: 2};\n";
some_ordinary_fild_per_lin_struct => "loft := fn(): int return loft.{\
\n\ta: 1,\n\tb: 2,\n};\n";
} }
} }