about summary refs log tree commit diff
path: root/src/parse/ast/statement.rs
blob: eec589e56e8d48b6aba5f33c4737636894ff2555 (plain)
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
use std::fmt::Display;

use super::expression::Expression;

#[derive(Debug)]
pub enum Statement {
    Expression(Expression),
    Print(Expression),
    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)?;
            }
        }

        Ok(())
    }
}