From 89cd1393ed9504ee5d687cb682e54dcc5e9cb438 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 14 Apr 2024 18:01:30 -0500 Subject: [PATCH] cl-ast: Give Impl/ImplKind some love --- cl-ast/src/ast_impl.rs | 25 +++++++++++++++++++++++-- cl-ast/src/lib.rs | 6 +++--- cl-repl/examples/yaml.rs | 10 ++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/cl-ast/src/ast_impl.rs b/cl-ast/src/ast_impl.rs index bc9fded..960e56e 100644 --- a/cl-ast/src/ast_impl.rs +++ b/cl-ast/src/ast_impl.rs @@ -236,8 +236,20 @@ mod display { } } impl Display for Impl { - fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - todo!("impl Display for Impl") + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let Self { target, body } = self; + write!(f, "impl {target} ")?; + delimit(|f| body.fmt(f), BRACES)(f) + } + } + impl Display for ImplKind { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + ImplKind::Type(t) => t.fmt(f), + ImplKind::Trait { impl_trait, for_type } => { + write!(f, "{impl_trait} for {for_type}") + } + } } } @@ -551,6 +563,15 @@ mod convert { Identifier(value.as_ref().into()) } } + impl> From for PathPart { + fn from(value: T) -> Self { + match value.as_ref() { + "Self" => PathPart::SelfKw, + "super" => PathPart::SuperKw, + ident => PathPart::Ident(ident.into()), + } + } + } macro impl_from ($(impl From for $T:ty {$($from:ty => $to:expr),*$(,)?})*) {$($( impl From<$from> for $T { diff --git a/cl-ast/src/lib.rs b/cl-ast/src/lib.rs index be9ac3f..10e1d5c 100644 --- a/cl-ast/src/lib.rs +++ b/cl-ast/src/lib.rs @@ -205,14 +205,14 @@ pub enum VariantKind { /// Sub-[items](Item) (associated functions, etc.) for a [Ty] #[derive(Clone, Debug, PartialEq, Eq)] pub struct Impl { - pub target: Ty, - pub body: Vec, + pub target: ImplKind, + pub body: File, } // TODO: `impl` Trait for { } #[derive(Clone, Debug, PartialEq, Eq)] pub enum ImplKind { - Type(Box), + Type(Ty), Trait { impl_trait: Path, for_type: Box }, } diff --git a/cl-repl/examples/yaml.rs b/cl-repl/examples/yaml.rs index 9027468..4d25979 100644 --- a/cl-repl/examples/yaml.rs +++ b/cl-repl/examples/yaml.rs @@ -312,6 +312,16 @@ pub mod yamlify { y.key("Impl").pair("target", target).pair("body", body); } } + impl Yamlify for ImplKind { + fn yaml(&self, y: &mut Yamler) { + match self { + ImplKind::Type(t) => y.value(t), + ImplKind::Trait { impl_trait, for_type } => { + y.pair("trait", impl_trait).pair("for_type", for_type) + } + }; + } + } impl Yamlify for Block { fn yaml(&self, y: &mut Yamler) { let Self { stmts } = self;