diff options
| author | Cody <cody@codyq.dev> | 2023-01-29 20:58:04 -0600 |
|---|---|---|
| committer | Cody <cody@codyq.dev> | 2023-01-29 20:58:04 -0600 |
| commit | a653a6602fe5ae5eb4739755db7b34bc92ecdadf (patch) | |
| tree | 2d99c570ccd77767e1d5fcf9b098d45c79cbe6e4 /src/ast/display.rs | |
| parent | b856e5c7226e71e500be5eff7079c5d3b0e9e2f4 (diff) | |
| download | sloth-a653a6602fe5ae5eb4739755db7b34bc92ecdadf.tar.gz | |
More stuffs
Diffstat (limited to 'src/ast/display.rs')
| -rw-r--r-- | src/ast/display.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/ast/display.rs b/src/ast/display.rs new file mode 100644 index 0000000..25b4d23 --- /dev/null +++ b/src/ast/display.rs @@ -0,0 +1,45 @@ +use std::fmt::Display; + +use super::{Expression, Statement, Value}; + +impl<'a> Display for Statement<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{{")?; + let value = match self { + Statement::Val { + identifier, + initializer, + } => format!("val {} {}", identifier.lexeme, initializer), + Statement::Var { + identifier, + initializer, + } => format!("var {} {}", identifier.lexeme, initializer), + Statement::Expression { expr } => expr.to_string(), + }; + write!(f, "{value}")?; + write!(f, "}}")?; + + Ok(()) + } +} + +impl<'a> Display for Expression<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "(")?; + let value = match self { + Expression::Literal(value) => value.0.to_string(), + Expression::Unary { expr, .. } => format!("+ {}", expr), + Expression::Binary { lhs, rhs, .. } => format!("+ {lhs} {rhs}"), + }; + write!(f, "{value}")?; + write!(f, ")")?; + + Ok(()) + } +} + +impl Display for Value { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} |
