From 98868d39603a96dcad1f942316afcf2077adb333 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 16 Apr 2024 20:35:27 -0500 Subject: [PATCH] cl-ast: allow TyRef to be mutable yaml.rs: Print AddrOf and TyRef the same way --- cl-ast/src/ast_impl.rs | 8 ++++---- cl-ast/src/lib.rs | 1 + cl-parser/src/parser.rs | 2 +- cl-repl/examples/yaml.rs | 9 ++++++--- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/cl-ast/src/ast_impl.rs b/cl-ast/src/ast_impl.rs index 2712f8e..1d57d58 100644 --- a/cl-ast/src/ast_impl.rs +++ b/cl-ast/src/ast_impl.rs @@ -299,11 +299,11 @@ mod display { } impl Display for TyRef { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let Self { count: _, to } = self; - for _ in 0..self.count { + let &Self { count, mutable, ref to } = self; + for _ in 0..count { f.write_char('&')?; } - write!(f, "{to}") + write!(f, "{mutable}{to}") } } impl Display for TyFn { @@ -632,7 +632,7 @@ mod convert { } impl From for VariantKind { u128 => VariantKind::CLike, - Vec => VariantKind::Tuple, + Ty => VariantKind::Tuple, // TODO: enum struct variants } impl From for TyKind { diff --git a/cl-ast/src/lib.rs b/cl-ast/src/lib.rs index 22e24e9..25560ed 100644 --- a/cl-ast/src/lib.rs +++ b/cl-ast/src/lib.rs @@ -243,6 +243,7 @@ pub struct TyTuple { /// A [Ty]pe-reference expression as (number of `&`, [Path]) #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct TyRef { + pub mutable: Mutability, pub count: u16, pub to: Path, } diff --git a/cl-parser/src/parser.rs b/cl-parser/src/parser.rs index add07d8..327a47c 100644 --- a/cl-parser/src/parser.rs +++ b/cl-parser/src/parser.rs @@ -671,7 +671,7 @@ impl<'t> Parser<'t> { } self.consume_peeked(); } - Ok(TyRef { count, to: self.path()? }) + Ok(TyRef { count, mutable: self.mutability()?, to: self.path()? }) } /// [TyFn] = `fn` [TyTuple] (-> [Ty])? pub fn tyfn(&mut self) -> PResult { diff --git a/cl-repl/examples/yaml.rs b/cl-repl/examples/yaml.rs index ae82e30..4594892 100644 --- a/cl-repl/examples/yaml.rs +++ b/cl-repl/examples/yaml.rs @@ -461,8 +461,8 @@ pub mod yamlify { fn yaml(&self, y: &mut Yamler) { let Self { count, mutable, expr } = self; y.key("AddrOf") - .yaml(mutable) .pair("count", count) + .yaml(mutable) .pair("expr", expr); } } @@ -588,8 +588,11 @@ pub mod yamlify { } impl Yamlify for TyRef { fn yaml(&self, y: &mut Yamler) { - let Self { count, to } = self; - y.key("TyRef").pair("count", count).pair("to", to); + let Self { count, mutable, to } = self; + y.key("TyRef") + .pair("count", count) + .yaml(mutable) + .pair("to", to); } } impl Yamlify for TyFn {