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        mutable: bool,
35        pattern: Pattern,
36        iterator: Expression,
37        body: StatementBody,
38    },
39    Match(Expression, Vec<(Vec<Pattern>, Expression)>),
40
41    VarDecl(VarDeclStmt),
42    Return(Option<Expression>),
43    Break,
44    Continue,
45}
46
47#[derive(Debug, Clone, Serialize)]
48pub struct VarDecl {
49    pub mutable: bool,
50    pub name: Pattern,
51    pub type_: Option<TypeExpr>,
52}
53
54#[derive(Debug, Clone, Serialize)]
55pub struct VarDeclStmt {
56    pub decl: VarDecl,
57    pub init: Option<Expression>,
58}
59
60#[derive(Debug, Clone, Serialize)]
61pub struct StatementBranch {
62    pub condition: Expression,
63    pub body: Box<StatementBody>,
64}
65
66impl Statement {
67    pub fn is_block(&self) -> bool {
68        match self {
69            Self::Block(_)
70            | Self::Match(_, _)
71            | Self::While(_)
72            | Self::For { .. }
73            | Self::Loop(..)
74            | Self::CStyleFor { .. }
75            | Self::If { .. } => true,
76            _ => false,
77        }
78    }
79}
80
81impl StatementBody {
82    pub fn is_soft_return(&self) -> bool {
83        match self {
84            Self::Expression(_) => true,
85            _ => false,
86        }
87    }
88}