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 is_unsafe: bool,
8
9    pub statements: Vec<Spanned<Expression>>,
10    pub soft_return: Option<Spanned<Expression>>,
11}
12
13#[derive(Debug, Clone, Serialize)]
14pub enum StatementBody {
15    Statement(Expression),
16    Expression(Expression),
17}
18
19#[derive(Debug, Clone, Serialize)]
20pub enum Statement {
21    Block(Block),
22    If {
23        initial: StatementBranch,
24        else_if: Vec<StatementBranch>,
25        else_branch: Option<StatementBody>,
26    },
27    Loop(StatementBody),
28    While(StatementBranch),
29    CStyleFor {
30        init: Expression,
31        condition: Expression,
32        update: Expression,
33        body: StatementBody,
34    },
35    For {
36        pattern: Pattern,
37        iterator: Expression,
38        body: StatementBody,
39    },
40    Match(Expression, Vec<Spanned<MatchItem>>),
41
42    VarDecl(VarDeclStmt),
43    Return(Option<Expression>),
44    Break,
45    Continue,
46}
47
48#[derive(Debug, Clone, Serialize)]
49pub struct MatchItem(pub Vec<Pattern>, pub Expression);
50
51#[derive(Debug, Clone, Serialize)]
52pub struct VarDecl {
53    pub name: Pattern,
54    pub type_: Option<TypeExpr>,
55}
56
57#[derive(Debug, Clone, Serialize)]
58pub struct VarDeclStmt {
59    pub decl: VarDecl,
60    pub init: Option<Expression>,
61}
62
63#[derive(Debug, Clone, Serialize)]
64pub struct StatementBranch {
65    pub condition: Expression,
66    pub body: Box<StatementBody>,
67}
68
69impl Statement {
70    pub fn is_block(&self) -> bool {
71        match self {
72            Self::Block(_)
73            | Self::Match(_, _)
74            | Self::While(_)
75            | Self::For { .. }
76            | Self::Loop(..)
77            | Self::CStyleFor { .. }
78            | Self::If { .. } => true,
79            _ => false,
80        }
81    }
82}
83
84impl StatementBody {
85    pub fn is_soft_return(&self) -> bool {
86        match self {
87            Self::Expression(_) => true,
88            _ => false,
89        }
90    }
91}