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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410

use crate::lexer::span::{ Loc, Span, LineColumn, };
use crate::lexer::token::{
    Identifier, LiteralNull, LiteralBoolean, LiteralString, LiteralNumeric,
    LiteralRegularExpression, 
    Punctuator, Keyword,
};
use crate::ast::expression::{ Expression, };
use crate::ast::class::ClassDeclaration;
use crate::ast::function::FunctionDeclaration;

use std::fmt;


#[derive(PartialEq, Clone, Copy)]
pub enum Statement<'ast> {
    Empty(&'ast EmptyStatement),
    Debugger(&'ast DebuggerStatement),
    
    Expression(&'ast Expression<'ast>),

    Variable(&'ast VariableStatement<'ast>),
    Function(&'ast FunctionDeclaration<'ast>),
    Class(&'ast ClassDeclaration<'ast>),

    Block(&'ast BlockStatement<'ast>),
    If(&'ast IfStatement<'ast>),

    // https://www.ecma-international.org/ecma-262/9.0/index.html#sec-iteration-statements
    DoWhile(&'ast DoWhileStatement<'ast>),
    While(&'ast WhileStatement<'ast>),
    For(&'ast ForStatement<'ast>),
    ForIn(&'ast ForInStatement<'ast>),
    ForOf(&'ast ForOfStatement<'ast>),
    ForAwaitOf(&'ast ForAwaitOfStatement<'ast>),

    Continue(&'ast ContinueStatement<'ast>),
    Break(&'ast BreakStatement<'ast>),
    Return(&'ast ReturnStatement<'ast>),
    With(&'ast WithStatement<'ast>),
    Switch(&'ast SwitchStatement<'ast>),
    Labelled(&'ast LabelledStatement<'ast>),
    Throw(&'ast ThrowStatement<'ast>),
    Try(&'ast TryStatement<'ast>),
}

impl<'ast> fmt::Debug for Statement<'ast> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Statement::Empty(inner) => fmt::Debug::fmt(inner, f),
            Statement::Debugger(inner) => fmt::Debug::fmt(inner, f),
            
            Statement::Expression(inner) => fmt::Debug::fmt(inner, f),

            Statement::Variable(inner) => fmt::Debug::fmt(inner, f),
            Statement::Function(inner) => fmt::Debug::fmt(inner, f),
            Statement::Class(inner) => fmt::Debug::fmt(inner, f),

            Statement::Block(inner) => fmt::Debug::fmt(inner, f),
            Statement::If(inner) => fmt::Debug::fmt(inner, f),

            Statement::DoWhile(inner) => fmt::Debug::fmt(inner, f),
            Statement::While(inner) => fmt::Debug::fmt(inner, f),
            Statement::For(inner) => fmt::Debug::fmt(inner, f),
            Statement::ForIn(inner) => fmt::Debug::fmt(inner, f),
            Statement::ForOf(inner) => fmt::Debug::fmt(inner, f),
            Statement::ForAwaitOf(inner) => fmt::Debug::fmt(inner, f),

            Statement::Continue(inner) => fmt::Debug::fmt(inner, f),
            Statement::Break(inner) => fmt::Debug::fmt(inner, f),
            Statement::Return(inner) => fmt::Debug::fmt(inner, f),
            Statement::With(inner) => fmt::Debug::fmt(inner, f),
            Statement::Switch(inner) => fmt::Debug::fmt(inner, f),
            Statement::Labelled(inner) => fmt::Debug::fmt(inner, f),
            Statement::Throw(inner) => fmt::Debug::fmt(inner, f),
            Statement::Try(inner) => fmt::Debug::fmt(inner, f),
        }
    }
}

impl<'ast> Statement<'ast> {
    pub fn loc(&self) -> Loc {
        match *self {
            Statement::Empty(inner) => inner.loc,
            Statement::Debugger(inner) => inner.loc,
            
            Statement::Expression(inner) => inner.loc(),

            Statement::Variable(inner) => inner.loc,
            Statement::Function(inner) => inner.loc,
            Statement::Class(inner) => inner.loc,

            Statement::Block(inner) => inner.loc,
            Statement::If(inner) => inner.loc,

            Statement::DoWhile(inner) => inner.loc,
            Statement::While(inner) => inner.loc,
            Statement::For(inner) => inner.loc,
            Statement::ForIn(inner) => inner.loc,
            Statement::ForOf(inner) => inner.loc,
            Statement::ForAwaitOf(inner) => inner.loc,

            Statement::Continue(inner) => inner.loc,
            Statement::Break(inner) => inner.loc,
            Statement::Return(inner) => inner.loc,
            Statement::With(inner) => inner.loc,
            Statement::Switch(inner) => inner.loc,
            Statement::Labelled(inner) => inner.loc,
            Statement::Throw(inner) => inner.loc,
            Statement::Try(inner) => inner.loc,
        }
    }

