minjs 0.1.2 → 0.1.3

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.
@@ -1,21 +1,20 @@
1
1
  module Minjs
2
2
  module ECMA262
3
3
  class Exp < Base
4
- def replace(from, to)
5
- puts "warning: #{self.class}: not implement"
6
- end
7
-
8
4
  def traverse
9
5
  yield(self)
10
- p "??#{self.class}"
11
6
  end
12
7
 
13
8
  def to_js(options = {})
14
- "??#{@val.to_js(options)}(#{_args})"
9
+ raise "internal error"
15
10
  end
16
11
 
17
12
  def reduce(parent)
18
13
  end
14
+
15
+ def priority(exp)
16
+ 9999
17
+ end
19
18
  end
20
19
 
21
20
  class ExpArg1 < Exp
@@ -77,10 +76,16 @@ module Minjs
77
76
 
78
77
  # ( exp )
79
78
  class ExpParen < Exp
79
+ attr_reader :val
80
+
80
81
  def initialize(val)
81
82
  @val = val
82
83
  end
83
84
 
85
+ def priority(exp)
86
+ 0
87
+ end
88
+
84
89
  def replace(from, to)
85
90
  if @val == from
86
91
  @val = to
@@ -102,61 +107,97 @@ module Minjs
102
107
  def sym
103
108
  "="
104
109
  end
110
+ def priority(exp)
111
+ 130
112
+ end
105
113
  end
106
114
  class ExpDivAssign < ExpAssign
107
115
  def sym
108
116
  "/="
109
117
  end
118
+ def priority(exp)
119
+ 130
120
+ end
110
121
  end
111
122
  class ExpMulAssign < ExpAssign
112
123
  def sym
113
124
  "*="
114
125
  end
126
+ def priority(exp)
127
+ 130
128
+ end
115
129
  end
116
130
  class ExpModAssign < ExpAssign
117
131
  def sym
118
- "/="
132
+ "%="
133
+ end
134
+ def priority(exp)
135
+ 130
119
136
  end
120
137
  end
121
138
  class ExpAddAssign < ExpAssign
122
139
  def sym
123
140
  "+="
124
141
  end
142
+ def priority(exp)
143
+ 130
144
+ end
125
145
  end
126
146
  class ExpSubAssign < ExpAssign
127
147
  def sym
128
148
  "-="
129
149
  end
150
+ def priority(exp)
151
+ 130
152
+ end
130
153
  end
131
154
  class ExpLShiftAssign < ExpAssign
132
155
  def sym
133
156
  "<<="
134
157
  end
158
+ def priority(exp)
159
+ 130
160
+ end
135
161
  end
136
162
  class ExpRShiftAssign < ExpAssign
137
163
  def sym
138
164
  ">>="
139
165
  end
166
+ def priority(exp)
167
+ 130
168
+ end
140
169
  end
141
170
  class ExpURShiftAssign < ExpAssign
142
171
  def sym
143
172
  ">>>="
144
173
  end
174
+ def priority(exp)
175
+ 130
176
+ end
145
177
  end
146
178
  class ExpAndAssign < ExpAssign
147
179
  def sym
148
180
  "&="
149
181
  end
182
+ def priority(exp)
183
+ 130
184
+ end
150
185
  end
151
186
  class ExpOrAssign < ExpAssign
152
187
  def sym
153
188
  "|="
154
189
  end
190
+ def priority(exp)
191
+ 130
192
+ end
155
193
  end
156
194
  class ExpXorAssign < ExpAssign
157
195
  def sym
158
196
  "^="
159
197
  end
198
+ def priority(exp)
199
+ 130
200
+ end
160
201
  end
161
202
 
162
203
  # a ? b : c
@@ -167,6 +208,10 @@ module Minjs
167
208
  @val3 = val3
168
209
  end
169
210
 
211
+ def priority(exp)
212
+ 120
213
+ end
214
+
170
215
  def replace(from, to)
