spider-src 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/Gemfile +7 -0
  4. data/LICENSE.txt +74 -0
  5. data/README.md +37 -0
  6. data/Rakefile +25 -0
  7. data/lib/spider-src/support/spider/.eslintrc +17 -0
  8. data/lib/spider-src/support/spider/.npmignore +9 -0
  9. data/lib/spider-src/support/spider/.travis.yml +7 -0
  10. data/lib/spider-src/support/spider/.yo-rc.json +3 -0
  11. data/lib/spider-src/support/spider/CHANGELOG.md +64 -0
  12. data/lib/spider-src/support/spider/Gruntfile.js +97 -0
  13. data/lib/spider-src/support/spider/Gruntfile.spider +93 -0
  14. data/lib/spider-src/support/spider/LICENSE +201 -0
  15. data/lib/spider-src/support/spider/README.md +50 -0
  16. data/lib/spider-src/support/spider/cli.js +119 -0
  17. data/lib/spider-src/support/spider/lib/ast/CaseClause.js +179 -0
  18. data/lib/spider-src/support/spider/lib/ast/CatchClause.js +26 -0
  19. data/lib/spider-src/support/spider/lib/ast/ExportBatchSpecifier.js +19 -0
  20. data/lib/spider-src/support/spider/lib/ast/ExportSpecifier.js +33 -0
  21. data/lib/spider-src/support/spider/lib/ast/ImportDefaultSpecifier.js +23 -0
  22. data/lib/spider-src/support/spider/lib/ast/ImportNamespaceSpecifier.js +23 -0
  23. data/lib/spider-src/support/spider/lib/ast/ImportSpecifier.js +40 -0
  24. data/lib/spider-src/support/spider/lib/ast/Node.js +93 -0
  25. data/lib/spider-src/support/spider/lib/ast/Parameter.js +229 -0
  26. data/lib/spider-src/support/spider/lib/ast/Program.js +58 -0
  27. data/lib/spider-src/support/spider/lib/ast/Property.js +32 -0
  28. data/lib/spider-src/support/spider/lib/ast/Range.js +257 -0
  29. data/lib/spider-src/support/spider/lib/ast/VariableDeclarator.js +29 -0
  30. data/lib/spider-src/support/spider/lib/ast/expressions/ArrayExpression.js +41 -0
  31. data/lib/spider-src/support/spider/lib/ast/expressions/ArrayPattern.js +45 -0
  32. data/lib/spider-src/support/spider/lib/ast/expressions/AssignmentExpression.js +29 -0
  33. data/lib/spider-src/support/spider/lib/ast/expressions/BinaryExpression.js +104 -0
  34. data/lib/spider-src/support/spider/lib/ast/expressions/CallExpression.js +139 -0
  35. data/lib/spider-src/support/spider/lib/ast/expressions/ConditionalExpression.js +31 -0
  36. data/lib/spider-src/support/spider/lib/ast/expressions/CurryCallExpression.js +102 -0
  37. data/lib/spider-src/support/spider/lib/ast/expressions/ExistentialExpression.js +83 -0
  38. data/lib/spider-src/support/spider/lib/ast/expressions/ForInExpression.js +116 -0
  39. data/lib/spider-src/support/spider/lib/ast/expressions/FunctionExpression.js +157 -0
  40. data/lib/spider-src/support/spider/lib/ast/expressions/InExpression.js +99 -0
  41. data/lib/spider-src/support/spider/lib/ast/expressions/LogicalExpression.js +50 -0
  42. data/lib/spider-src/support/spider/lib/ast/expressions/MemberExpression.js +43 -0
  43. data/lib/spider-src/support/spider/lib/ast/expressions/NewExpression.js +46 -0
  44. data/lib/spider-src/support/spider/lib/ast/expressions/NullCheckCallExpression.js +132 -0
  45. data/lib/spider-src/support/spider/lib/ast/expressions/NullCoalescingExpression.js +114 -0
  46. data/lib/spider-src/support/spider/lib/ast/expressions/NullPropagatingExpression.js +161 -0
  47. data/lib/spider-src/support/spider/lib/ast/expressions/ObjectExpression.js +39 -0
  48. data/lib/spider-src/support/spider/lib/ast/expressions/ObjectPattern.js +49 -0
  49. data/lib/spider-src/support/spider/lib/ast/expressions/RangeMemberExpression.js +157 -0
  50. data/lib/spider-src/support/spider/lib/ast/expressions/SplatExpression.js +48 -0
  51. data/lib/spider-src/support/spider/lib/ast/expressions/SuperExpression.js +170 -0
  52. data/lib/spider-src/support/spider/lib/ast/expressions/ThisExpression.js +22 -0
  53. data/lib/spider-src/support/spider/lib/ast/expressions/UnaryExpression.js +147 -0
  54. data/lib/spider-src/support/spider/lib/ast/expressions/UpdateExpression.js +26 -0
  55. data/lib/spider-src/support/spider/lib/ast/literals/BooleanLiteral.js +24 -0
  56. data/lib/spider-src/support/spider/lib/ast/literals/Identifier.js +60 -0
  57. data/lib/spider-src/support/spider/lib/ast/literals/NullLiteral.js +24 -0
  58. data/lib/spider-src/support/spider/lib/ast/literals/NumberLiteral.js +24 -0
  59. data/lib/spider-src/support/spider/lib/ast/literals/RegularExpressionLiteral.js +25 -0
  60. data/lib/spider-src/support/spider/lib/ast/literals/StringLiteral.js +90 -0
  61. data/lib/spider-src/support/spider/lib/ast/literals/UndefinedLiteral.js +30 -0
  62. data/lib/spider-src/support/spider/lib/ast/statements/BlockStatement.js +47 -0
  63. data/lib/spider-src/support/spider/lib/ast/statements/BreakStatement.js +19 -0
  64. data/lib/spider-src/support/spider/lib/ast/statements/ContinueStatement.js +19 -0
  65. data/lib/spider-src/support/spider/lib/ast/statements/DebuggerStatement.js +19 -0
  66. data/lib/spider-src/support/spider/lib/ast/statements/DoWhileStatement.js +25 -0
  67. data/lib/spider-src/support/spider/lib/ast/statements/ExportDeclarationStatement.js +51 -0
  68. data/lib/spider-src/support/spider/lib/ast/statements/ExpressionStatement.js +22 -0
  69. data/lib/spider-src/support/spider/lib/ast/statements/FallthroughStatement.js +74 -0
  70. data/lib/spider-src/support/spider/lib/ast/statements/ForInStatement.js +79 -0
  71. data/lib/spider-src/support/spider/lib/ast/statements/ForOfStatement.js +98 -0
  72. data/lib/spider-src/support/spider/lib/ast/statements/ForStatement.js +43 -0
  73. data/lib/spider-src/support/spider/lib/ast/statements/FunctionDeclarationStatement.js +104 -0
  74. data/lib/spider-src/support/spider/lib/ast/statements/GoStatement.js +41 -0
  75. data/lib/spider-src/support/spider/lib/ast/statements/IfStatement.js +32 -0
  76. data/lib/spider-src/support/spider/lib/ast/statements/ImportDeclarationStatement.js +40 -0
  77. data/lib/spider-src/support/spider/lib/ast/statements/PushStatement.js +37 -0
  78. data/lib/spider-src/support/spider/lib/ast/statements/ReturnStatement.js +26 -0
  79. data/lib/spider-src/support/spider/lib/ast/statements/SwitchStatement.js +159 -0
  80. data/lib/spider-src/support/spider/lib/ast/statements/ThrowStatement.js +22 -0
  81. data/lib/spider-src/support/spider/lib/ast/statements/TryStatement.js +36 -0
  82. data/lib/spider-src/support/spider/lib/ast/statements/UntilStatement.js +31 -0
  83. data/lib/spider-src/support/spider/lib/ast/statements/UseStatement.js +49 -0
  84. data/lib/spider-src/support/spider/lib/ast/statements/VariableDeclarationStatement.js +36 -0
  85. data/lib/spider-src/support/spider/lib/ast/statements/WhileStatement.js +25 -0
  86. data/lib/spider-src/support/spider/lib/ast.js +16 -0
  87. data/lib/spider-src/support/spider/lib/parser.js +14878 -0
  88. data/lib/spider-src/support/spider/lib/spider.js +217 -0
  89. data/lib/spider-src/support/spider/package.json +61 -0
  90. data/lib/spider-src/support/spider/spider-script.js +6 -0
  91. data/lib/spider-src/support/spider/src/ast/CaseClause.spider +179 -0
  92. data/lib/spider-src/support/spider/src/ast/CatchClause.spider +30 -0
  93. data/lib/spider-src/support/spider/src/ast/ExportBatchSpecifier.spider +19 -0
  94. data/lib/spider-src/support/spider/src/ast/ExportSpecifier.spider +37 -0
  95. data/lib/spider-src/support/spider/src/ast/ImportDefaultSpecifier.spider +25 -0
  96. data/lib/spider-src/support/spider/src/ast/ImportNamespaceSpecifier.spider +25 -0
  97. data/lib/spider-src/support/spider/src/ast/ImportSpecifier.spider +44 -0
  98. data/lib/spider-src/support/spider/src/ast/Node.spider +104 -0
  99. data/lib/spider-src/support/spider/src/ast/Parameter.spider +233 -0
  100. data/lib/spider-src/support/spider/src/ast/Program.spider +109 -0
  101. data/lib/spider-src/support/spider/src/ast/Property.spider +36 -0
  102. data/lib/spider-src/support/spider/src/ast/Range.spider +291 -0
  103. data/lib/spider-src/support/spider/src/ast/VariableDeclarator.spider +33 -0
  104. data/lib/spider-src/support/spider/src/ast/expressions/ArrayExpression.spider +32 -0
  105. data/lib/spider-src/support/spider/src/ast/expressions/ArrayPattern.spider +37 -0
  106. data/lib/spider-src/support/spider/src/ast/expressions/AssignmentExpression.spider +34 -0
  107. data/lib/spider-src/support/spider/src/ast/expressions/BinaryExpression.spider +125 -0
  108. data/lib/spider-src/support/spider/src/ast/expressions/CallExpression.spider +149 -0
  109. data/lib/spider-src/support/spider/src/ast/expressions/ConditionalExpression.spider +34 -0
  110. data/lib/spider-src/support/spider/src/ast/expressions/CurryCallExpression.spider +109 -0
  111. data/lib/spider-src/support/spider/src/ast/expressions/ExistentialExpression.spider +102 -0
  112. data/lib/spider-src/support/spider/src/ast/expressions/ForInExpression.spider +140 -0
  113. data/lib/spider-src/support/spider/src/ast/expressions/FunctionExpression.spider +183 -0
  114. data/lib/spider-src/support/spider/src/ast/expressions/InExpression.spider +114 -0
  115. data/lib/spider-src/support/spider/src/ast/expressions/LogicalExpression.spider +62 -0
  116. data/lib/spider-src/support/spider/src/ast/expressions/MemberExpression.spider +57 -0
  117. data/lib/spider-src/support/spider/src/ast/expressions/NewExpression.spider +40 -0
  118. data/lib/spider-src/support/spider/src/ast/expressions/NullCheckCallExpression.spider +151 -0
  119. data/lib/spider-src/support/spider/src/ast/expressions/NullCoalescingExpression.spider +151 -0
  120. data/lib/spider-src/support/spider/src/ast/expressions/NullPropagatingExpression.spider +190 -0
  121. data/lib/spider-src/support/spider/src/ast/expressions/ObjectExpression.spider +30 -0
  122. data/lib/spider-src/support/spider/src/ast/expressions/ObjectPattern.spider +42 -0
  123. data/lib/spider-src/support/spider/src/ast/expressions/RangeMemberExpression.spider +179 -0
  124. data/lib/spider-src/support/spider/src/ast/expressions/SplatExpression.spider +49 -0
  125. data/lib/spider-src/support/spider/src/ast/expressions/SuperExpression.spider +235 -0
  126. data/lib/spider-src/support/spider/src/ast/expressions/ThisExpression.spider +21 -0
  127. data/lib/spider-src/support/spider/src/ast/expressions/UnaryExpression.spider +158 -0
  128. data/lib/spider-src/support/spider/src/ast/expressions/UpdateExpression.spider +28 -0
  129. data/lib/spider-src/support/spider/src/ast/literals/BooleanLiteral.spider +23 -0
  130. data/lib/spider-src/support/spider/src/ast/literals/Identifier.spider +68 -0
  131. data/lib/spider-src/support/spider/src/ast/literals/NullLiteral.spider +23 -0
  132. data/lib/spider-src/support/spider/src/ast/literals/NumberLiteral.spider +23 -0
  133. data/lib/spider-src/support/spider/src/ast/literals/RegularExpressionLiteral.spider +24 -0
  134. data/lib/spider-src/support/spider/src/ast/literals/StringLiteral.spider +91 -0
  135. data/lib/spider-src/support/spider/src/ast/literals/UndefinedLiteral.spider +30 -0
  136. data/lib/spider-src/support/spider/src/ast/statements/BlockStatement.spider +45 -0
  137. data/lib/spider-src/support/spider/src/ast/statements/BreakStatement.spider +19 -0
  138. data/lib/spider-src/support/spider/src/ast/statements/ContinueStatement.spider +19 -0
  139. data/lib/spider-src/support/spider/src/ast/statements/DebuggerStatement.spider +19 -0
  140. data/lib/spider-src/support/spider/src/ast/statements/DoWhileStatement.spider +28 -0
  141. data/lib/spider-src/support/spider/src/ast/statements/ExportDeclarationStatement.spider +46 -0
  142. data/lib/spider-src/support/spider/src/ast/statements/ExpressionStatement.spider +22 -0
  143. data/lib/spider-src/support/spider/src/ast/statements/FallthroughStatement.spider +85 -0
  144. data/lib/spider-src/support/spider/src/ast/statements/ForInStatement.spider +92 -0
  145. data/lib/spider-src/support/spider/src/ast/statements/ForOfStatement.spider +117 -0
  146. data/lib/spider-src/support/spider/src/ast/statements/ForStatement.spider +51 -0
  147. data/lib/spider-src/support/spider/src/ast/statements/FunctionDeclarationStatement.spider +115 -0
  148. data/lib/spider-src/support/spider/src/ast/statements/GoStatement.spider +44 -0
  149. data/lib/spider-src/support/spider/src/ast/statements/IfStatement.spider +38 -0
  150. data/lib/spider-src/support/spider/src/ast/statements/ImportDeclarationStatement.spider +33 -0
  151. data/lib/spider-src/support/spider/src/ast/statements/PushStatement.spider +40 -0
  152. data/lib/spider-src/support/spider/src/ast/statements/ReturnStatement.spider +28 -0
  153. data/lib/spider-src/support/spider/src/ast/statements/SwitchStatement.spider +161 -0
  154. data/lib/spider-src/support/spider/src/ast/statements/ThrowStatement.spider +23 -0
  155. data/lib/spider-src/support/spider/src/ast/statements/TryStatement.spider +42 -0
  156. data/lib/spider-src/support/spider/src/ast/statements/UntilStatement.spider +36 -0
  157. data/lib/spider-src/support/spider/src/ast/statements/UseStatement.spider +62 -0
  158. data/lib/spider-src/support/spider/src/ast/statements/VariableDeclarationStatement.spider +34 -0
  159. data/lib/spider-src/support/spider/src/ast/statements/WhileStatement.spider +28 -0
  160. data/lib/spider-src/support/spider/src/ast.spider +80 -0
  161. data/lib/spider-src/support/spider/src/spider.pegjs +1434 -0
  162. data/lib/spider-src/support/spider/src/spider.spider +276 -0
  163. data/lib/spider-src/support/spider/test/spider_test.js +1787 -0
  164. data/lib/spider-src/version.rb +5 -0
  165. data/lib/spider-src.rb +37 -0
  166. data/spider-src.gemspec +24 -0
  167. data/test/test_spider_src.rb +33 -0
  168. metadata +225 -0
