From 8c6aba00b26f1329134059d2c73eecc2939b908e Mon Sep 17 00:00:00 2001 From: Kajetan Puchalski Date: Tue, 10 Sep 2024 03:49:19 +0100 Subject: [PATCH] ast/tree: Tree print-based Display implementations + trace prints --- src/ast/tree.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/src/ast/tree.rs b/src/ast/tree.rs index 39f7896..39b865f 100644 --- a/src/ast/tree.rs +++ b/src/ast/tree.rs @@ -112,11 +112,17 @@ impl FunctionDeclaration { body, }; - log::trace!("--- Parsed function declaration: {:?}", func); + log::trace!("--- Parsed function declaration:\n{}", func); Ok(func) } } +impl fmt::Display for FunctionDeclaration { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.tree_print(f, "") + } +} + #[derive(Debug, PartialEq, Clone)] #[allow(dead_code)] pub struct Block { @@ -138,6 +144,12 @@ impl Block { } } +impl fmt::Display for Block { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.tree_print(f, "") + } +} + #[derive(Debug, PartialEq, Clone)] #[allow(dead_code)] pub enum BlockItem { @@ -159,6 +171,12 @@ impl BlockItem { } } +impl fmt::Display for BlockItem { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.tree_print(f, "") + } +} + #[derive(Debug, PartialEq, Clone)] pub enum Declaration { FunDecl(FunctionDeclaration), @@ -176,6 +194,12 @@ impl Declaration { } } +impl fmt::Display for Declaration { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.tree_print(f, "") + } +} + #[derive(Debug, PartialEq, Clone)] pub struct VariableDeclaration { pub name: Identifier, @@ -201,11 +225,17 @@ impl VariableDeclaration { expect_token(TokenKind::Semicolon, tokens)?; let result = Self { name: ident, init }; - log::trace!("-- Parsed declaration: {:?}", result); + log::trace!("-- Parsed declaration:\n{}", result); Ok(result) } } +impl fmt::Display for VariableDeclaration { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.tree_print(f, "") + } +} + #[derive(Debug, PartialEq, Clone)] #[allow(dead_code)] pub enum Statement { @@ -309,11 +339,17 @@ impl Statement { } }; - log::trace!("-- Parsed statement: {:?}", result); + log::trace!("-- Parsed statement:\n{}", result); Ok(result) } } +impl fmt::Display for Statement { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.tree_print(f, "") + } +} + #[derive(Debug, PartialEq, Clone)] #[allow(dead_code)] pub enum ForInit { @@ -337,6 +373,12 @@ impl ForInit { } } +impl fmt::Display for ForInit { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.tree_print(f, "") + } +} + #[derive(Debug, PartialEq, Clone)] #[allow(dead_code)] pub enum Expression { @@ -381,7 +423,7 @@ impl Expression { } token = tokens.front().unwrap().to_owned(); } - log::trace!("-- Parsed expression: {:?}", left); + log::trace!("-- Parsed expression:\n{}", left); Ok(left) } @@ -453,6 +495,12 @@ impl Expression { } } +impl fmt::Display for Expression { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.tree_print(f, "") + } +} + #[derive(Debug, PartialEq, Clone, EnumIs, Display)] pub enum BinaryOperator { Add,