1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//! AST definitions for JavaScript statements.
//!
//! The main struct here is [`Statement`], which wraps [`Stmt`] enum.

use crate::derive_enum_from;
use crate::prelude::*;

use crate::source;

use super::{
    Binding,
    Expression,
    FuncBinding,
    Function,
    Identifier,
    LetConst,
    Pattern,
};

/// `Statement` represents an [`Stmt`] together with its source span, if any.
#[derive(Clone, Debug)]
pub struct Statement {
    pub stmt: Stmt,
    pub loc: Option<Box<source::Location>>,
}

impl Statement {
    pub fn with_loc(self, loc: source::Location) -> Self {
        Statement {
            stmt: self.stmt,
            loc: Some(Box::new(loc)),
        }
    }
}

impl PartialEq for Statement {
    fn eq(&self, other: &Self) -> bool {
        self.stmt == other.stmt
    }
}

impl Eq for Statement {}

impl<T> From<T> for Statement
where
    Stmt: From<T>,
{
    fn from(stmt: T) -> Self {
        Statement {
            stmt: Stmt::from(stmt),
            loc: None,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Stmt {
    Empty,
    Block(BlockStatement),
    Expr(ExpressionStatement),
    If(Box<IfStatement>),
    Switch(SwitchStatement),
    For(Box<ForStatement>),
    ForIn(Box<ForInStatement>),
    DoWhile(DoWhileStatement),
    Return(ReturnStatement),
    Break(BreakStatement),
    Continue(ContinueStatement),
    Label(Box<LabelStatement>),
    Throw(ThrowStatement),
    Try(TryStatement),

    // TODO: move declarations out?
    Variable(VariableDeclaration),
    Function(FunctionDeclaration),
}

derive_enum_from!(Stmt::Expr, T: Into<ExpressionStatement>);
derive_enum_from!(Stmt::Variable, VariableDeclaration);
derive_enum_from!(Stmt::Function, FunctionDeclaration);
derive_enum_from!(Stmt::Block, BlockStatement);
derive_enum_from!(Stmt::Return, ReturnStatement);

// ==============================================
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ExpressionStatement {
    pub expression: Expression,
    // TODO: used_ids: Vec<Identifier>,
}

impl<E> From<E> for ExpressionStatement
where
    Expression: From<E>,
{
    fn from(e: E) -> Self {
        let expression = Expression::from(e);
        ExpressionStatement { expression }
    }
}

// ==============================================

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VariableDeclaration {
    pub kind: DeclarationKind,
    pub declarations: Vec<(Identifier, Option<Expression>)>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum DeclarationKind {
    Var,
    Let,
    Const,
}

// ==============================================
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FunctionDeclaration {
    pub func: Rc<Function>,
}

impl FunctionDeclaration {
    pub fn get_name(&self) -> Identifier {
        // must have id:
        self.func.id.as_ref().unwrap().clone()
    }
}

// ==============================================
#[derive(Clone, Debug)]
pub struct BlockStatement {
    pub body: Vec<Statement>,
    pub(super) functions: Vec<FuncBinding>,
    pub(super) bindings: Vec<LetConst>,
}

impl BlockStatement {
    pub fn bindings_iter(&self) -> BlockBindings {
        BlockBindings {
            block: self,
            func_index: 0,
            let_index: 0,
        }
    }
}

impl PartialEq for BlockStatement {
    fn eq(&self, other: &Self) -> bool {
        self.body == other.body
    }
}
impl Eq for BlockStatement {}

pub struct BlockBindings<'b> {
    block: &'b BlockStatement,
    func_index: usize,
    let_index: usize,
}

impl<'b> Iterator for BlockBindings<'b> {
    type Item = Binding;

    fn next(&mut self) -> Option<Self::Item> {
        // TODO: keep vector iterator
        if let Some(fb) = self.block.functions.get(self.func_index) {
            self.func_index += 1;
            return Some(Binding::from(fb.clone()));
        }
        if let Some(lb) = self.block.bindings.get(self.let_index) {
            self.let_index += 1;
            return Some(Binding::from(lb.clone()));
        }
        None
    }
}

// ==============================================
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IfStatement {
    pub test: Expression,
    pub consequent: Statement,
    pub alternate: Option<Statement>,
}

// ==============================================
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SwitchStatement {
    pub discriminant: Expression,
    pub cases: Vec<SwitchCase>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SwitchCase {
    pub test: Option<Expression>,
    pub consequent: Vec<Statement>,
}

// ==============================================
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ForStatement {
    pub init: Option<ForTarget>,
    pub test: Option<Expression>,
    pub update: Option<Expression>,
    pub body: Statement,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DoWhileStatement {
    pub body: Box<Statement>,
    pub test: Option<Expression>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ForInStatement {
    pub left: ForTarget, // VariableDeclaration
    pub right: Expression,
    pub body: Statement,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ForTarget {
    // TODO: this can only contain one variable, change.
    Var(VariableDeclaration),  // for (var i = ...)
    Expr(ExpressionStatement), // for (i = ...;;)
}

// ==============================================
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BreakStatement(pub Option<Identifier>);

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ContinueStatement(pub Option<Identifier>);

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LabelStatement(pub Identifier, pub Statement);

// ==============================================
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReturnStatement(pub Option<Expression>);

// ==============================================
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ThrowStatement(pub Expression);

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TryStatement {
    pub block: BlockStatement,
    pub handler: Option<CatchClause>,
    pub finalizer: Option<BlockStatement>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CatchClause {
    pub param: Pattern,
    pub body: BlockStatement,
}