Class: Rubinius::ToolSet.current::TS::AST::When

Inherits:
Node
  • Object
show all
Defined in:
lib/rubinius/ast/control_flow.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Node

#ascii_graph, #attributes, #children, #defined, match_arguments?, match_send?, #new_block_generator, #new_generator, #node_name, #or_bytecode, #pos, #set_child, #transform, transform, transform_comment, transform_kind, transform_kind=, transform_name, #value_defined, #visit, #walk

Constructor Details

#initialize(line, conditions, body) ⇒ When

Returns a new instance of When.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rubinius/ast/control_flow.rb', line 86

def initialize(line, conditions, body)
  @line = line
  @body = body || NilLiteral.new(line)
  @splat = nil
  @single = nil

  if conditions.kind_of? ConcatArgs
    @splat = SplatWhen.new line, conditions.rest
    conditions = conditions.array
  end

  if conditions.kind_of? ArrayLiteral
    if conditions.body.last.kind_of? When
      last = conditions.body.pop
      if last.conditions.kind_of? ArrayLiteral
        conditions.body.concat last.conditions.body
      elsif last.single
        @splat = SplatWhen.new line, last.single
      else
        @splat = SplatWhen.new line, last.conditions
      end
    end

    if conditions.body.size == 1 and !@splat
      @single = conditions.body.first
    else
      @conditions = conditions
    end
  elsif conditions.kind_of? SplatValue
    @splat = SplatWhen.new line, conditions.value
    @conditions = nil
  else
    @conditions = conditions
  end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



84
85
86
# File 'lib/rubinius/ast/control_flow.rb', line 84

def body
  @body
end

#conditionsObject

Returns the value of attribute conditions.



84
85
86
# File 'lib/rubinius/ast/control_flow.rb', line 84

def conditions
  @conditions
end

#singleObject

Returns the value of attribute single.



84
85
86
# File 'lib/rubinius/ast/control_flow.rb', line 84

def single
  @single
end

#splatObject

Returns the value of attribute splat.



84
85
86
# File 'lib/rubinius/ast/control_flow.rb', line 84

def splat
  @splat
end

Instance Method Details

#bytecode(g, done) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rubinius/ast/control_flow.rb', line 158

def bytecode(g, done)
  pos(g)

  nxt = g.new_label
  body = g.new_label

  if @single
    @single.bytecode(g)
    g.gif nxt
  else
    if @conditions
      @conditions.body.each do |condition|
        condition.bytecode(g)
        g.git body
      end
    end

    @splat.bytecode(g, body, nxt) if @splat
    g.goto nxt

    body.set!
  end

  @body.bytecode(g)
  g.goto done

  nxt.set!
end

#condition_bytecode(g, condition) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/rubinius/ast/control_flow.rb', line 122

def condition_bytecode(g, condition)
  condition.pos(g)
  g.dup
  condition.bytecode(g)
  g.swap
  g.send :===, 1
end

#receiver_bytecode(g, done) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rubinius/ast/control_flow.rb', line 130

def receiver_bytecode(g, done)
  body = g.new_label
  nxt = g.new_label

  if @single
    condition_bytecode(g, @single)
    g.gif nxt
  else
    if @conditions
      @conditions.body.each do |c|
        condition_bytecode(g, c)
        g.git body
      end
    end

    @splat.receiver_bytecode(g, body, nxt) if @splat
    g.goto nxt

    body.set!
  end

  g.pop
  @body.bytecode(g)
  g.goto done

  nxt.set!
end

#to_sexpObject



187
188
189
190
191
192
193
194
195
# File 'lib/rubinius/ast/control_flow.rb', line 187

def to_sexp
  if @single
    conditions_sexp = [:array, @single.to_sexp]
  else
    conditions_sexp = @conditions ? @conditions.to_sexp : []
    conditions_sexp << @splat.to_sexp if @splat
  end
  [:when, conditions_sexp, @body.to_sexp]
end