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