Class: Opal::Rewriters::MlhsArgs::Arguments

Inherits:
Base
  • Object
show all
Defined in:
opal/lib/opal/rewriters/mlhs_args.rb

Constant Summary

Constants inherited from Base

Base::DUMMY_LOCATION

Instance Attribute Summary collapse

Attributes inherited from Base

#current_node

Instance Method Summary collapse

Methods inherited from Base

#append_to_body, #begin_with_stmts, #error, #prepend_to_body, #process, #s, s, #stmts_of

Constructor Details

#initialize(args) ⇒ Arguments

Returns a new instance of Arguments



70
71
72
73
74
75
76
77
# File 'opal/lib/opal/rewriters/mlhs_args.rb', line 70

def initialize(args)
  @args = args
  @rewritten = []
  @initialization = []
  @rewriter = MlhsRewriter.new

  split!
end

Instance Attribute Details

#initializationObject (readonly)

Returns the value of attribute initialization



68
69
70
# File 'opal/lib/opal/rewriters/mlhs_args.rb', line 68

def initialization
  @initialization
end

#rewrittenObject (readonly)

Returns the value of attribute rewritten



68
69
70
# File 'opal/lib/opal/rewriters/mlhs_args.rb', line 68

def rewritten
  @rewritten
end

Instance Method Details

#new_mlhs_tmpObject



83
84
85
86
87
# File 'opal/lib/opal/rewriters/mlhs_args.rb', line 83

def new_mlhs_tmp
  @counter ||= 0
  @counter += 1
  :$mlhs_tmp#{@counter}"
end

#reset_tmp_counter!Object



79
80
81
# File 'opal/lib/opal/rewriters/mlhs_args.rb', line 79

def reset_tmp_counter!
  @counter = 0
end

#split!Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'opal/lib/opal/rewriters/mlhs_args.rb', line 89

def split!
  @args.children.each do |arg|
    if arg.type == :mlhs
      var_name = new_mlhs_tmp
      rhs = s(:lvar, var_name)
      mlhs = @rewriter.process(arg)
      @initialization << s(:masgn, mlhs, rhs)
      @rewritten << s(:arg, var_name).updated(nil, nil, meta: { arg_name: var_name })
    else
      @rewritten << arg
    end
  end

  if @initialization.length == 1
    @initialization = @initialization[0]
  elsif @initialization.empty?
    @initialization = nil
  else
    @initialization = s(:begin, *@initialization)
  end
end