ast/
stmt.rs

1//! Statements
2//!
3use core::fmt;
4
5use span::Span;
6
7use crate::item::Item;
8use crate::{Block, Expression, Parenthesized};
9
10#[derive(Debug)]
11pub enum StatementKind {
12    Expression(Expression, Span),
13    Item(Box<Item>),
14    Block(Block<Statement>),
15    If {
16        kw_if: Span,
17        cond: Parenthesized<Expression>,
18        if_body: Box<Statement>,
19        kw_else: Option<Span>,
20        else_body: Option<Box<Statement>>,
21    },
22    While {
23        kw_while: Span,
24        cond: Expression,
25        body: Box<Statement>,
26    },
27    For {
28        kw_for: Span,
29        init: Option<Box<Item>>,
30        cond: Option<Expression>,
31        inc: Option<Expression>,
32        body: Box<Statement>,
33    },
34    Empty(Span),
35    Break(Span),
36    Continue(Span),
37    Return {
38        kw_ret: Span,
39        expr: Option<Expression>,
40        semmicollon: Span,
41    },
42}
43
44pub struct Statement {
45    pub kind: StatementKind,
46    pub span: Span,
47}
48
49impl From<Block<Statement>> for Statement {
50    fn from(value: Block<Statement>) -> Self {
51        let span = value.open_brace.join(&value.close_brace);
52        Statement {
53            kind: StatementKind::Block(value),
54            span,
55        }
56    }
57}
58
59impl fmt::Debug for Statement {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:#?}", self.kind) }
61}