171
216
  if from == @val
172
217
  @val = to
@@ -194,6 +239,9 @@ module Minjs
194
239
  def sym
195
240
  "||"
196
241
  end
242
+ def priority(exp)
243
+ 118
244
+ end
197
245
  end
198
246
 
199
247
  # &&
@@ -201,6 +249,9 @@ module Minjs
201
249
  def sym
202
250
  "&&"
203
251
  end
252
+ def priority(exp)
253
+ 116
254
+ end
204
255
  end
205
256
 
206
257
  # |
@@ -208,6 +259,9 @@ module Minjs
208
259
  def sym
209
260
  "|"
210
261
  end
262
+ def priority(exp)
263
+ 108
264
+ end
211
265
  end
212
266
 
213
267
  # ^
@@ -215,6 +269,9 @@ module Minjs
215
269
  def sym
216
270
  "^"
217
271
  end
272
+ def priority(exp)
273
+ 106
274
+ end
218
275
  end
219
276
 
220
277
  # &
@@ -222,6 +279,9 @@ module Minjs
222
279
  def sym
223
280
  "&"
224
281
  end
282
+ def priority(exp)
283
+ 104
284
+ end
225
285
  end
226
286
 
227
287
  # 11.9
@@ -230,48 +290,72 @@ module Minjs
230
290
  def sym
231
291
  "=="
232
292
  end
293
+ def priority(exp)
294
+ 90
295
+ end
233
296
  end
234
297
  # !=
235
298
  class ExpNotEq < ExpArg2
236
299
  def sym
237
300
  "!="
238
301
  end
302
+ def priority(exp)
303
+ 90
304
+ end
239
305
  end
240
306
  # ===
241
307
  class ExpStrictEq < ExpArg2
242
308
  def sym
243
309
  "==="
244
310
  end
311
+ def priority(exp)
312
+ 90
313
+ end
245
314
  end
246
315
  # !==
247
316
  class ExpStrictNotEq < ExpArg2
248
317
  def sym
249
318
  "!=="
250
319
  end
320
+ def priority(exp)
321
+ 90
322
+ end
251
323
  end
252
324
 
253
325
  class ExpLt < ExpArg2
254
326
  def sym
255
327
  "<"
256
328
  end
329
+ def priority(exp)
330
+ 80
331
+ end
257
332
  end
258
333
 
259
334
  class ExpGt < ExpArg2
260
335
  def sym
261
336
  ">"
262
337
  end
338
+ def priority(exp)
339
+ 80
340
+ end
263
341
  end
264
342
 
265
343
  class ExpLtEq < ExpArg2
266
344
  def sym
267
345
  "<="
268
346
  end
347
+ def priority(exp)
348
+ 80
349
+ end
269
350
  end
270
351
 
271
352
  class ExpGtEq < ExpArg2
272
353
  def sym
273
354
  ">="
274
355
  end
356
+ def priority(exp)
357
+ 80
358
+ end
275
359
  end
276
360
 
277
361
  #+
@@ -279,6 +363,9 @@ module Minjs
279
363
  def sym
280
364
  "+"
281
365
  end
366
+ def priority(exp)
367
+ 60
368
+ end
282
369
 
283
370
  def reduce(parent)
284
371
  # a + 0 => a
@@ -291,25 +378,25 @@ module Minjs
291
378
  end
292
379
  # N + M => (N + M)
293
380
  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))
381
+ parent.replace(self, ECMA262Numeric.new(@val.to_num + @val2.to_num))
295
382
  end
296
383
  if @val2.kind_of? ECMA262Numeric and @val2.integer?
297
384
  # ((a + N) + M) or ((N + a) + M)
298
385
  if @val.kind_of? ExpAdd
299
386
  if @val.val2.kind_of? ECMA262Numeric and @val.val2.integer?
