minjs 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,630 @@
1
+ module Minjs
2
+ module ECMA262
3
+ class Exp < Base
4
+ def replace(from, to)
5
+ puts "warning: #{self.class}: not implement"
6
+ end
7
+
8
+ def traverse
9
+ yield(self)
10
+ p "??#{self.class}"
11
+ end
12
+
13
+ def to_js(options = {})
14
+ "??#{@val.to_js(options)}(#{_args})"
15
+ end
16
+
17
+ def reduce(parent)
18
+ end
19
+ end
20
+
21
+ class ExpArg1 < Exp
22
+ def initialize(val)
23
+ @val = val
24
+ end
25
+
26
+ def replace(from, to)
27
+ if @val == from
28
+ @val = to
29
+ end
30
+ end
31
+
32
+ def traverse(parent, &block)
33
+ @val.traverse(self, &block)
34
+ yield self, parent
35
+ end
36
+
37
+ def to_js(options = {})
38
+ concat options, sym, @val
39
+ end
40
+ end
41
+
42
+ class ExpArg2 < Exp
43
+ attr_reader :val, :val2
44
+
45
+ def initialize(val, val2)
46
+ @val = val
47
+ @val2 = val2
48
+ end
49
+
50
+ def replace(from, to)
51
+ if @val == from
52
+ @val = to
53
+ elsif @val2 == from
54
+ @val2 = to
55
+ end
56
+ end
57
+
58
+ def traverse(parent, &block)
59
+ @val.traverse(self, &block)
60
+ @val2.traverse(self, &block)
61
+ yield self, parent
62
+ end
63
+
64
+ def to_js(options = {})
65
+ concat options, @val, sym, @val2
66
+ end
67
+ end
68
+
69
+ # ""
70
+ class ExpEmpty < Exp
71
+ def traverse(parent, &block)
72
+ end
73
+ def to_js(options = {})
74
+ ""
75
+ end
76
+ end
77
+
78
+ # ( exp )
79
+ class ExpParen < Exp
80
+ def initialize(val)
81
+ @val = val
82
+ end
83
+
84
+ def replace(from, to)
85
+ if @val == from
86
+ @val = to
87
+ end
88
+ end
89
+
90
+ def traverse(parent, &block)
91
+ @val.traverse(self, &block)
92
+ yield self, parent
93
+ end
94
+
95
+ def to_js(options = {})
96
+ "(#{@val.to_js(options)})"
97
+ end
98
+ end
99
+
100
+ # val = val2
101
+ class ExpAssign < ExpArg2
102
+ def sym
103
+ "="
104
+ end
105
+ end
106
+ class ExpDivAssign < ExpAssign
107
+ def sym
108
+ "/="
109
+ end
110
+ end
111
+ class ExpMulAssign < ExpAssign
112
+ def sym
113
+ "*="
114
+ end
115
+ end
116
+ class ExpModAssign < ExpAssign
117
+ def sym
118
+ "/="
119
+ end
120
+ end
121
+ class ExpAddAssign < ExpAssign
122
+ def sym
123
+ "+="
124
+ end
125
+ end
126
+ class ExpSubAssign < ExpAssign
127
+ def sym
128
+ "-="
129
+ end
130
+ end
131
+ class ExpLShiftAssign < ExpAssign
132
+ def sym
133
+ "<<="
134
+ end
135
+ end
136
+ class ExpRShiftAssign < ExpAssign
137
+ def sym
138
+ ">>="
139
+ end
140
+ end
141
+ class ExpURShiftAssign < ExpAssign
142
+ def sym
143
+ ">>>="
144
+ end
145
+ end
146
+ class ExpAndAssign < ExpAssign
147
+ def sym
148
+ "&="
149
+ end
150
+ end
151
+ class ExpOrAssign < ExpAssign
152
+ def sym
153
+ "|="
154
+ end
155
+ end
156
+ class ExpXorAssign < ExpAssign
157
+ def sym
158
+ "^="
159
+ end
160
+ end
161
+
162
+ # a ? b : c
163
+ class ExpCond < Exp
164
+ def initialize(val, val2, val3)
165
+ @val = val
166
+ @val2 = val2
167
+ @val3 = val3
168
+ end
169
+
170
+ def replace(from, to)
171
+ if from == @val
172
+ @val = to
173
+ elsif from == @val2
174
+ @val2 = to
175
+ elsif from == @val3
176
+ @val3 = to
177
+ end
178
+ end
179
+
180
+ def traverse(parent, &block)
181
+ @val.traverse(self, &block)
182
+ @val2.traverse(self, &block)
183
+ @val3.traverse(self, &block)
184
+ yield self, parent
185
+ end
186
+
187
+ def to_js(options = {})
188
+ "#{@val.to_js(options)}?#{@val2.to_js(options)}:#{@val3.to_js(options)}"
189
+ end
190
+ end
191
+
192
+ # ||
193
+ class ExpLogicalOr < ExpArg2
194
+ def sym
195
+ "||"
196
+ end
197
+ end
198
+
199
+ # &&
200
+ class ExpLogicalAnd < ExpArg2
201
+ def sym
202
+ "&&"
203
+ end
204
+ end
205
+
206
+ # |
207
+ class ExpOr < ExpArg2
208
+ def sym
209
+ "|"
210
+ end
211
+ end
212
+
213
+ # ^
214
+ class ExpXor < ExpArg2
215
+ def sym
216
+ "^"
217
+ end
218
+ end
219
+
220
+ # &
221
+ class ExpAnd < ExpArg2
222
+ def sym
223
+ "&"
224
+ end
225
+ end
226
+
227
+ # 11.9
228
+ # ==
229
+ class ExpEq < ExpArg2
230
+ def sym
231
+ "=="
232
+ end
233
+ end
234
+ # !=
235
+ class ExpNotEq < ExpArg2
236
+ def sym
237
+ "!="
238
+ end
239
+ end
240
+ # ===
241
+ class ExpStrictEq < ExpArg2
242
+ def sym
243
+ "==="
244
+ end
245
+ end
246
+ # !==
247
+ class ExpStrictNotEq < ExpArg2
248
+ def sym
249
+ "!=="
250
+ end
251
+ end
252
+
253
+ class ExpLt < ExpArg2
254
+ def sym
255
+ "<"
256
+ end
257
+ end
258
+
259
+ class ExpGt < ExpArg2
260
+ def sym
261
+ ">"
262
+ end
263
+ end
264
+
265
+ class ExpLtEq < ExpArg2
266
+ def sym
267
+ "<="
268
+ end
269
+ end
270
+
271
+ class ExpGtEq < ExpArg2
272
+ def sym
273
+ ">="
274
+ end
275
+ end
276
+
277
+ #+
278
+ class ExpAdd < ExpArg2
279
+ def sym
280
+ "+"
281
+ end
282
+
283
+ def reduce(parent)
284
+ # a + 0 => a
285
+ if @val.kind_of? ECMA262Numeric and @val2.kind_of? ECMA262Numeric and @val.to_num == 0
286
+ parent.replace(self, @val2)
287
+ end
288
+ # 0 + b => b
289
+ if @val.kind_of? ECMA262Numeric and @val2.kind_of? ECMA262Numeric and @val2.to_num == 0
290
+ parent.replace(self, @val)
291
+ end
292
+ # N + M => (N + M)
293
+ if @val.kind_of? ECMA262Numeric and @val2.kind_of? ECMA262Numeric and @val.integer? and @val2.integer?
294
+ parent.replace(self, ECMA262Numeric.new(nil, @val.to_num + @val2.to_num))
295
+ end
296
+ if @val2.kind_of? ECMA262Numeric and @val2.integer?
297
+ # ((a + N) + M) or ((N + a) + M)
298
+ if @val.kind_of? ExpAdd
299
+ if @val.val2.kind_of? ECMA262Numeric and @val.val2.integer?
300
+ @val2 = ECMA262Numeric.new(nil, @val.val2.to_num + @val2.to_num)
301
+ @val = @val.val
302
+ elsif @val.val.kind_of? ECMA262Numeric and @val.val.integer?
303
+ @val2 = ECMA262Numeric.new(nil, @val.val.to_num + @val2.to_num)
304
+ @val = @val.val2
305
+ end
306
+ # ((a - N) + M) or ((N - a) + M)
307
+ elsif @val.kind_of? ExpSub
308
+ if @val.val2.kind_of? ECMA262Numeric and @val.val2.integer?
309
+ @val2 = ECMA262Numeric.new(nil, -(@val.val2.to_num - @val2.to_num))
310
+ @val = @val.val
311
+ elsif @val.val.kind_of? ECMA262Numeric and @val.val.integer?
312
+ @val2 = ECMA262Numeric.new(nil, -(@val.val.to_num - @val2.to_num))
313
+ @val = @val.val2
314
+ end
315
+ end
316
+ end
317
+ end
318
+
319
+ end
320
+
321
+ class ExpSub < ExpArg2
322
+ def sym
323
+ "-"
324
+ end
325
+
326
+ def reduce(parent)
327
+ # a - 0 => a
328
+ if @val.kind_of? ECMA262Numeric and @val2.kind_of? ECMA262Numeric and @val.to_num == 0
329
+ parent.replace(self, @val2)
330
+ end
331
+ # 0 - b => b
332
+ if @val2.kind_of? ECMA262Numeric and @val.kind_of? ECMA262Numeric and @val2.to_num == 0
333
+ parent.replace(self, @val)
334
+ end
335
+ # N - M => (N - M)
336
+ if @val.kind_of? ECMA262Numeric and @val2.kind_of? ECMA262Numeric and @val.integer? and @val2.integer?
337
+ parent.replace(self, ECMA262Numeric.new(nil, @val.to_num - @val2.to_num))
338
+ end
339
+ if @val2.kind_of? ECMA262Numeric and @val2.integer?
340
+ # ((a - N) - M) or ((N - a) - M)
341
+ if @val.kind_of? ExpSub
342
+ if @val.val2.kind_of? ECMA262Numeric and @val.val2.integer?
343
+ @val2 = ECMA262Numeric.new(nil, @val.val2.to_num + @val2.to_num)
344
+ @val = @val.val
345
+ elsif @val.val.kind_of? ECMA262Numeric and @val.val.integer?
346
+ @val2 = ECMA262Numeric.new(nil, @val.val.to_num + @val2.to_num)
347
+ @val = @val.val2
348
+ end
349
+ # ((a + N) - M) or ((N + a) - M)
350
+ elsif @val.kind_of? ExpAdd
351
+ if @val.val2.kind_of? ECMA262Numeric and @val.val2.integer?
352
+ @val2 = ECMA262Numeric.new(nil, -(@val.val2.to_num - @val2.to_num))
353
+ @val = @val.val
354
+ elsif @val.val.kind_of? ECMA262Numeric and @val.val.integer?
355
+ @val2 = ECMA262Numeric.new(nil, -(@val.val.to_num - @val2.to_num))
356
+ @val = @val.val2
357
+ end
358
+ end
359
+ end
360
+ end
361
+
362
+ end
363
+
364
+ class ExpInstanceOf < ExpArg2
365
+ def sym
366
+ "instanceof"
367
+ end
368
+ end
369
+
370
+ class ExpIn < ExpArg2
371
+ def sym
372
+ "in"
373
+ end
374
+ end
375
+
376
+ class ExpLShift < ExpArg2
377
+ def sym
378
+ "<<"
379
+ end
380
+ end
381
+ class ExpRShift < ExpArg2
382
+ def sym
383
+ ">>"
384
+ end
385
+ end
386
+ class ExpURShift < ExpArg2
387
+ def sym
388
+ ">>>"
389
+ end
390
+ end
391
+
392
+ class ExpMul < ExpArg2
393
+ def sym
394
+ "*"
395
+ end
396
+
397
+ def reduce(parent)
398
+ # a * 1 => a
399
+ if @val.kind_of? ECMA262Numeric and @val2.kind_of? ECMA262Numeric and @val.to_num == 1
400
+ parent.replace(self, @val2)
401
+ end
402
+ # 1 * b => b
403
+ if @val.kind_of? ECMA262Numeric and @val2.kind_of? ECMA262Numeric and @val2.to_num == 1
404
+ parent.replace(self, @val)
405
+ end
406
+ # N * M => (N * M)
407
+ if @val.kind_of? ECMA262Numeric and @val2.kind_of? ECMA262Numeric and @val.integer? and @val2.integer?
408
+ parent.replace(self, ECMA262Numeric.new(nil, @val.to_num * @val2.to_num))
409
+ end
410
+ # ((a * N) * M) or ((N * a) * M)
411
+ if @val2.kind_of? ECMA262Numeric and @val2.integer? and @val.kind_of? ExpMul
412
+ if @val.val2.kind_of? ECMA262Numeric and @val.val2.integer?
413
+ @val2 = ECMA262Numeric.new(nil, @val.val2.to_num * @val2.to_num)
414
+ @val = @val.val
415
+ elsif @val.val.kind_of? ECMA262Numeric and @val.val.integer?
416
+ @val2 = ECMA262Numeric.new(nil, @val.val.to_num * @val2.to_num)
417
+ @val = @val.val2
418
+ end
419
+ end
420
+ end
421
+ end
422
+ class ExpDiv < ExpArg2
423
+ def sym
424
+ "/"
425
+ end
426
+ end
427
+ class ExpMod < ExpArg2
428
+ def sym
429
+ "%"
430
+ end
431
+ end
432
+ #
433
+ # 11.4
434
+ # unary expression
435
+ #
436
+ class ExpDelete < ExpArg1
437
+ def sym
438
+ "delete"
439
+ end
440
+ end
441
+ class ExpVoid < ExpArg1
442
+ def sym
443
+ "void"
444
+ end
445
+ end
446
+ class ExpTypeof < ExpArg1
447
+ def sym
448
+ "typeof"
449
+ end
450
+ end
451
+ class ExpPostInc < ExpArg1
452
+ def sym
453
+ "++"
454
+ end
455
+ def to_js(options = {})
456
+ concat options, @val, sym
457
+ end
458
+ end
459
+ class ExpPostDec < ExpArg1
460
+ def sym
461
+ "--"
462
+ end
463
+ def to_js(options = {})
464
+ concat options, @val, sym
465
+ end
466
+ end
467
+ class ExpPreInc < ExpArg1
468
+ def sym
469
+ "++"
470
+ end
471
+ end
472
+ class ExpPreDec < ExpArg1
473
+ def sym
474
+ "--"
475
+ end
476
+ end
477
+ class ExpPositive < ExpArg1
478
+ def sym
479
+ "+"
480
+ end
481
+
482
+ def reduce(parent)
483
+ if @val.kind_of? ECMA262Numeric
484
+ parent.replace(self, @val)
485
+ end
486
+ end
487
+ end
488
+
489
+ class ExpNegative < ExpArg1
490
+ def sym
491
+ "-"
492
+ end
493
+
494
+ def reduce(parent)
495
+ if @val.kind_of? ECMA262Numeric
496
+ if @val.integer.match(/^\-/)
497
+ integer = $'
498
+ else
499
+ integer = "-#{@val.integer}"
500
+ end
501
+ val = ECMA262Numeric.new(integer, integer, @val.decimal, @val.exp)
502
+ parent.replace(self, val)
503
+ end
504
+ end
505
+ end
506
+ class ExpBitwiseNot < ExpArg1
507
+ def sym
508
+ "~"
509
+ end
510
+ end
511
+ class ExpLogicalNot < ExpArg1
512
+ def sym
513
+ "!"
514
+ end
515
+ end
516
+
517
+ class ExpNew < Exp
518
+ def initialize(name, args)
519
+ @name = name
520
+ @args = args
521
+ end
522
+
523
+ def replace(from, to)
524
+ if @name == from
525
+ @name = from
526
+ elsif @args and (idx = @args.index(from))
527
+ @args[idx] = to
528
+ end
529
+ end
530
+
531
+ def traverse(parent, &block)
532
+ @name.traverse(self, &block)
533
+ if @args
534
+ @args.each do |arg|
535
+ arg.traverse(self, &block)
536
+ end
537
+ end
538
+ yield self, parent
539
+ end
540
+
541
+ def to_js(options = {})
542
+ if @args
543
+ args = @args.collect{|x| x.to_js(options)}.join(",")
544
+ concat options, :new, @name, '(', args, ')'
545
+ else
546
+ concat options, :new, @name
547
+ end
548
+ end
549
+ end
550
+ #11.2
551
+ # => name(args)
552
+ #
553
+ class ExpCall < Exp
554
+ attr_reader :name
555
+ attr_reader :args
556
+
557
+ def initialize(name, args)
558
+ @name = name
559
+ @args = args
560
+ end
561
+
562
+ def replace(from, to)
563
+ @args.each_index do |i|
564
+ arg = @args[i]
565
+ if arg == from
566
+ @args[i] = to
567
+ break
568
+ end
569
+ end
570
+ end
571
+
572
+ def traverse(parent, &block)
573
+ @name.traverse(self, &block)
574
+ @args.each do |x|
575
+ x.traverse(self, &block)
576
+ end
577
+ yield self, parent
578
+ end
579
+
580
+ def to_js(options = {})
581
+ args = @args.collect{|x| x.to_js(options)}.join(",")
582
+ "#{@name.to_js(options)}(#{args})"
583
+ end
584
+ end
585
+ #
586
+ # => val.val2
587
+ #
588
+ class ExpProp < Exp
589
+ def initialize(val, val2)
590
+ @val = val
591
+ if val2.kind_of? IdentifierName
592
+ @val2 = ECMA262::ECMA262String.new(val2.val)
593
+ else
594
+ raise "val2 must be kind_of ItentiferName"
595
+ end
596
+ end
597
+ def traverse(parent, &block)
598
+ @val.traverse(self, &block)
599
+ @val2.traverse(self, &block)
600
+ yield self, parent
601
+ end
602
+ def to_js(options = {})
603
+ "#{@val.to_js(options)}.#{@val2.val}"
604
+ end
605
+ end
606
+ #
607
+ # => val[val2]
608
+ #
609
+ class ExpPropBrac < Exp
610
+ def initialize(val, val2)
611
+ @val = val
612
+ @val2 = val2
613
+ end
614
+ def traverse(parent, &block)
615
+ @val.traverse(self, &block)
616
+ @val2.traverse(self, &block)
617
+ yield self, parent
618
+ end
619
+ def to_js(options = {})
620
+ "#{@val.to_js(options)}[#{@val2.to_js(options)}]"
621
+ end
622
+ end
623
+
624
+ class ExpComma < ExpArg2
625
+ def sym
626
+ ","
627
+ end
628
+ end
629
+ end
630
+ end