    pub fn span(&self) -> Span {
        match *self {
            Statement::Empty(inner) => inner.span,
            Statement::Debugger(inner) => inner.span,
            
            Statement::Expression(inner) => inner.span(),

            Statement::Variable(inner) => inner.span,
            Statement::Function(inner) => inner.span,
            Statement::Class(inner) => inner.span,
            
            Statement::Block(inner) => inner.span,
            Statement::If(inner) => inner.span,

            Statement::DoWhile(inner) => inner.span,
            Statement::While(inner) => inner.span,
            Statement::For(inner) => inner.span,
            Statement::ForIn(inner) => inner.span,
            Statement::ForOf(inner) => inner.span,
            Statement::ForAwaitOf(inner) => inner.span,

            Statement::Continue(inner) => inner.span,
            Statement::Break(inner) => inner.span,
            Statement::Return(inner) => inner.span,
            Statement::With(inner) => inner.span,
            Statement::Switch(inner) => inner.span,
            Statement::Labelled(inner) => inner.span,
            Statement::Throw(inner) => inner.span,
            Statement::Try(inner) => inner.span,
        }
    }

    pub fn is_declaration(&self) -> bool {
        match *self {
            Statement::Variable(_)
            // | Statement::Function(_)
            // | Statement::Class(_)
            => true,
            _ => false,
        }
    }
    
    pub fn is_expression(&self) -> bool {
        match *self {
            Statement::Expression(_) => true,
            _ => false,
        }
    }

    pub fn is_statement(&self) -> bool {
        !self.is_declaration() && !self.is_expression()
    }

    pub fn is_iteration_statement(&self) -> bool {
        match *self {
            Statement::DoWhile(_)
            | Statement::While(_)
            | Statement::For(_)
            | Statement::ForIn(_)
            | Statement::ForOf(_)
            | Statement::ForAwaitOf(_) => true,
            _ => false,
        }
    }

    pub fn is_breakable_statement(&self) -> bool {
        // https://www.ecma-international.org/ecma-262/9.0/index.html#prod-BreakableStatement
        match *self {
            Statement::Switch(_) => true,
            _ => self.is_iteration_statement(),
        }
    }

    pub fn is_hoistable(&self) -> bool {
        unimplemented!()
        // match *self {
        //     // Statement::Variable(_) ?
        //     Statement::Function(_) => true,
        //     _ => false,
        // }
    }

    pub fn is_hoistable_declaration(&self) -> bool {
        unimplemented!()
        // match *self {
        //     Statement::Function(_) => true,
        //     _ => false,
        // }
    }
}


#[derive(Debug, PartialEq, Clone, Copy)]
pub struct EmptyStatement {
    pub loc: Loc,
    pub span: Span,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct DebuggerStatement {
    pub loc: Loc,
    pub span: Span,
}


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

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct LexicalBinding<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub name: Expression<'ast>,
    pub initializer: Option<Expression<'ast>>,
}


#[derive(Debug, PartialEq, Clone, Copy)]
pub struct VariableStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub kind: LexicalDeclarationKind,
    pub declarators: &'ast [ LexicalBinding<'ast> ],
}

impl<'ast> VariableStatement<'ast> {
    pub fn is_var(&self) -> bool {
        match self.kind {
            LexicalDeclarationKind::Var => true,
            _ => false,
        }
    }

    pub fn is_let(&self) -> bool {
        match self.kind {
            LexicalDeclarationKind::Let => true,
            _ => false,
        }
    }

    pub fn is_const(&self) -> bool {
        match self.kind {
            LexicalDeclarationKind::Const => true,
            _ => false,
        }
    }
}


#[derive(Debug, PartialEq, Clone, Copy)]
pub struct BlockStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub body: &'ast [ Statement<'ast> ],
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct IfStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub condition: Expression<'ast>,
    pub and_then: Statement<'ast>,
    pub or_else: Statement<'ast>,
}

// Iteration Statements
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct DoWhileStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub condition: Expression<'ast>,
    pub body: Statement<'ast>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct WhileStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub condition: Expression<'ast>,
    pub body: Statement<'ast>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ForStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    // initialization
    pub init: Option<Statement<'ast>>,       // var/let/const/expr
    pub condition: Option<Expression<'ast>>,
    pub finally: Option<Expression<'ast>>,
    pub body: Statement<'ast>,
}


#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ForInStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub left: Expression<'ast>,
    pub right: Expression<'ast>,
    pub body: Statement<'ast>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ForOfStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub left: Expression<'ast>,
    pub right: Expression<'ast>,
    pub body: Statement<'ast>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ForAwaitOfStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub left: Expression<'ast>,
    pub right: Expression<'ast>,
    pub body: Statement<'ast>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ContinueStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub label: Option<Identifier<'ast>>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct BreakStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub label: Option<Identifier<'ast>>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ReturnStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub value: Option<Expression<'ast>>,
}

// NOTE: 不推荐的语句!(在编译的时候会报错)
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct WithStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub condition: Expression<'ast>,
    pub then: Statement<'ast>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct SwitchStatementCaseClause<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub value: Expression<'ast>,
    pub body: Statement<'ast>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct SwitchStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub value: Expression<'ast>,
    // default_clause: Option<SwitchStatementCaseClause<'ast>>
    pub clauses: &'ast [ SwitchStatementCaseClause<'ast> ],
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct LabelledStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub label: Identifier<'ast>,
    // FunctionDeclaration is not allowed.
    pub item: Statement<'ast>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ThrowStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub value: Expression<'ast>,
}

#[derive(Debug, PartialEq, Clone)]
pub struct TryStatement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub body: BlockStatement<'ast>,
    pub catch_parameter: Option<Expression<'ast>>,
    pub catch_body: BlockStatement<'ast>,
    pub finally: Option<BlockStatement<'ast>>,
}