Skip to main content

mist_parser/ast/
statement.rs

1use serde::Serialize;
2
3use super::*;
4
5#[derive(Debug, Clone, Serialize, Default)]
6pub struct Block(
7    pub Vec<Spanned<Expression>>,
8    pub Option<Spanned<Expression>>,
9);
10
11#[derive(Debug, Clone, Serialize)]
12pub enum Statement {
13    Block(Block),
14    If {
15        initial: StatementBranch,
16        else_if: Vec<StatementBranch>,
17        else_branch: Option<Box<Expression>>,
18    },
19    Loop(Expression),
20    While(StatementBranch),
21    CStyleFor {
22        init: Expression,
23        condition: Expression,
24        update: Expression,
25        body: Expression,
26    },
27    For {
28        mutable: bool,
29        pattern: Pattern,
30        iterator: Expression,
31        body: Box<Expression>,
32    },
33    Match(Expression, Vec<(Vec<Pattern>, Expression)>),
34
35    VarDecl(VarDeclStmt),
36    Return(Option<Expression>),
37    Break,
38    Continue,
39}
40
41#[derive(Debug, Clone, Serialize)]
42pub struct VarDecl {
43    pub mutable: bool,
44    pub name: Pattern,
45    pub type_: Option<TypeExpr>,
46}
47
48#[derive(Debug, Clone, Serialize)]
49pub struct VarDeclStmt {
50    pub decl: VarDecl,
51    pub init: Option<Expression>,
52}
53
54#[derive(Debug, Clone, Serialize)]
55pub struct StatementBranch {
56    pub condition: Expression,
57    pub body: Box<Expression>,
58}
59
60impl Statement {
61    pub fn is_block(&self) -> bool {
62        match self {
63            Self::Block(_) => true,
64            Self::If {
65                initial,
66                else_if,
67                else_branch,
68            } => {
69                else_branch
70                    .as_ref()
71                    .map(|v| v.is_block())
72                    .unwrap_or_default()
73                    || else_if
74                        .last()
75                        .map(|b| b.body.is_block())
76                        .unwrap_or_default()
77                    || initial.body.is_block()
78            }
79            _ => false,
80        }
81    }
82}