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
53
54
55
56
57
58
59
60
61
62
63
64
|
use std::fmt::Display;
use crate::lex::token::Location;
use super::expression::Expression;
#[derive(Debug, Clone)]
pub struct Statement {
pub at: Location,
pub kind: StatementKind,
}
#[derive(Debug, Clone)]
pub enum StatementKind {
Expression(Expression),
Print(Expression),
Break(Option<Expression>),
Continue,
Return(Option<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.kind {
StatementKind::Expression(expression) => expression.nested_fmt(f, depth)?,
StatementKind::Print(expression) => {
writeln!(f, "{}Print:", pad)?;
expression.nested_fmt(f, depth + 1)?;
}
StatementKind::Return(expression) => {
if let Some(returned) = expression {
writeln!(f, "{}Return:", pad)?;
returned.nested_fmt(f, depth + 1)?;
} else {
writeln!(f, "{}Return", pad)?;
}
}
StatementKind::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)?;
}
}
StatementKind::Continue => {
writeln!(f, "{}Continue", pad)?;
}
}
Ok(())
}
}
|