Class: CodeTools::AST::When

Inherits:
Node
  • Object
show all
Defined in:
lib/rubinius/code/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
# File 'lib/rubinius/code/ast/control_flow.rb', line 86

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

  case conditions
  when 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
  when SplatValue, ConcatArgs, PushArgs
    @splat = SplatWhen.new line, conditions
    @conditions = nil
  else
    @conditions = conditions
  end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#conditionsObject

Returns the value of attribute conditions.



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

def conditions
  @conditions
end

#singleObject

Returns the value of attribute single.



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

def single
  @single
end

#splatObject

Returns the value of attribute splat.



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

def splat
  @splat
end

Instance Method Details

#bytecode(g, done) ⇒ Object



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

def bytecode(g, done)
  pos(g)

  nxt = g.new_label
  body = g.new_label

  if @single
    @single.bytecode(g)
    g.goto_if_false nxt
  else
    if @conditions
      @conditions.body.each do |condition|
        condition.bytecode(g)
        g.goto_if_true 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



118
119
120
121
122
123
124
# File 'lib/rubinius/code/ast/control_flow.rb', line 118

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

#receiver_bytecode(g, done) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rubinius/code/ast/control_flow.rb', line 126

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

  if @single
    condition_bytecode(g, @single)
    g.goto_if_false nxt
  else
    if @conditions
      @conditions.body.each do |c|
        condition_bytecode(g, c)
        g.goto_if_true 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



183
184
185
186
187
188
189
190
191
# File 'lib/rubinius/code/ast/control_flow.rb', line 183

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