300
- @val2 = ECMA262Numeric.new(nil, @val.val2.to_num + @val2.to_num)
387
+ @val2 = ECMA262Numeric.new(@val.val2.to_num + @val2.to_num)
301
388
  @val = @val.val
302
389
  elsif @val.val.kind_of? ECMA262Numeric and @val.val.integer?
303
- @val2 = ECMA262Numeric.new(nil, @val.val.to_num + @val2.to_num)
390
+ @val2 = ECMA262Numeric.new(@val.val.to_num + @val2.to_num)
304
391
  @val = @val.val2
305
392
  end
306
393
  # ((a - N) + M) or ((N - a) + M)
307
394
  elsif @val.kind_of? ExpSub
308
395
  if @val.val2.kind_of? ECMA262Numeric and @val.val2.integer?
309
- @val2 = ECMA262Numeric.new(nil, -(@val.val2.to_num - @val2.to_num))
396
+ @val2 = ECMA262Numeric.new(-(@val.val2.to_num - @val2.to_num))
310
397
  @val = @val.val
311
398
  elsif @val.val.kind_of? ECMA262Numeric and @val.val.integer?
312
- @val2 = ECMA262Numeric.new(nil, -(@val.val.to_num - @val2.to_num))
399
+ @val2 = ECMA262Numeric.new(-(@val.val.to_num - @val2.to_num))
313
400
  @val = @val.val2
314
401
  end
315
402
  end
@@ -322,6 +409,9 @@ module Minjs
322
409
  def sym
323
410
  "-"
324
411
  end
412
+ def priority(exp)
413
+ 60
414
+ end
325
415
 
326
416
  def reduce(parent)
327
417
  # a - 0 => a
@@ -334,25 +424,25 @@ module Minjs
334
424
  end
335
425
  # N - M => (N - M)
336
426
  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))
427
+ parent.replace(self, ECMA262Numeric.new(@val.to_num - @val2.to_num))
338
428
  end
339
429
  if @val2.kind_of? ECMA262Numeric and @val2.integer?
340
430
  # ((a - N) - M) or ((N - a) - M)
341
431
  if @val.kind_of? ExpSub
342
432
  if @val.val2.kind_of? ECMA262Numeric and @val.val2.integer?
343
- @val2 = ECMA262Numeric.new(nil, @val.val2.to_num + @val2.to_num)
433
+ @val2 = ECMA262Numeric.new(@val.val2.to_num + @val2.to_num)
344
434
  @val = @val.val
345
435
  elsif @val.val.kind_of? ECMA262Numeric and @val.val.integer?
346
- @val2 = ECMA262Numeric.new(nil, @val.val.to_num + @val2.to_num)
436
+ @val2 = ECMA262Numeric.new(@val.val.to_num + @val2.to_num)
347
437
  @val = @val.val2
348
438
  end
349
439
  # ((a + N) - M) or ((N + a) - M)
350
440
  elsif @val.kind_of? ExpAdd
351
441
  if @val.val2.kind_of? ECMA262Numeric and @val.val2.integer?
352
- @val2 = ECMA262Numeric.new(nil, -(@val.val2.to_num - @val2.to_num))
442
+ @val2 = ECMA262Numeric.new(-(@val.val2.to_num - @val2.to_num))
353
443
  @val = @val.val
354
444
  elsif @val.val.kind_of? ECMA262Numeric and @val.val.integer?
355
- @val2 = ECMA262Numeric.new(nil, -(@val.val.to_num - @val2.to_num))
445
+ @val2 = ECMA262Numeric.new(-(@val.val.to_num - @val2.to_num))
356
446
  @val = @val.val2
357
447
  end
358
448
  end
@@ -365,34 +455,52 @@ module Minjs
365
455
  def sym
366
456
  "instanceof"
367
457
  end
458
+ def priority(exp)
459
+ 80
460
+ end
368
461
  end
369
462
 
370
463
  class ExpIn < ExpArg2
