1 | var assert = require('assert');
|
2 |
|
3 | var BlockStatement = require('./BlockStatement');
|
4 | var Identifier = require('./Identifier');
|
5 | var VariableDeclaration = require('./VariableDeclaration');
|
6 |
|
7 | var helpers = require('../helpers');
|
8 |
|
9 | var IfStatement = module.exports = function(test, consequent, alternate) {
|
10 | this.type = 'IfStatement';
|
11 | this.test = test;
|
12 | this.consequent = consequent;
|
13 | this.alternate = alternate;
|
14 | this.async = false;
|
15 | if (!this.alternate) {
|
16 | this.alternate = null;
|
17 | }
|
18 | };
|
19 |
|
20 | IfStatement.prototype.normalize = function (place) {
|
21 |
|
22 | if (this.consequent.type !== 'BlockStatement') {
|
23 | this.consequent = new BlockStatement([this.consequent]);
|
24 | }
|
25 | this.consequent.normalize();
|
26 | if (this.alternate !== null) {
|
27 | if (this.alternate.type !== 'BlockStatement') {
|
28 | this.alternate = new BlockStatement([this.alternate]);
|
29 | }
|
30 | this.alternate.normalize();
|
31 | }
|
32 |
|
33 | place.push(this);
|
34 | };
|
35 |
|
36 | IfStatement.prototype.transform = function (place) {
|
37 | var consequentPlace = this.consequent.transform();
|
38 | this.async = this.async || this.consequent.async;
|
39 |
|
40 | if (this.alternate !== null) {
|
41 | assert(this.alternate.type === 'BlockStatement');
|
42 | var alternatePlace = this.alternate.transform();
|
43 | this.async = this.async || this.alternate.async;
|
44 | }
|
45 |
|
46 | if (this.async) {
|
47 | consequentPlace.push(helpers.continuationStatement);
|
48 | if (this.alternate !== null) {
|
49 | alternatePlace.push(helpers.continuationStatement);
|
50 | } else {
|
51 | this.alternate = new BlockStatement(helpers.continuationStatement);
|
52 | }
|
53 |
|
54 | var nextPlace = [];
|
55 | var ifWrapper = helpers.makeCPS([this], nextPlace);
|
56 | var ifExpression = ifWrapper.expression;
|
57 | var ifContFunc = ifExpression.arguments[0];
|
58 |
|
59 |
|
60 | var errIdentifier = new Identifier(helpers.errName);
|
61 | ifContFunc.params = [errIdentifier];
|
62 |
|
63 |
|
64 | var BinaryExpression = require('./BinaryExpression');
|
65 | var CallExpression = require('./CallExpression');
|
66 | var ReturnStatement = require('./ReturnStatement');
|
67 | var judgeErrorStatement = new IfStatement(
|
68 | new BinaryExpression('!==', errIdentifier, new Identifier('undefined')),
|
69 | new ReturnStatement(new CallExpression(new Identifier(helpers.continuationIdentifier), errIdentifier)),
|
70 | null
|
71 | );
|
72 | nextPlace.push(judgeErrorStatement);
|
73 |
|
74 | place.push(ifWrapper);
|
75 | return nextPlace;
|
76 | } else {
|
77 |
|
78 | place.push(this);
|
79 | return place;
|
80 | }
|
81 | }
|