Skip to content

Commit

Permalink
ast/print: Move into a TreePrint trait
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkajetanp committed Sep 9, 2024
1 parent 3b705e4 commit e281df0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/ast/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ fn option_ident_to_string(ident: &Option<Identifier>) -> String {
}
}

impl FunctionDeclaration {
pub fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
pub trait TreePrint {
fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result;
}

impl TreePrint for FunctionDeclaration {
fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
let indent = std::iter::repeat(INDENT)
.take(indent_level)
.collect::<String>();
Expand All @@ -39,8 +43,8 @@ impl FunctionDeclaration {
}
}

impl Block {
pub fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
impl TreePrint for Block {
fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
let indent = std::iter::repeat(INDENT)
.take(indent_level)
.collect::<String>();
Expand All @@ -52,8 +56,8 @@ impl Block {
}
}

impl BlockItem {
pub fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
impl TreePrint for BlockItem {
fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
let indent = std::iter::repeat(INDENT)
.take(indent_level)
.collect::<String>();
Expand All @@ -66,8 +70,8 @@ impl BlockItem {
}
}

impl Declaration {
pub fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
impl TreePrint for Declaration {
fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
let indent = std::iter::repeat(INDENT)
.take(indent_level)
.collect::<String>();
Expand All @@ -81,8 +85,8 @@ impl Declaration {
}
}

impl VariableDeclaration {
pub fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
impl TreePrint for VariableDeclaration {
fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
let indent = std::iter::repeat(INDENT)
.take(indent_level)
.collect::<String>();
Expand All @@ -98,8 +102,8 @@ impl VariableDeclaration {
}
}

impl Statement {
pub fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
impl TreePrint for Statement {
fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
let indent = std::iter::repeat(INDENT)
.take(indent_level)
.collect::<String>();
Expand Down Expand Up @@ -177,8 +181,8 @@ impl Statement {
}
}

impl Expression {
pub fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
impl TreePrint for Expression {
fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
let indent = std::iter::repeat(INDENT)
.take(indent_level)
.collect::<String>();
Expand Down Expand Up @@ -230,8 +234,8 @@ impl Expression {
}
}

impl ForInit {
pub fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
impl TreePrint for ForInit {
fn tree_print(&self, f: &mut fmt::Formatter, indent_level: usize) -> fmt::Result {
let indent = std::iter::repeat(INDENT)
.take(indent_level)
.collect::<String>();
Expand Down
1 change: 1 addition & 0 deletions src/ast/tree.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::print::TreePrint;
use crate::lexer::*;
use std::fmt;
use std::mem::discriminant;
Expand Down

0 comments on commit e281df0

Please sign in to comment.