371
464
  def sym
372
465
  "in"
373
466
  end
467
+ def priority(exp)
468
+ 80
469
+ end
374
470
  end
375
471
 
376
472
  class ExpLShift < ExpArg2
377
473
  def sym
378
474
  "<<"
379
475
  end
476
+ def priority(exp)
477
+ 70
478
+ end
380
479
  end
381
480
  class ExpRShift < ExpArg2
382
481
  def sym
383
482
  ">>"
384
483
  end
484
+ def priority(exp)
485
+ 70
486
+ end
385
487
  end
386
488
  class ExpURShift < ExpArg2
387
489
  def sym
388
490
  ">>>"
389
491
  end
492
+ def priority(exp)
493
+ 70
494
+ end
390
495
  end
391
496
 
392
497
  class ExpMul < ExpArg2
393
498
  def sym
394
499
  "*"
395
500
  end
501
+ def priority(exp)
502
+ 50
503
+ end
396
504
 
397
505
  def reduce(parent)
398
506
  # a * 1 => a
@@ -405,15 +513,15 @@ module Minjs
405
513
  end
406
514
  # N * M => (N * M)
407
515
  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))
516
+ parent.replace(self, ECMA262Numeric.new(@val.to_num * @val2.to_num))
409
517
  end
410
518
  # ((a * N) * M) or ((N * a) * M)
411
519
  if @val2.kind_of? ECMA262Numeric and @val2.integer? and @val.kind_of? ExpMul
412
520
  if @val.val2.kind_of? ECMA262Numeric and @val.val2.integer?
413
- @val2 = ECMA262Numeric.new(nil, @val.val2.to_num * @val2.to_num)
521
+ @val2 = ECMA262Numeric.new(@val.val2.to_num * @val2.to_num)
414
522
  @val = @val.val
415
523
  elsif @val.val.kind_of? ECMA262Numeric and @val.val.integer?
416
- @val2 = ECMA262Numeric.new(nil, @val.val.to_num * @val2.to_num)
524
+ @val2 = ECMA262Numeric.new(@val.val.to_num * @val2.to_num)
417
525
  @val = @val.val2
418
526
  end
419
527
  end
@@ -423,11 +531,17 @@ module Minjs
423
531
  def sym
424
532
  "/"
425
533
  end
534
+ def priority(exp)
535
+ 50
536
+ end
426
537
  end
427
538
  class ExpMod < ExpArg2
428
539
  def sym
429
540
  "%"
430
541
  end
542
+ def priority(exp)
543
+ 50
544
+ end
431
545
  end
432
546
  #
433
547
  # 11.4
@@ -437,21 +551,33 @@ module Minjs
437
551
  def sym
438
552
  "delete"
439
553
  end
554
+ def priority(exp)
555
+ 40
556
+ end
440
557
  end
441
558
  class ExpVoid < ExpArg1
442
559
  def sym
443
560
  "void"
444
561
  end
562
+ def priority(exp)
563
+ 40
564
+ end
445
565
  end
446
566
  class ExpTypeof < ExpArg1
447
567
  def sym
448
568
  "typeof"
449
569
  end
570
+ def priority(exp)
571
+ 40
572
+ end
450
573
  end
451
574
  class ExpPostInc < ExpArg1
452
575
  def sym
453
576
  "++"
454
577
  end
578
+ def priority(exp)
579
+ 30
580
+ end
455
581
  def to_js(options = {})
456
582
  concat options, @val, sym
457
583
  end
@@ -460,6 +586,9 @@ module Minjs
460
586
  def sym
461
587
  "--"
462
588
  end
589
+ def priority(exp)
590
+ 30
591
+ end
463
592
  def to_js(options = {})
464
593
  concat options, @val, sym
465
594
  end
@@ -468,16 +597,25 @@ module Minjs
468
597
  def sym
469
598
  "++"
470
599
  end
