1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
use std::fmt::Display;
use super::expression::Expression;
#[derive(Debug, Clone)]
pub enum Statement {
Expression(Expression),
Print(Expression),
Break(Option<Expression>),
Continue,
Return(Expression),
}
impl Display for Statement {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.nested_fmt(f, 0)
}
}
impl Statement {
pub(crate) fn nested_fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
depth: usize,
) -> std::fmt::Result {
let pad = " ".repeat(depth);
match self {
Statement::Expression(expression) => expression.nested_fmt(f, depth)?,
Statement::Print(expression) => {
writeln!(f, "{}Print:", pad)?;
expression.nested_fmt(f, depth + 1)?;
}
Statement::Return(expression) => {
writeln!(f, "{}Return:", pad)?;
expression.nested_fmt(f, depth + 1)?;
}
Statement::Break(expression) => {
if let Some(returned_on_break) = expression {
writeln!(f, "{}Break:", pad)?;
returned_on_break.nested_fmt(f, depth + 1)?;
} else {
writeln!(f, "{}Break", pad)?;
}
}
Statement::Continue => {
writeln!(f, "{}Continue", pad)?;
}
}
Ok(())
}
}
|