spider-src 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +74 -0
- data/README.md +37 -0
- data/Rakefile +25 -0
- data/lib/spider-src/support/spider/.eslintrc +17 -0
- data/lib/spider-src/support/spider/.npmignore +9 -0
- data/lib/spider-src/support/spider/.travis.yml +7 -0
- data/lib/spider-src/support/spider/.yo-rc.json +3 -0
- data/lib/spider-src/support/spider/CHANGELOG.md +64 -0
- data/lib/spider-src/support/spider/Gruntfile.js +97 -0
- data/lib/spider-src/support/spider/Gruntfile.spider +93 -0
- data/lib/spider-src/support/spider/LICENSE +201 -0
- data/lib/spider-src/support/spider/README.md +50 -0
- data/lib/spider-src/support/spider/cli.js +119 -0
- data/lib/spider-src/support/spider/lib/ast/CaseClause.js +179 -0
- data/lib/spider-src/support/spider/lib/ast/CatchClause.js +26 -0
- data/lib/spider-src/support/spider/lib/ast/ExportBatchSpecifier.js +19 -0
- data/lib/spider-src/support/spider/lib/ast/ExportSpecifier.js +33 -0
- data/lib/spider-src/support/spider/lib/ast/ImportDefaultSpecifier.js +23 -0
- data/lib/spider-src/support/spider/lib/ast/ImportNamespaceSpecifier.js +23 -0
- data/lib/spider-src/support/spider/lib/ast/ImportSpecifier.js +40 -0
- data/lib/spider-src/support/spider/lib/ast/Node.js +93 -0
- data/lib/spider-src/support/spider/lib/ast/Parameter.js +229 -0
- data/lib/spider-src/support/spider/lib/ast/Program.js +58 -0
- data/lib/spider-src/support/spider/lib/ast/Property.js +32 -0
- data/lib/spider-src/support/spider/lib/ast/Range.js +257 -0
- data/lib/spider-src/support/spider/lib/ast/VariableDeclarator.js +29 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/ArrayExpression.js +41 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/ArrayPattern.js +45 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/AssignmentExpression.js +29 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/BinaryExpression.js +104 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/CallExpression.js +139 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/ConditionalExpression.js +31 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/CurryCallExpression.js +102 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/ExistentialExpression.js +83 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/ForInExpression.js +116 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/FunctionExpression.js +157 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/InExpression.js +99 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/LogicalExpression.js +50 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/MemberExpression.js +43 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/NewExpression.js +46 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/NullCheckCallExpression.js +132 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/NullCoalescingExpression.js +114 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/NullPropagatingExpression.js +161 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/ObjectExpression.js +39 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/ObjectPattern.js +49 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/RangeMemberExpression.js +157 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/SplatExpression.js +48 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/SuperExpression.js +170 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/ThisExpression.js +22 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/UnaryExpression.js +147 -0
- data/lib/spider-src/support/spider/lib/ast/expressions/UpdateExpression.js +26 -0
- data/lib/spider-src/support/spider/lib/ast/literals/BooleanLiteral.js +24 -0
- data/lib/spider-src/support/spider/lib/ast/literals/Identifier.js +60 -0
- data/lib/spider-src/support/spider/lib/ast/literals/NullLiteral.js +24 -0
- data/lib/spider-src/support/spider/lib/ast/literals/NumberLiteral.js +24 -0
- data/lib/spider-src/support/spider/lib/ast/literals/RegularExpressionLiteral.js +25 -0
- data/lib/spider-src/support/spider/lib/ast/literals/StringLiteral.js +90 -0
- data/lib/spider-src/support/spider/lib/ast/literals/UndefinedLiteral.js +30 -0
- data/lib/spider-src/support/spider/lib/ast/statements/BlockStatement.js +47 -0
- data/lib/spider-src/support/spider/lib/ast/statements/BreakStatement.js +19 -0
- data/lib/spider-src/support/spider/lib/ast/statements/ContinueStatement.js +19 -0
- data/lib/spider-src/support/spider/lib/ast/statements/DebuggerStatement.js +19 -0
- data/lib/spider-src/support/spider/lib/ast/statements/DoWhileStatement.js +25 -0
- data/lib/spider-src/support/spider/lib/ast/statements/ExportDeclarationStatement.js +51 -0
- data/lib/spider-src/support/spider/lib/ast/statements/ExpressionStatement.js +22 -0
- data/lib/spider-src/support/spider/lib/ast/statements/FallthroughStatement.js +74 -0
- data/lib/spider-src/support/spider/lib/ast/statements/ForInStatement.js +79 -0
- data/lib/spider-src/support/spider/lib/ast/statements/ForOfStatement.js +98 -0
- data/lib/spider-src/support/spider/lib/ast/statements/ForStatement.js +43 -0
- data/lib/spider-src/support/spider/lib/ast/statements/FunctionDeclarationStatement.js +104 -0
- data/lib/spider-src/support/spider/lib/ast/statements/GoStatement.js +41 -0
- data/lib/spider-src/support/spider/lib/ast/statements/IfStatement.js +32 -0
- data/lib/spider-src/support/spider/lib/ast/statements/ImportDeclarationStatement.js +40 -0
- data/lib/spider-src/support/spider/lib/ast/statements/PushStatement.js +37 -0
- data/lib/spider-src/support/spider/lib/ast/statements/ReturnStatement.js +26 -0
- data/lib/spider-src/support/spider/lib/ast/statements/SwitchStatement.js +159 -0
- data/lib/spider-src/support/spider/lib/ast/statements/ThrowStatement.js +22 -0
- data/lib/spider-src/support/spider/lib/ast/statements/TryStatement.js +36 -0
- data/lib/spider-src/support/spider/lib/ast/statements/UntilStatement.js +31 -0
- data/lib/spider-src/support/spider/lib/ast/statements/UseStatement.js +49 -0
- data/lib/spider-src/support/spider/lib/ast/statements/VariableDeclarationStatement.js +36 -0
- data/lib/spider-src/support/spider/lib/ast/statements/WhileStatement.js +25 -0
- data/lib/spider-src/support/spider/lib/ast.js +16 -0
- data/lib/spider-src/support/spider/lib/parser.js +14878 -0
- data/lib/spider-src/support/spider/lib/spider.js +217 -0
- data/lib/spider-src/support/spider/package.json +61 -0
- data/lib/spider-src/support/spider/spider-script.js +6 -0
- data/lib/spider-src/support/spider/src/ast/CaseClause.spider +179 -0
- data/lib/spider-src/support/spider/src/ast/CatchClause.spider +30 -0
- data/lib/spider-src/support/spider/src/ast/ExportBatchSpecifier.spider +19 -0
- data/lib/spider-src/support/spider/src/ast/ExportSpecifier.spider +37 -0
- data/lib/spider-src/support/spider/src/ast/ImportDefaultSpecifier.spider +25 -0
- data/lib/spider-src/support/spider/src/ast/ImportNamespaceSpecifier.spider +25 -0
- data/lib/spider-src/support/spider/src/ast/ImportSpecifier.spider +44 -0
- data/lib/spider-src/support/spider/src/ast/Node.spider +104 -0
- data/lib/spider-src/support/spider/src/ast/Parameter.spider +233 -0
- data/lib/spider-src/support/spider/src/ast/Program.spider +109 -0
- data/lib/spider-src/support/spider/src/ast/Property.spider +36 -0
- data/lib/spider-src/support/spider/src/ast/Range.spider +291 -0
- data/lib/spider-src/support/spider/src/ast/VariableDeclarator.spider +33 -0
- data/lib/spider-src/support/spider/src/ast/expressions/ArrayExpression.spider +32 -0
- data/lib/spider-src/support/spider/src/ast/expressions/ArrayPattern.spider +37 -0
- data/lib/spider-src/support/spider/src/ast/expressions/AssignmentExpression.spider +34 -0
- data/lib/spider-src/support/spider/src/ast/expressions/BinaryExpression.spider +125 -0
- data/lib/spider-src/support/spider/src/ast/expressions/CallExpression.spider +149 -0
- data/lib/spider-src/support/spider/src/ast/expressions/ConditionalExpression.spider +34 -0
- data/lib/spider-src/support/spider/src/ast/expressions/CurryCallExpression.spider +109 -0
- data/lib/spider-src/support/spider/src/ast/expressions/ExistentialExpression.spider +102 -0
- data/lib/spider-src/support/spider/src/ast/expressions/ForInExpression.spider +140 -0
- data/lib/spider-src/support/spider/src/ast/expressions/FunctionExpression.spider +183 -0
- data/lib/spider-src/support/spider/src/ast/expressions/InExpression.spider +114 -0
- data/lib/spider-src/support/spider/src/ast/expressions/LogicalExpression.spider +62 -0
- data/lib/spider-src/support/spider/src/ast/expressions/MemberExpression.spider +57 -0
- data/lib/spider-src/support/spider/src/ast/expressions/NewExpression.spider +40 -0
- data/lib/spider-src/support/spider/src/ast/expressions/NullCheckCallExpression.spider +151 -0
- data/lib/spider-src/support/spider/src/ast/expressions/NullCoalescingExpression.spider +151 -0
- data/lib/spider-src/support/spider/src/ast/expressions/NullPropagatingExpression.spider +190 -0
- data/lib/spider-src/support/spider/src/ast/expressions/ObjectExpression.spider +30 -0
- data/lib/spider-src/support/spider/src/ast/expressions/ObjectPattern.spider +42 -0
- data/lib/spider-src/support/spider/src/ast/expressions/RangeMemberExpression.spider +179 -0
- data/lib/spider-src/support/spider/src/ast/expressions/SplatExpression.spider +49 -0
- data/lib/spider-src/support/spider/src/ast/expressions/SuperExpression.spider +235 -0
- data/lib/spider-src/support/spider/src/ast/expressions/ThisExpression.spider +21 -0
- data/lib/spider-src/support/spider/src/ast/expressions/UnaryExpression.spider +158 -0
- data/lib/spider-src/support/spider/src/ast/expressions/UpdateExpression.spider +28 -0
- data/lib/spider-src/support/spider/src/ast/literals/BooleanLiteral.spider +23 -0
- data/lib/spider-src/support/spider/src/ast/literals/Identifier.spider +68 -0
- data/lib/spider-src/support/spider/src/ast/literals/NullLiteral.spider +23 -0
- data/lib/spider-src/support/spider/src/ast/literals/NumberLiteral.spider +23 -0
- data/lib/spider-src/support/spider/src/ast/literals/RegularExpressionLiteral.spider +24 -0
- data/lib/spider-src/support/spider/src/ast/literals/StringLiteral.spider +91 -0
- data/lib/spider-src/support/spider/src/ast/literals/UndefinedLiteral.spider +30 -0
- data/lib/spider-src/support/spider/src/ast/statements/BlockStatement.spider +45 -0
- data/lib/spider-src/support/spider/src/ast/statements/BreakStatement.spider +19 -0
- data/lib/spider-src/support/spider/src/ast/statements/ContinueStatement.spider +19 -0
- data/lib/spider-src/support/spider/src/ast/statements/DebuggerStatement.spider +19 -0
- data/lib/spider-src/support/spider/src/ast/statements/DoWhileStatement.spider +28 -0
- data/lib/spider-src/support/spider/src/ast/statements/ExportDeclarationStatement.spider +46 -0
- data/lib/spider-src/support/spider/src/ast/statements/ExpressionStatement.spider +22 -0
- data/lib/spider-src/support/spider/src/ast/statements/FallthroughStatement.spider +85 -0
- data/lib/spider-src/support/spider/src/ast/statements/ForInStatement.spider +92 -0
- data/lib/spider-src/support/spider/src/ast/statements/ForOfStatement.spider +117 -0
- data/lib/spider-src/support/spider/src/ast/statements/ForStatement.spider +51 -0
- data/lib/spider-src/support/spider/src/ast/statements/FunctionDeclarationStatement.spider +115 -0
- data/lib/spider-src/support/spider/src/ast/statements/GoStatement.spider +44 -0
- data/lib/spider-src/support/spider/src/ast/statements/IfStatement.spider +38 -0
- data/lib/spider-src/support/spider/src/ast/statements/ImportDeclarationStatement.spider +33 -0
- data/lib/spider-src/support/spider/src/ast/statements/PushStatement.spider +40 -0
- data/lib/spider-src/support/spider/src/ast/statements/ReturnStatement.spider +28 -0
- data/lib/spider-src/support/spider/src/ast/statements/SwitchStatement.spider +161 -0
- data/lib/spider-src/support/spider/src/ast/statements/ThrowStatement.spider +23 -0
- data/lib/spider-src/support/spider/src/ast/statements/TryStatement.spider +42 -0
- data/lib/spider-src/support/spider/src/ast/statements/UntilStatement.spider +36 -0
- data/lib/spider-src/support/spider/src/ast/statements/UseStatement.spider +62 -0
- data/lib/spider-src/support/spider/src/ast/statements/VariableDeclarationStatement.spider +34 -0
- data/lib/spider-src/support/spider/src/ast/statements/WhileStatement.spider +28 -0
- data/lib/spider-src/support/spider/src/ast.spider +80 -0
- data/lib/spider-src/support/spider/src/spider.pegjs +1434 -0
- data/lib/spider-src/support/spider/src/spider.spider +276 -0
- data/lib/spider-src/support/spider/test/spider_test.js +1787 -0
- data/lib/spider-src/version.rb +5 -0
- data/lib/spider-src.rb +37 -0
- data/spider-src.gemspec +24 -0
- data/test/test_spider_src.rb +33 -0
- metadata +225 -0
@@ -0,0 +1,34 @@
|
|
1
|
+
use :node;
|
2
|
+
|
3
|
+
var Node = module.require('../Node').Node;
|
4
|
+
|
5
|
+
fn ConditionalExpression(test, consequent, alternate)
|
6
|
+
extends Node {
|
7
|
+
|
8
|
+
this.type = 'ConditionalExpression';
|
9
|
+
|
10
|
+
this.test = test;
|
11
|
+
this.test.parent = this;
|
12
|
+
|
13
|
+
this.consequent = consequent;
|
14
|
+
this.consequent.parent = this;
|
15
|
+
|
16
|
+
this.alternate = alternate;
|
17
|
+
this.alternate.parent = this;
|
18
|
+
}
|
19
|
+
|
20
|
+
ConditionalExpression.prototype.codegen = () -> {
|
21
|
+
if !super.codegen() {
|
22
|
+
return;
|
23
|
+
}
|
24
|
+
|
25
|
+
this.test = this.test.codegen();
|
26
|
+
this.consequent = this.consequent.codegen();
|
27
|
+
this.alternate = this.alternate.codegen();
|
28
|
+
|
29
|
+
return this;
|
30
|
+
};
|
31
|
+
|
32
|
+
ConditionalExpression.prototype.hasCallExpression = () -> true;
|
33
|
+
|
34
|
+
exports.ConditionalExpression = ConditionalExpression;
|
@@ -0,0 +1,109 @@
|
|
1
|
+
use :node;
|
2
|
+
|
3
|
+
var Node = module.require('../Node').Node,
|
4
|
+
CallExpression = module.require('./CallExpression').CallExpression;
|
5
|
+
|
6
|
+
fn CurryCallExpression(callee, args)
|
7
|
+
extends Node {
|
8
|
+
|
9
|
+
this.type = 'CurryCallExpression';
|
10
|
+
|
11
|
+
this.callee = callee;
|
12
|
+
this.args = args;
|
13
|
+
}
|
14
|
+
|
15
|
+
CurryCallExpression.prototype.codegen = () -> {
|
16
|
+
if !super.codegen() {
|
17
|
+
return;
|
18
|
+
}
|
19
|
+
|
20
|
+
var call = new CallExpression(this.callee, this.args);
|
21
|
+
call.parent = this;
|
22
|
+
call = call.codegen();
|
23
|
+
|
24
|
+
if call.type != "CallExpression" or (call?.callee.type == "MemberExpression" and call?.callee.property.name == "apply") {
|
25
|
+
Node.getErrorManager().error({
|
26
|
+
type: "InvalidFunctionCurrying",
|
27
|
+
message: "currying functions with existential operator or splats is disallowed.",
|
28
|
+
loc: this.loc
|
29
|
+
});
|
30
|
+
}
|
31
|
+
|
32
|
+
this.type = "FunctionExpression";
|
33
|
+
this.id = null;
|
34
|
+
this.params = [];
|
35
|
+
this.defaults = [];
|
36
|
+
this.body = {
|
37
|
+
"type": "BlockStatement",
|
38
|
+
"body": [{
|
39
|
+
"type": "ReturnStatement",
|
40
|
+
"argument": {
|
41
|
+
"type": "CallExpression",
|
42
|
+
"callee": {
|
43
|
+
"type": "MemberExpression",
|
44
|
+
"computed": false,
|
45
|
+
"object": call.callee,
|
46
|
+
"property": {
|
47
|
+
"type": "Identifier",
|
48
|
+
"name": "apply"
|
49
|
+
}
|
50
|
+
},
|
51
|
+
"arguments": [
|
52
|
+
{ "type": "ThisExpression" },
|
53
|
+
{
|
54
|
+
"type": "CallExpression",
|
55
|
+
"callee": {
|
56
|
+
"type": "MemberExpression",
|
57
|
+
"computed": false,
|
58
|
+
"object": {
|
59
|
+
"type": "ArrayExpression",
|
60
|
+
"elements": call.arguments,
|
61
|
+
},
|
62
|
+
"property": {
|
63
|
+
"type": "Identifier",
|
64
|
+
"name": "concat"
|
65
|
+
}
|
66
|
+
},
|
67
|
+
"arguments": [{
|
68
|
+
"type": "CallExpression",
|
69
|
+
"callee": {
|
70
|
+
"type": "MemberExpression",
|
71
|
+
"computed": false,
|
72
|
+
"object": {
|
73
|
+
"type": "MemberExpression",
|
74
|
+
"computed": false,
|
75
|
+
"object": {
|
76
|
+
"type": "ArrayExpression",
|
77
|
+
"elements": []
|
78
|
+
},
|
79
|
+
"property": {
|
80
|
+
"type": "Identifier",
|
81
|
+
"name": "slice"
|
82
|
+
}
|
83
|
+
},
|
84
|
+
"property": {
|
85
|
+
"type": "Identifier",
|
86
|
+
"name": "apply"
|
87
|
+
}
|
88
|
+
},
|
89
|
+
"arguments": [{
|
90
|
+
"type": "Identifier",
|
91
|
+
"name": "arguments"
|
92
|
+
}]
|
93
|
+
}]
|
94
|
+
}
|
95
|
+
]
|
96
|
+
}
|
97
|
+
}]
|
98
|
+
};
|
99
|
+
|
100
|
+
this.rest = null;
|
101
|
+
this.generator = false;
|
102
|
+
this.expression = false;
|
103
|
+
|
104
|
+
return this;
|
105
|
+
};
|
106
|
+
|
107
|
+
CurryCallExpression.prototype.hasCallExpression = () -> true;
|
108
|
+
|
109
|
+
exports.CurryCallExpression = CurryCallExpression;
|
@@ -0,0 +1,102 @@
|
|
1
|
+
use :node;
|
2
|
+
|
3
|
+
var Node = module.require('../Node').Node;
|
4
|
+
|
5
|
+
fn ExistentialExpression(argument)
|
6
|
+
extends Node {
|
7
|
+
|
8
|
+
this.type = 'ExistentialExpression';
|
9
|
+
this.argument = argument;
|
10
|
+
this.argument.parent = this;
|
11
|
+
}
|
12
|
+
|
13
|
+
ExistentialExpression.prototype.codegen = () -> {
|
14
|
+
if !super.codegen() {
|
15
|
+
return;
|
16
|
+
}
|
17
|
+
|
18
|
+
var isArgumentCallExpression = this.argument.hasCallExpression?() ?? false;
|
19
|
+
|
20
|
+
this.argument = this.argument.codegen();
|
21
|
+
|
22
|
+
// If the argument has a function call (e.g: a().b)
|
23
|
+
// then store its value in a separate variable to avoid
|
24
|
+
// calling the function twice.
|
25
|
+
if isArgumentCallExpression {
|
26
|
+
var context = this.getContext();
|
27
|
+
|
28
|
+
var id = {
|
29
|
+
"type": "Identifier",
|
30
|
+
"name": ExistentialExpression.getNextVariableName()
|
31
|
+
};
|
32
|
+
|
33
|
+
context.node.body.splice(context.position +
|
34
|
+
(ExistentialExpression.existentialIndex - 2), 0, {
|
35
|
+
"type": "VariableDeclaration",
|
36
|
+
"declarations": [
|
37
|
+
{
|
38
|
+
"type": "VariableDeclarator",
|
39
|
+
"id": id,
|
40
|
+
"init": this.argument
|
41
|
+
}
|
42
|
+
],
|
43
|
+
"kind": "let",
|
44
|
+
"codeGenerated": true
|
45
|
+
});
|
46
|
+
|
47
|
+
this.argument = id;
|
48
|
+
}
|
49
|
+
|
50
|
+
// The generated syntax looks like:
|
51
|
+
// argument === null
|
52
|
+
var nullCheck = {
|
53
|
+
"type": "BinaryExpression",
|
54
|
+
"operator": "!==",
|
55
|
+
"left": this.argument,
|
56
|
+
"right": {
|
57
|
+
"type": "Literal",
|
58
|
+
"value": null,
|
59
|
+
"raw": "null"
|
60
|
+
}
|
61
|
+
};
|
62
|
+
|
63
|
+
// Add undefined check
|
64
|
+
this.type = 'LogicalExpression';
|
65
|
+
this.operator = '&&';
|
66
|
+
this.left = {
|
67
|
+
"type": "BinaryExpression",
|
68
|
+
"operator": "!==",
|
69
|
+
"left": {
|
70
|
+
"type": "UnaryExpression",
|
71
|
+
"operator": "typeof",
|
72
|
+
"argument": this.argument,
|
73
|
+
"prefix": true
|
74
|
+
},
|
75
|
+
"right": {
|
76
|
+
"type": "Literal",
|
77
|
+
"value": "undefined",
|
78
|
+
"raw": "'undefined'"
|
79
|
+
}
|
80
|
+
};
|
81
|
+
|
82
|
+
this.right = nullCheck;
|
83
|
+
return this;
|
84
|
+
};
|
85
|
+
|
86
|
+
ExistentialExpression.prototype.hasCallExpression = () -> {
|
87
|
+
return this.argument?.hasCallExpression?() ?? false;
|
88
|
+
};
|
89
|
+
|
90
|
+
ExistentialExpression.getNextVariableName = () -> {
|
91
|
+
if !this.existentialIndex? {
|
92
|
+
this.existentialIndex = 0;
|
93
|
+
}
|
94
|
+
|
95
|
+
return "existential" + this.existentialIndex++;
|
96
|
+
};
|
97
|
+
|
98
|
+
ExistentialExpression.resetVariableNames = () -> {
|
99
|
+
this.existentialIndex = 0;
|
100
|
+
};
|
101
|
+
|
102
|
+
exports.ExistentialExpression = ExistentialExpression;
|
@@ -0,0 +1,140 @@
|
|
1
|
+
use :node;
|
2
|
+
|
3
|
+
var Node = module.require('../Node').Node,
|
4
|
+
ForInStatement = module.require('../statements/ForInStatement').ForInStatement,
|
5
|
+
BlockStatement = module.require('../statements/BlockStatement').BlockStatement;
|
6
|
+
|
7
|
+
fn ForInExpression(expression, item, index, array, condition)
|
8
|
+
extends Node {
|
9
|
+
|
10
|
+
this.type = 'ForInExpression';
|
11
|
+
|
12
|
+
this.expression = expression;
|
13
|
+
this.expression.parent = this;
|
14
|
+
|
15
|
+
this.item = item;
|
16
|
+
this.item.parent = this;
|
17
|
+
|
18
|
+
this.index = index;
|
19
|
+
if this.index? {
|
20
|
+
this.index.parent = this;
|
21
|
+
}
|
22
|
+
|
23
|
+
this.array = array;
|
24
|
+
this.array.parent = this;
|
25
|
+
|
26
|
+
this.condition = condition;
|
27
|
+
if this.condition? {
|
28
|
+
this.condition.parent = this;
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
32
|
+
ForInExpression.prototype.codegen = () -> {
|
33
|
+
if !super.codegen() {
|
34
|
+
return;
|
35
|
+
}
|
36
|
+
|
37
|
+
this.defineIdentifier(this.item);
|
38
|
+
|
39
|
+
if this.index? {
|
40
|
+
this.defineIdentifier(this.index);
|
41
|
+
}
|
42
|
+
|
43
|
+
var id = {
|
44
|
+
"type": "Identifier",
|
45
|
+
"name": ForInExpression.getNextVariableName(),
|
46
|
+
"codeGenerated": true
|
47
|
+
};
|
48
|
+
|
49
|
+
var pushStatement = {
|
50
|
+
"type": "ExpressionStatement",
|
51
|
+
"codeGenerated": true,
|
52
|
+
"expression": {
|
53
|
+
"type": "CallExpression",
|
54
|
+
"callee": {
|
55
|
+
"type": "MemberExpression",
|
56
|
+
"computed": false,
|
57
|
+
"object": id,
|
58
|
+
"property": {
|
59
|
+
"type": "Identifier",
|
60
|
+
"name": "push"
|
61
|
+
}
|
62
|
+
},
|
63
|
+
"arguments": [this.expression.codegen()]
|
64
|
+
}
|
65
|
+
};
|
66
|
+
|
67
|
+
if this.condition? {
|
68
|
+
pushStatement = {
|
69
|
+
"type": "IfStatement",
|
70
|
+
"codeGenerated": true,
|
71
|
+
"test": this.condition.codegen(),
|
72
|
+
"consequent": {
|
73
|
+
"type": "BlockStatement",
|
74
|
+
"body": [pushStatement]
|
75
|
+
},
|
76
|
+
"alternate": null
|
77
|
+
};
|
78
|
+
}
|
79
|
+
|
80
|
+
var forInStatement = new ForInStatement(this.item, this.index, this.array,
|
81
|
+
new BlockStatement([pushStatement]));
|
82
|
+
|
83
|
+
var fnBlock = new BlockStatement([
|
84
|
+
{
|
85
|
+
"type": "VariableDeclaration",
|
86
|
+
"declarations": [
|
87
|
+
{
|
88
|
+
"type": "VariableDeclarator",
|
89
|
+
"id": id,
|
90
|
+
"init": {
|
91
|
+
"type": "ArrayExpression",
|
92
|
+
"elements": []
|
93
|
+
}
|
94
|
+
}
|
95
|
+
],
|
96
|
+
"kind": "let",
|
97
|
+
"codeGenerated": true
|
98
|
+
},
|
99
|
+
forInStatement,
|
100
|
+
{
|
101
|
+
"type": "ReturnStatement",
|
102
|
+
"codeGenerated": true,
|
103
|
+
"argument": id
|
104
|
+
}
|
105
|
+
]);
|
106
|
+
|
107
|
+
forInStatement.parent = fnBlock;
|
108
|
+
|
109
|
+
this.type = "CallExpression";
|
110
|
+
this.callee = {
|
111
|
+
"type": "FunctionExpression",
|
112
|
+
"id": null,
|
113
|
+
"params": [],
|
114
|
+
"defaults": [],
|
115
|
+
"body": fnBlock.codegen()
|
116
|
+
};
|
117
|
+
|
118
|
+
::Object.defineProperty(this, 'arguments', {
|
119
|
+
value: [],
|
120
|
+
enumerable: true
|
121
|
+
});
|
122
|
+
|
123
|
+
return this;
|
124
|
+
};
|
125
|
+
|
126
|
+
ForInExpression.prototype.hasCallExpression = () -> true;
|
127
|
+
|
128
|
+
ForInExpression.getNextVariableName = () -> {
|
129
|
+
if !this.forInIndex? {
|
130
|
+
this.forInIndex = 0;
|
131
|
+
}
|
132
|
+
|
133
|
+
return "forIn" + this.forInIndex++;
|
134
|
+
};
|
135
|
+
|
136
|
+
ForInExpression.resetVariableNames = () -> {
|
137
|
+
this.forInIndex = 0;
|
138
|
+
};
|
139
|
+
|
140
|
+
exports.ForInExpression = ForInExpression;
|
@@ -0,0 +1,183 @@
|
|
1
|
+
use :node;
|
2
|
+
|
3
|
+
var Node = module.require('../Node').Node,
|
4
|
+
Parameter = module.require('../Parameter').Parameter,
|
5
|
+
CallExpression = module.require('../expressions/CallExpression').CallExpression;
|
6
|
+
|
7
|
+
fn FunctionExpression (id, params, body, inheritsFrom, operator)
|
8
|
+
extends Node {
|
9
|
+
|
10
|
+
if operator == "=>" {
|
11
|
+
this.type = "ArrowFunctionExpression";
|
12
|
+
} else {
|
13
|
+
this.type = 'FunctionExpression';
|
14
|
+
}
|
15
|
+
|
16
|
+
this.defaults = [];
|
17
|
+
this.rest = null;
|
18
|
+
this.generator = false;
|
19
|
+
this.expression = false;
|
20
|
+
this.operator = operator;
|
21
|
+
|
22
|
+
this.id = id;
|
23
|
+
|
24
|
+
if this.id? {
|
25
|
+
this.id.parent = this;
|
26
|
+
}
|
27
|
+
|
28
|
+
this.body = body;
|
29
|
+
this.params = params;
|
30
|
+
|
31
|
+
body.parent = this;
|
32
|
+
|
33
|
+
for param in params {
|
34
|
+
param.parent = this;
|
35
|
+
}
|
36
|
+
|
37
|
+
this.inheritsFrom = inheritsFrom;
|
38
|
+
|
39
|
+
if this.inheritsFrom? {
|
40
|
+
this.inheritsFrom.parent = this;
|
41
|
+
}
|
42
|
+
|
43
|
+
if this.body.type != 'BlockStatement' {
|
44
|
+
this.autoBlock = true;
|
45
|
+
this.body = {
|
46
|
+
"type": "BlockStatement",
|
47
|
+
"body": [
|
48
|
+
{
|
49
|
+
"type": "ReturnStatement",
|
50
|
+
"argument": this.body
|
51
|
+
}
|
52
|
+
]
|
53
|
+
};
|
54
|
+
|
55
|
+
var self = this;
|
56
|
+
this.getContext = () -> {
|
57
|
+
return {
|
58
|
+
node: self.body,
|
59
|
+
position: -1
|
60
|
+
};
|
61
|
+
};
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
FunctionExpression.prototype.codegen = () -> {
|
66
|
+
if !super.codegen() {
|
67
|
+
return;
|
68
|
+
}
|
69
|
+
|
70
|
+
if this.id? {
|
71
|
+
this.id = this.id.codegen(false);
|
72
|
+
}
|
73
|
+
|
74
|
+
Parameter.generateFunctionBody(this, this.params, this.body, this.defaults);
|
75
|
+
|
76
|
+
if this.autoBlock {
|
77
|
+
this.body.body[0].argument = this.body.body[this.body.body.length - 1].argument.codegen();
|
78
|
+
} else {
|
79
|
+
this.body = this.body.codegen();
|
80
|
+
}
|
81
|
+
|
82
|
+
if this.inheritsFrom? {
|
83
|
+
if this.inheritsFrom.type != 'CallExpression' {
|
84
|
+
this.inheritsFrom = new CallExpression(this.inheritsFrom, []);
|
85
|
+
this.inheritsFrom.parent = this;
|
86
|
+
}
|
87
|
+
|
88
|
+
var context = this.getContext();
|
89
|
+
|
90
|
+
this.body.body.splice(0, 0, {
|
91
|
+
"type": "ExpressionStatement",
|
92
|
+
"expression": {
|
93
|
+
"type": "CallExpression",
|
94
|
+
"callee": {
|
95
|
+
"type": "MemberExpression",
|
96
|
+
"computed": false,
|
97
|
+
"object": this.inheritsFrom.callee,
|
98
|
+
"property": {
|
99
|
+
"type": "Identifier",
|
100
|
+
"name": "call"
|
101
|
+
}
|
102
|
+
},
|
103
|
+
"arguments": [
|
104
|
+
{ "type": "ThisExpression" }
|
105
|
+
].concat(this.inheritsFrom.arguments)
|
106
|
+
}
|
107
|
+
});
|
108
|
+
|
109
|
+
var id = {
|
110
|
+
"type": "Identifier",
|
111
|
+
"name": FunctionExpression.getNextVariableName()
|
112
|
+
};
|
113
|
+
|
114
|
+
context.node.body.splice(context.position, 0, {
|
115
|
+
"type": "VariableDeclaration",
|
116
|
+
"declarations": [
|
117
|
+
{
|
118
|
+
"type": "VariableDeclarator",
|
119
|
+
"id": id,
|
120
|
+
"init": this
|
121
|
+
}
|
122
|
+
],
|
123
|
+
"kind": "let",
|
124
|
+
"codeGenerated": true
|
125
|
+
});
|
126
|
+
|
127
|
+
context.node.body.splice(context.position + 1, 0, {
|
128
|
+
"type": "ExpressionStatement",
|
129
|
+
"codeGenerated": "true",
|
130
|
+
"expression": {
|
131
|
+
"type": "AssignmentExpression",
|
132
|
+
"operator": "=",
|
133
|
+
"left": {
|
134
|
+
"type": "MemberExpression",
|
135
|
+
"computed": false,
|
136
|
+
"object": id,
|
137
|
+
"property": {
|
138
|
+
"type": "Identifier",
|
139
|
+
"name": "prototype"
|
140
|
+
}
|
141
|
+
},
|
142
|
+
"right": {
|
143
|
+
"type": "CallExpression",
|
144
|
+
"callee": {
|
145
|
+
"type": "MemberExpression",
|
146
|
+
"computed": false,
|
147
|
+
"object": {
|
148
|
+
"type": "Identifier",
|
149
|
+
"name": "Object"
|
150
|
+
},
|
151
|
+
"property": {
|
152
|
+
"type": "Identifier",
|
153
|
+
"name": "create"
|
154
|
+
}
|
155
|
+
},
|
156
|
+
"arguments": [
|
157
|
+
this.inheritsFrom.callee
|
158
|
+
]
|
159
|
+
}
|
160
|
+
}
|
161
|
+
});
|
162
|
+
|
163
|
+
return id;
|
164
|
+
}
|
165
|
+
|
166
|
+
return this;
|
167
|
+
};
|
168
|
+
|
169
|
+
FunctionExpression.prototype.hasCallExpression = () -> true;
|
170
|
+
|
171
|
+
FunctionExpression.getNextVariableName = () -> {
|
172
|
+
if !this.functionExpressionIndex? {
|
173
|
+
this.functionExpressionIndex = 0;
|
174
|
+
}
|
175
|
+
|
176
|
+
return "functionExpression" + this.functionExpressionIndex++;
|
177
|
+
};
|
178
|
+
|
179
|
+
FunctionExpression.resetVariableNames = () -> {
|
180
|
+
this.functionExpressionIndex = 0;
|
181
|
+
};
|
182
|
+
|
183
|
+
exports.FunctionExpression = FunctionExpression;
|
@@ -0,0 +1,114 @@
|
|
1
|
+
use :node;
|
2
|
+
|
3
|
+
var Node = module.require('../Node').Node;
|
4
|
+
|
5
|
+
fn InExpression(left, right)
|
6
|
+
extends Node {
|
7
|
+
|
8
|
+
this.type = 'InExpression';
|
9
|
+
|
10
|
+
this.left = left;
|
11
|
+
this.left.parent = this;
|
12
|
+
|
13
|
+
this.right = right;
|
14
|
+
this.right.parent = this;
|
15
|
+
}
|
16
|
+
|
17
|
+
InExpression.prototype.codegen = () -> {
|
18
|
+
if !super.codegen() {
|
19
|
+
return;
|
20
|
+
}
|
21
|
+
|
22
|
+
this.left = this.left.codegen();
|
23
|
+
this.right = this.right.codegen();
|
24
|
+
|
25
|
+
if this.right.hasCallExpression?() {
|
26
|
+
var context = this.getContext();
|
27
|
+
|
28
|
+
var id = {
|
29
|
+
"type": "Identifier",
|
30
|
+
"name": InExpression.getNextVariableName(),
|
31
|
+
"codeGenerated": true
|
32
|
+
};
|
33
|
+
|
34
|
+
context.node.body.splice(context.position +
|
35
|
+
(InExpression.inExpressionIndex - 2), 0, {
|
36
|
+
"type": "VariableDeclaration",
|
37
|
+
"declarations": [
|
38
|
+
{
|
39
|
+
"type": "VariableDeclarator",
|
40
|
+
"id": id,
|
41
|
+
"init": this.right
|
42
|
+
}
|
43
|
+
],
|
44
|
+
"kind": "let",
|
45
|
+
"codeGenerated": true
|
46
|
+
});
|
47
|
+
|
48
|
+
this.right = id;
|
49
|
+
}
|
50
|
+
|
51
|
+
this.type = "ConditionalExpression";
|
52
|
+
this.test = {
|
53
|
+
"type": "BinaryExpression",
|
54
|
+
"operator": "instanceof",
|
55
|
+
"left": this.right,
|
56
|
+
"right": {
|
57
|
+
"type": "Identifier",
|
58
|
+
"name": "Array"
|
59
|
+
}
|
60
|
+
};
|
61
|
+
|
62
|
+
this.consequent = {
|
63
|
+
"type": "BinaryExpression",
|
64
|
+
"operator": "!==",
|
65
|
+
"left": {
|
66
|
+
"type": "CallExpression",
|
67
|
+
"callee": {
|
68
|
+
"type": "MemberExpression",
|
69
|
+
"computed": false,
|
70
|
+
"object": this.right,
|
71
|
+
"property": {
|
72
|
+
"type": "Identifier",
|
73
|
+
"name": "indexOf"
|
74
|
+
}
|
75
|
+
},
|
76
|
+
"arguments": [this.left]
|
77
|
+
},
|
78
|
+
"right": {
|
79
|
+
"type": "UnaryExpression",
|
80
|
+
"operator": "-",
|
81
|
+
"argument": {
|
82
|
+
"type": "Literal",
|
83
|
+
"value": 1,
|
84
|
+
"raw": "1"
|
85
|
+
},
|
86
|
+
"prefix": true
|
87
|
+
}
|
88
|
+
};
|
89
|
+
|
90
|
+
this.alternate = {
|
91
|
+
"type": "BinaryExpression",
|
92
|
+
"operator": "in",
|
93
|
+
"left": this.left,
|
94
|
+
"right": this.right
|
95
|
+
};
|
96
|
+
|
97
|
+
return this;
|
98
|
+
};
|
99
|
+
|
100
|
+
InExpression.prototype.hasCallExpression = () -> true;
|
101
|
+
|
102
|
+
InExpression.getNextVariableName = () -> {
|
103
|
+
if !this.inExpressionIndex? {
|
104
|
+
this.inExpressionIndex = 0;
|
105
|
+
}
|
106
|
+
|
107
|
+
return "inExpression" + this.inExpressionIndex++;
|
108
|
+
};
|
109
|
+
|
110
|
+
InExpression.resetVariableNames = () -> {
|
111
|
+
this.inExpressionIndex = 0;
|
112
|
+
};
|
113
|
+
|
114
|
+
exports.InExpression = InExpression;
|