600
+ def priority(exp)
601
+ 40
602
+ end
471
603
  end
472
604
  class ExpPreDec < ExpArg1
473
605
  def sym
474
606
  "--"
475
607
  end
608
+ def priority(exp)
609
+ 40
610
+ end
476
611
  end
477
612
  class ExpPositive < ExpArg1
478
613
  def sym
479
614
  "+"
480
615
  end
616
+ def priority(exp)
617
+ 40
618
+ end
481
619
 
482
620
  def reduce(parent)
483
621
  if @val.kind_of? ECMA262Numeric
@@ -490,6 +628,9 @@ module Minjs
490
628
  def sym
491
629
  "-"
492
630
  end
631
+ def priority(exp)
632
+ 40
633
+ end
493
634
 
494
635
  def reduce(parent)
495
636
  if @val.kind_of? ECMA262Numeric
@@ -498,7 +639,7 @@ module Minjs
498
639
  else
499
640
  integer = "-#{@val.integer}"
500
641
  end
501
- val = ECMA262Numeric.new(integer, integer, @val.decimal, @val.exp)
642
+ val = ECMA262Numeric.new(integer, @val.decimal, @val.exp)
502
643
  parent.replace(self, val)
503
644
  end
504
645
  end
@@ -507,11 +648,17 @@ module Minjs
507
648
  def sym
508
649
  "~"
509
650
  end
651
+ def priority(exp)
652
+ 40
653
+ end
510
654
  end
511
655
  class ExpLogicalNot < ExpArg1
512
656
  def sym
513
657
  "!"
514
658
  end
659
+ def priority(exp)
660
+ 40
661
+ end
515
662
  end
516
663
 
517
664
  class ExpNew < Exp
@@ -520,6 +667,10 @@ module Minjs
520
667
  @args = args
521
668
  end
522
669
 
670
+ def priority(exp)
671
+ 20
672
+ end
673
+
523
674
  def replace(from, to)
524
675
  if @name == from
525
676
  @name = from
@@ -559,6 +710,14 @@ module Minjs
559
710
  @args = args
560
711
  end
561
712
 
713
+ def priority(exp)
714
+ if @args.index(exp)
715
+ 999
716
+ else
717
+ 20
718
+ end
719
+ end
720
+
562
721
  def replace(from, to)
563
722
  @args.each_index do |i|
564
723
  arg = @args[i]
@@ -585,15 +744,20 @@ module Minjs
585
744
  #
586
745
  # => val.val2
587
746
  #
588
- class ExpProp < Exp
747
+ class ExpProp < ExpArg2
589
748
  def initialize(val, val2)
590
749
  @val = val
591
750
  if val2.kind_of? IdentifierName
592
751
  @val2 = ECMA262::ECMA262String.new(val2.val)
593
752
  else
594
- raise "val2 must be kind_of ItentiferName"
753
+ raise "internal error: val2 must be kind_of ItentiferName"
595
754
  end
596
755
  end
756
+
757
+ def priority(exp)
758
+ 20
759
+ end
760
+
597
761
  def traverse(parent, &block)
598
762
  @val.traverse(self, &block)
599
763
  @val2.traverse(self, &block)
@@ -606,11 +770,11 @@ module Minjs
606
770
  #
607
771
  # => val[val2]
608
772
  #
609
- class ExpPropBrac < Exp
610
- def initialize(val, val2)
611
- @val = val
612
- @val2 = val2
773
+ class ExpPropBrac < ExpArg2
774
+ def priority(exp)
775
+ 20
613
776
  end
777
+
614
778
  def traverse(parent, &block)
615
779
  @val.traverse(self, &block)
616
780
  @val2.traverse(self, &block)
@@ -625,6 +789,9 @@ module Minjs
625
789
  def sym
626
790
  ","
627
791
  end
792
+ def priority(exp)
793
+ 140
794
+ end
628
795
  end
629
796
  end
630
797
  end