Skip to content

Commit

Permalink
ast/tree: Tree print-based Display implementations + trace prints
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkajetanp committed Sep 10, 2024
1 parent b8fe307 commit 8c6aba0
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions src/ast/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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),
Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8c6aba0

Please sign in to comment.