@@ -145,6 +145,8 @@ def fragment(code, sexp = nil)
145
145
Fragment . new ( code , sexp )
146
146
end
147
147
148
+ alias_method :f , :fragment
149
+
148
150
# Converts a ruby method name into its javascript equivalent for
149
151
# a method/function call. All ruby method names get prefixed with
150
152
# a '$', and if the name is a valid javascript identifier, it will
@@ -204,20 +206,20 @@ def top(sexp, options = {})
204
206
@scope . add_temp "def = __opal.Object._proto" if @scope . defines_defn
205
207
@helpers . keys . each { |h | @scope . add_temp "__#{ h } = __opal.#{ h } " }
206
208
207
- vars = [ fragment ( INDENT , sexp ) , @scope . to_vars , fragment ( "\n " , sexp ) ]
209
+ vars = [ f ( INDENT , sexp ) , @scope . to_vars , f ( "\n " , sexp ) ]
208
210
209
211
if @irb_vars
210
212
code . unshift fragment ( "if (!Opal.irb_vars) { Opal.irb_vars = {}; }\n " , sexp )
211
213
end
212
214
end
213
215
214
216
if @stub_methods
215
- stubs = fragment ( "\n #{ INDENT } __opal.add_stubs([" + @method_calls . keys . map { |k | "'$#{ k } '" } . join ( ", " ) + "]);\n " , sexp )
217
+ stubs = f ( "\n #{ INDENT } __opal.add_stubs([" + @method_calls . keys . map { |k | "'$#{ k } '" } . join ( ", " ) + "]);\n " , sexp )
216
218
else
217
219
stubs = [ ]
218
220
end
219
221
220
- [ fragment ( "(function(__opal) {\n " , sexp ) , vars , stubs , code , fragment ( "\n })(Opal);\n " , sexp ) ]
222
+ [ f ( "(function(__opal) {\n " , sexp ) , vars , stubs , code , f ( "\n })(Opal);\n " , sexp ) ]
221
223
end
222
224
223
225
# Every time the parser enters a new scope, this is called with
@@ -326,7 +328,7 @@ def in_while?
326
328
# @param [Array] sexp the sexp to process
327
329
# @param [Symbol] level the level to process (see `LEVEL`)
328
330
# @return [String]
329
- def process ( sexp , level )
331
+ def process ( sexp , level = :expr )
330
332
type = sexp . shift
331
333
meth = "process_#{ type } "
332
334
raise "Unsupported sexp: #{ type } " unless respond_to? meth
@@ -556,20 +558,10 @@ def process_sym(sexp, level)
556
558
end
557
559
558
560
def process_int ( sexp , level )
559
- handle_number sexp , level
561
+ fragment ( ( level == :recv ? "( #{ sexp [ 0 ] } )" : sexp [ 0 ] . to_s ) , sexp )
560
562
end
561
563
562
- def process_float ( sexp , level )
563
- handle_number sexp , level
564
- end
565
-
566
- def handle_number ( sexp , level )
567
- if level == :recv
568
- fragment ( "(#{ sexp [ 0 ] } )" , sexp )
569
- else
570
- fragment ( sexp [ 0 ] . to_s , sexp )
571
- end
572
- end
564
+ alias_method :process_float , :process_int
573
565
574
566
def process_regexp ( sexp , level )
575
567
val = sexp [ 0 ]
@@ -595,62 +587,55 @@ def process_dregx(sexp, level)
595
587
end
596
588
597
589
def process_dot2 ( sexp , level )
598
- lhs = process sexp [ 0 ] , :expr
599
- rhs = process sexp [ 1 ] , :expr
600
590
@helpers [ :range ] = true
601
591
602
- [ fragment ( "__range(" , sexp ) , lhs , fragment ( ", " , sexp ) , rhs , fragment ( ", false)" , sexp ) ]
592
+ [ f ( "__range(" , sexp ) , process ( sexp [ 0 ] ) , f ( ", " , sexp ) , process ( sexp [ 1 ] ) , f ( ", false)" , sexp ) ]
603
593
end
604
594
605
595
def process_dot3 ( sexp , level )
606
- lhs = process sexp [ 0 ] , :expr
607
- rhs = process sexp [ 1 ] , :expr
608
596
@helpers [ :range ] = true
609
597
610
- [ fragment ( "__range(" , sexp ) , lhs , fragment ( ", " , sexp ) , rhs , fragment ( ", true)" , sexp ) ]
598
+ [ f ( "__range(" , sexp ) , process ( sexp [ 0 ] ) , f ( ", " , sexp ) , process ( sexp [ 1 ] ) , f ( ", true)" , sexp ) ]
611
599
end
612
600
613
601
# s(:str, "string")
614
602
def process_str ( sexp , level )
615
603
str = sexp . shift
616
- if str == @file
617
- @uses_file = true
618
- fragment ( @file . inspect , sexp )
619
- else
620
- fragment ( str . inspect , sexp )
621
- end
604
+ @uses_file = true if str == @file
605
+
606
+ f ( str . inspect , sexp )
622
607
end
623
608
624
609
def process_defined ( sexp , level )
625
610
part = sexp [ 0 ]
626
611
case part [ 0 ]
627
612
when :self
628
- fragment ( " self" . inspect , sexp )
613
+ f ( "' self'" , sexp )
629
614
when :nil
630
- fragment ( " nil" . inspect , sexp )
615
+ f ( "' nil'" , sexp )
631
616
when :true
632
- fragment ( " true" . inspect , sexp )
617
+ f ( "' true'" , sexp )
633
618
when :false
634
- fragment ( " false" . inspect , sexp )
619
+ f ( "' false'" , sexp )
635
620
when :call
636
621
mid = mid_to_jsid part [ 2 ] . to_s
637
- recv = part [ 1 ] ? process ( part [ 1 ] , :expr ) : fragment ( current_self , sexp )
638
- [ fragment ( "(" , sexp ) , recv , fragment ( "#{ mid } ? 'method' : nil)" , sexp ) ]
622
+ recv = part [ 1 ] ? process ( part [ 1 ] , :expr ) : f ( current_self , sexp )
623
+ [ f ( "(" , sexp ) , recv , f ( "#{ mid } ? 'method' : nil)" , sexp ) ]
639
624
when :xstr , :dxstr
640
- [ fragment ( "(typeof(" , sexp ) , process ( part , :expr ) , fragment ( ") !== 'undefined')" , sexp ) ]
625
+ [ f ( "(typeof(" , sexp ) , process ( part , :expr ) , f ( ") !== 'undefined')" , sexp ) ]
641
626
when :const
642
- fragment ( "(__scope.#{ part [ 1 ] . to_s } != null)" , sexp )
627
+ f ( "(__scope.#{ part [ 1 ] . to_s } != null)" , sexp )
643
628
when :colon2
644
- fragment ( "false" , sexp )
629
+ f ( "false" , sexp )
645
630
when :colon3
646
- fragment ( "(__opal.Object._scope.#{ sexp [ 0 ] [ 1 ] } == null ? nil : 'constant')" , sexp )
631
+ f ( "(__opal.Object._scope.#{ sexp [ 0 ] [ 1 ] } == null ? nil : 'constant')" , sexp )
647
632
when :ivar
648
633
ivar_name = part [ 1 ] . to_s [ 1 ..-1 ]
649
634
with_temp do |t |
650
- fragment ( "((#{ t } = #{ current_self } [#{ ivar_name . inspect } ], #{ t } != null && #{ t } !== nil) ? 'instance-variable' : nil)" , sexp )
635
+ f ( "((#{ t } = #{ current_self } [#{ ivar_name . inspect } ], #{ t } != null && #{ t } !== nil) ? 'instance-variable' : nil)" , sexp )
651
636
end
652
637
when :lvar
653
- fragment ( "local-variable" , sexp )
638
+ f ( "local-variable" , sexp )
654
639
else
655
640
raise "bad defined? part: #{ part [ 0 ] } "
656
641
end
@@ -2254,7 +2239,7 @@ def process_resbody(exp, level)
2254
2239
2255
2240
# FIXME: Hack.. grammar should remove top level begin.
2256
2241
def process_begin ( exp , level )
2257
- result = process exp [ 0 ] , level
2242
+ process exp [ 0 ] , level
2258
2243
end
2259
2244
2260
2245
def process_next ( exp , level )
0 commit comments