@@ -0,0 +1,291 @@
1
+ use :node;
2
+
3
+ var Node = module.require('./Node').Node;
4
+
5
+ fn Range(start, operator, to)
6
+ extends Node {
7
+
8
+ this.type = 'Range';
9
+ this.operator = operator;
10
+
11
+ this.start = start;
12
+
13
+ if this.start? {
14
+ this.start.parent = this;
15
+ }
16
+
17
+ this.to = to;
18
+
19
+ if this.to? {
20
+ this.to.parent = this;
21
+ }
22
+ }
23
+
24
+ Range.prototype.codegen = () -> {
25
+ if !super.codegen() {
26
+ return;
27
+ }
28
+
29
+ var isNumber = (n) -> !::isNaN(::parseFloat(n)) && ::isFinite(n);
30
+ var isNumeric = isNumber(this.start.value) && isNumber(this.to.value);
31
+
32
+ this.start = this.start.codegen();
33
+ this.to = this.to.codegen();
34
+
35
+ var updateOperator, testOperator;
36
+
37
+ if isNumeric {
38
+ updateOperator = '++' if this.start.value < this.to.value else '--';
39
+ testOperator = '<' if this.start.value < this.to.value else '>';
40
+
41
+ if this.operator == '..' {
42
+ testOperator += '=';
43
+ }
44
+ }
45
+
46
+ if isNumeric && ::Math.abs(this.to.value - this.start.value) <= 20 {
47
+ this.type = 'ArrayExpression';
48
+ this.elements = [];
49
+
50
+ var test = (i) -> {
51
+ switch testOperator {
52
+ case '>': {
53
+ return i > this.to.value;
54
+ },
55
+
56
+ case '<': {
57
+ return i < this.to.value;
58
+ },
59
+
60
+ case '>=': {
61
+ return i >= this.to.value;
62
+ },
63
+
64
+ case '<=': {
65
+ return i <= this.to.value;
66
+ }
67
+ }
68
+ };
69
+
70
+ for var i = this.start.value;
71
+ test.call(this, i);
72
+ i++ if updateOperator == '++' else i-- {
73
+ this.elements.push({
74
+ "type": "Literal",
75
+ "value": i
76
+ });
77
+ }
78
+ } else {
79
+ var testExpression, updateExpression;
80
+
81
+ var declarations = [{
82
+ "type": "VariableDeclarator",
83
+ "id": {
84
+ "type": "Identifier",
85
+ "name": "_results"
86
+ },
87
+ "init": {
88
+ "type": "ArrayExpression",
89
+ "elements": []
90
+ }
91
+ }];
92
+
93
+ if isNumeric {
94
+ testExpression = {
95
+ "type": "BinaryExpression",
96
+ "operator": testOperator,
97
+ "left": {
98
+ "type": "Identifier",
99
+ "name": "_i"
100
+ },
101
+ "right": this.to
102
+ };
103
+
104
+ updateExpression = {
105
+ "type": "UpdateExpression",
106
+ "operator": updateOperator,
107
+ "argument": {
108
+ "type": "Identifier",
109
+ "name": "_i"
110
+ },
111
+ "prefix": false
112
+ };
113
+ } else {
114
+ if this.start.hasCallExpression() {
115
+ var startId = {
116
+ "type": "Identifier",
117
+ "name": "_start"
118
+ };
119
+
120
+ declarations.push({
121
+ "type": "VariableDeclarator",
122
+ "id": startId,
123
+ "init": this.start
124
+ });
125
+
126
+ this.start = startId;
127
+ }
128
+
129
+ if this.to.hasCallExpression() {
130
+ var endId = {
131
+ "type": "Identifier",
132
+ "name": "_end"
133
+ };
134
+
135
+ declarations.push({
136
+ "type": "VariableDeclarator",
137
+ "id": endId,
138
+ "init": this.to
139
+ });
140
+
141
+ this.to = endId;
142
+ }
143
+
144
+ testExpression = {
145
+ "type": "ConditionalExpression",
146
+ "test": {
147
+ "type": "BinaryExpression",
148
+ "operator": "<=",
149
+ "left": this.start,
150
+ "right": this.to
151
+ },
152
+ "consequent": {
153
+ "type": "BinaryExpression",
154
+ "operator": "<" + ('=' if this.operator == '..' else ''),
155
+ "left": {
156
+ "type": "Identifier",
157
+ "name": "_i"
158
+ },
159
+ "right": this.to
160
+ },
161
+ "alternate": {
162
+ "type": "BinaryExpression",
163
+ "operator": ">" + ('=' if this.operator == '..' else ''),
164
+ "left": {
165
+ "type": "Identifier",
166
+ "name": "_i"
167
+ },
168
+ "right": this.to
169
+ }
170
+ };
171
+
172
+ updateExpression = {
173
+ "type": "ConditionalExpression",
174
+ "test": {
175
+ "type": "BinaryExpression",
176
+ "operator": "<=",
177
+ "left": this.start,
178
+ "right": this.to
179
+ },
180
+ "consequent": {
181
+ "type": "UpdateExpression",
182
+ "operator": "++",
183
+ "argument": {
184
+ "type": "Identifier",
185
+ "name": "_i"
186
+ },
187
+ "prefix": false
188
+ },
189
+ "alternate": {
190
+ "type": "UpdateExpression",
191
+ "operator": "--",
192
+ "argument": {
193
+ "type": "Identifier",
194
+ "name": "_i"
195
+ },
196
+ "prefix": false
197
+ }
198
+ };
199
+ }
200
+
201
+ this.type = 'CallExpression';
202
+
203
+ this.callee = {
204
+ "type": "MemberExpression",
205
+ "computed": false,
206
+ "object": {
207
+ "type": "FunctionExpression",
208
+ "id": null,
209
+ "params": [],
210
+ "defaults": [],
211
+ "body": {
212
+ "type": "BlockStatement",
213
+ "body": [
214
+ {
215
+ "type": "VariableDeclaration",
216
+ "declarations": declarations,
217
+ "kind": "let"
218
+ },
219
+ {
220
+ "type": "ForStatement",
221
+ "init": {
222
+ "type": "VariableDeclaration",
223
+ "declarations": [{
224
+ "type": "VariableDeclarator",
225
+ "id": {
226
+ "type": "Identifier",
227
+ "name": "_i"
228
+ },
229
+ "init": this.start
230
+ }],
231
+ "kind": "let"
232
+ },
233
+ "test": testExpression,
234
+ "update": updateExpression,
235
+ "body": {
236
+ "type": "BlockStatement",
237
+ "body": [{
238
+ "type": "ExpressionStatement",
239
+ "expression": {
240
+ "type": "CallExpression",
241
+ "callee": {
242
+ "type": "MemberExpression",
243
+ "computed": false,
244
+ "object": {
245
+ "type": "Identifier",
246
+ "name": "_results"
247
+ },
248
+ "property": {
249
+ "type": "Identifier",
250
+ "name": "push"
251
+ }
252
+ },
253
+ "arguments": [{
254
+ "type": "Identifier",
255
+ "name": "_i"
256
+ }]
257
+ }
258
+ }]
259
+ }
260
+ },
261
+ {
262
+ "type": "ReturnStatement",
263
+ "argument": {
264
+ "type": "Identifier",
265
+ "name": "_results"
266
+ }
267
+ }
268
+ ]
269
+ },
270
+ "rest": null,
271
+ "generator": false,
272
+ "expression": false
273
+ },
274
+ "property": {
275
+ "type": "Identifier",
276
+ "name": "apply"
277
+ }
278
+ };
279
+
280
+ ::Object.defineProperty(this, 'arguments', {
281
+ value: [{ "type": "ThisExpression" }],
282
+ enumerable: true
283
+ });
284
+ }
285
+
286
+ return this;
287
+ };
288
+
289
+ Range.prototype.hasCallExpression = () -> true;
290
+
291
+ exports.Range = Range;
@@ -0,0 +1,33 @@
1
+ use :node;
2
+
3
+ var Node = module.require('./Node').Node;
4
+
5
+ fn VariableDeclarator(id, init)
6
+ extends Node {
7
+
8
+ this.type = 'VariableDeclarator';
9
+
10
+ this.id = id;
11
+ this.id.parent = this;
12
+
13
+ this.init = init;
14
+
15
+ if this.init? {
16
+ this.init.parent = this;
17
+ }
18
+ }
19
+
20
+ VariableDeclarator.prototype.codegen = () -> {
21
+ if !super.codegen() {
22
+ return;
23
+ }
24
+
25
+ if this.init? {
26
+ this.init = this.init.codegen();
27
+ }
28
+
29
+ this.id = this.id.codegen();
30
+ return this;
31
+ };
32
+
33
+ exports.VariableDeclarator = VariableDeclarator;
@@ -0,0 +1,32 @@
1
+ use :node;
2
+
3
+ var Node = module.require('../Node').Node;
4
+
5
+ fn ArrayExpression(elements)
6
+ extends Node {
7
+
8
+ this.type = 'ArrayExpression';
9
+ this.elements = elements;
10
+
11
+ for element in this.elements {
12
+ if element? {
13
+ element.parent = this;
14
+ }
15
+ }
16
+ }
17
+
18
+ ArrayExpression.prototype.codegen = () -> {
19
+ if !super.codegen() {
20
+ return;
21
+ }
22
+
23
+ for element, i in this.elements {
24
+ this.elements[i] = element?.codegen();
25
+ }
26
+
27
+ return this;
28
+ };
29
+
30
+ ArrayExpression.prototype.hasCallExpression = () -> true;
31
+
32
+ exports.ArrayExpression = ArrayExpression;
@@ -0,0 +1,37 @@
1
+ use :node;
2
+
3
+ var Node = module.require('../Node').Node;
4
+
5
+ fn ArrayPattern(elements)
6
+ extends Node {
7
+
8
+ this.type = 'ArrayPattern';
9
+ this.elements = elements;
10
+
11
+ for element in this.elements {
12
+ if element? {
13
+ element.parent = this;
14
+ }
15
+ }
16
+ }
17
+
18
+ ArrayPattern.prototype.codegen = () -> {
19
+ if !super.codegen() {
20
+ return;
21
+ }
22
+
23
+ var context = this.getContext().node;
24
+ for element, i in this.elements {
25
+ this.elements[i] = element?.codegen(false);
26
+
27
+ if element? and element.type == "Identifier" {
28
+ context.defineIdentifier(element);
29
+ }
30
+ }
31
+
32
+ return this;
33
+ };
34
+
35
+ ArrayPattern.prototype.hasCallExpression = () -> true;
36
+
37
+ exports.ArrayPattern = ArrayPattern;
@@ -0,0 +1,34 @@
1
+ use :node;
2
+
3
+ var Node = module.require('../Node').Node;
4
+
5
+ fn AssignmentExpression(left, operator, right)
6
+ extends Node {
7
+
8
+ this.type = 'AssignmentExpression';
9
+ this.operator = operator;
10
+
11
+ this.left = left;
12
+ this.left.parent = this;
13
+
14
+ this.right = right;
15
+ this.right.parent = this;
16
+ }
17
+
18
+ AssignmentExpression.prototype.codegen = () -> {
19
+ if !super.codegen() {
20
+ return;
21
+ }
22
+
23
+ this.left = this.left.codegen();
24
+ this.right = this.right.codegen();
25
+
26
+ return this;
27
+ };
28
+
29
+ AssignmentExpression.prototype.hasCallExpression = () -> {
30
+ return this.left?.hasCallExpression() ||
31
+ this.right?.hasCallExpression();
32
+ };
33
+
34
+ exports.AssignmentExpression = AssignmentExpression;
@@ -0,0 +1,125 @@
1
+ use :node;
2
+
3
+ var Node = module.require('../Node').Node;
4
+
5
+ fn BinaryExpression(left, operator, right)
6
+ extends Node {
7
+
8
+ this.type = 'BinaryExpression';
9
+ this.operator = operator;
10
+
11
+ this.left = left;
12
+ this.left.parent = this;
13
+
14
+ this.right = right;
15
+ this.right.parent = this;
16
+ }
17
+
18
+ BinaryExpression.prototype.codegen = () -> {
19
+ if !super.codegen() {
20
+ return;
21
+ }
22
+
23
+ var isRelational = (operator)
24
+ -> operator in ['>', '>=', '<', '>='];
25
+
26
+ // If we are dealing with something like a < x < b,
27
+ // turn it into: a < x && x < b
28
+ if isRelational(this.operator) &&
29
+ isRelational(this.left.operator) {
30
+ this.type = 'LogicalExpression';
31
+ this.right = {
32
+ "type": "BinaryExpression",
33
+ "operator": this.operator,
34
+ "left": this.left.right,
35
+ "right": this.right.codegen()
36
+ };
37
+
38
+ this.left = this.left.codegen();
39
+ this.operator = '&&';
40
+ } else {
41
+ this.left = this.left.codegen();
42
+ this.right = this.right.codegen();
43
+
44
+ switch (this.operator) {
45
+ case '==': {
46
+ this.operator = '===';
47
+ },
48
+
49
+ case '!=': {
50
+ this.operator = '!==';
51
+ },
52
+
53
+ case '**': {
54
+ this.type = 'CallExpression';
55
+ this.callee = {
56
+ "type": "MemberExpression",
57
+ "computed": false,
58
+ "object": {
59
+ "type": "Identifier",
60
+ "name": "Math"
61
+ },
62
+ "property": {
63
+ "type": "Identifier",
64
+ "name": "pow"
65
+ }
66
+ };
67
+
68
+ ::Object.defineProperty(this, 'arguments', {
69
+ value: [this.left, this.right],
70
+ enumerable: true
71
+ });
72
+ },
73
+
74
+ case '#': {
75
+ this.type = 'CallExpression';
76
+ this.callee = {
77
+ "type": "MemberExpression",
78
+ "computed": false,
79
+ "object": {
80
+ "type": "Identifier",
81
+ "name": "Math"
82
+ },
83
+ "property": {
84
+ "type": "Identifier",
85
+ "name": "floor"
86
+ }
87
+ };
88
+
89
+ ::Object.defineProperty(this, 'arguments', {
90
+ value: [{
91
+ "type": "BinaryExpression",
92
+ "operator": "/",
93
+ "left": this.left,
94
+ "right": this.right
95
+ }],
96
+ enumerable: true
97
+ });
98
+ },
99
+
100
+ case '%%': {
101
+ this.operator = '%';
102
+ this.left = {
103
+ "type": "BinaryExpression",
104
+ "operator": "+",
105
+ "left": {
106
+ "type": "BinaryExpression",
107
+ "operator": "%",
108
+ "left": this.left,
109
+ "right": this.right
110
+ },
111
+ "right": this.right
112
+ };
113
+ }
114
+ }
115
+ }
116
+
117
+ return this;
118
+ };
119
+
120
+ BinaryExpression.prototype.hasCallExpression = () -> {
121
+ return this.left?.hasCallExpression() ||
122
+ this.right?.hasCallExpression();
123
+ };
124
+
125
+ exports.BinaryExpression = BinaryExpression;
@@ -0,0 +1,149 @@
1
+ use :node;
2
+
3
+ var Node = module.require('../Node').Node;
4
+
5
+ fn CallExpression(callee, args)
6
+ extends Node {
7
+
8
+ this.type = 'CallExpression';
9
+ this.callee = callee;
10
+ this.callee.parent = this;
11
+
12
+ ::Object.defineProperty(this, 'arguments', {
13
+ value: args,
14
+ enumerable: true
15
+ });
16
+
17
+ for arg in args {
18
+ arg.parent = this;
19
+ }
20
+ }
21
+
22
+ CallExpression.prototype.codegen = () -> {
23
+ if !super.codegen() {
24
+ return;
25
+ }
26
+
27
+ var calleeType = this.callee.type;
28
+
29
+ if !this.callee.codeGenerated {
30
+ this.callee = this.callee.codegen();
31
+ }
32
+
33
+ var args = this.arguments;
34
+ var splatPositions = [];
35
+
36
+ for arg, i in args {
37
+ if args[i].type == "SplatExpression" || args[i].__splat {
38
+ splatPositions.push(i);
39
+ }
40
+
41
+ if !args[i].codeGenerated {
42
+ args[i] = arg.codegen();
43
+ }
44
+ }
45
+
46
+ if splatPositions.length > 0 {
47
+ var argsClone = args.slice(0);
48
+ args.length = 0;
49
+ args.push({
50
+ "type": "Literal",
51
+ "value": null
52
+ });
53
+
54
+ if argsClone.length == 1 {
55
+ args.push(argsClone[0].arguments[0]);
56
+ } else {
57
+ args.push({
58
+ "type": "CallExpression",
59
+ "callee": {
60
+ "type": "MemberExpression",
61
+ "computed": false,
62
+ "object": argsClone[0] if splatPositions[0] == 0 else {
63
+ "type": "ArrayExpression",
64
+ "elements": argsClone[...splatPositions[0]]
65
+ },
66
+ "property": {
67
+ "type": "Identifier",
68
+ "name": "concat"
69
+ }
70
+ },
71
+ "arguments": argsClone[1 if splatPositions[0] == 0 else splatPositions[0]..]
72
+ .map((arg, i) -> {
73
+ if splatPositions.indexOf(i + (1 if splatPositions[0] == 0 else splatPositions[0])) != -1 {
74
+ return arg;
75
+ }
76
+
77
+ return {
78
+ "type": "ArrayExpression",
79
+ "elements": [arg]
80
+ };
81
+ })
82
+ });
83
+ }
84
+ }
85
+
86
+ // If we are null propagating (?.), then turn this
87
+ // into a condition and add the null propagating condition.
88
+ if this.callee.type == 'ConditionalExpression' &&
89
+ (calleeType == 'NullPropagatingExpression' || calleeType == 'MemberExpression') {
90
+ var parent = this.parent;
91
+
92
+ var consequent = {
93
+ type: 'CallExpression',
94
+ callee: this.callee.consequent,
95
+ arguments: this.arguments
96
+ };
97
+
98
+ if splatPositions.length > 0 {
99
+ this.callee.consequent = {
100
+ "type": "MemberExpression",
101
+ "computed": false,
102
+ "object": this.callee.consequent,
103
+ "property": {
104
+ "type": "Identifier",
105
+ "name": "apply"
106
+ }
107
+ };
108
+ }
109
+
110
+ // If we're inside a statement, then turn this into
111
+ // a normal if statement.
112
+ if parent.type == 'ExpressionStatement' {
113
+ parent.type = 'IfStatement';
114
+ parent.test = this.callee.test;
115
+ parent.consequent = {
116
+ type: 'BlockStatement',
117
+ body: [
118
+ {
119
+ type: 'ExpressionStatement',
120
+ expression: consequent
121
+ }
122
+ ]
123
+ };
124
+ parent.alternate = null;
125
+ } else {
126
+ // Otherwise, it should be a conditional expression (?:).
127
+ this.type = 'ConditionalExpression';
128
+ this.test = this.callee.test;
129
+ this.consequent = consequent;
130
+ this.alternate = this.callee.alternate;
131
+ }
132
+ } else if splatPositions.length > 0 {
133
+ this.callee = {
134
+ "type": "MemberExpression",
135
+ "computed": false,
136
+ "object": this.callee,
137
+ "property": {
138
+ "type": "Identifier",
139
+ "name": "apply"
140
+ }
141
+ };
142
+ }
143
+
144
+ return this;
145
+ };
146
+
147
+ CallExpression.prototype.hasCallExpression = () -> true;
148
+
149
+ exports.CallExpression = CallExpression;