Class: CodeTools::AST::HashLiteral

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

Instance Attribute Summary collapse

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Node

#ascii_graph, #attributes, #children, 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, array) ⇒ HashLiteral

Returns a new instance of HashLiteral.



118
119
120
121
# File 'lib/rubinius/code/ast/literals.rb', line 118

def initialize(line, array)
  @line = line
  @array = array
end

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



116
117
118
# File 'lib/rubinius/code/ast/literals.rb', line 116

def array
  @array
end

Instance Method Details

#bytecode(g) ⇒ Object



123
124
125
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
153
154
155
156
157
158
159
# File 'lib/rubinius/code/ast/literals.rb', line 123

def bytecode(g)
  pos(g)

  count = @array.size
  i = 0

  g.push_cpath_top
  g.find_const :Hash
  g.push_int count / 2
  g.send :new_from_literal, 1

  while i < count
    key = @array[i]
    value = @array[i + 1]

    if key
      g.dup
      key.bytecode(g)
      value.bytecode(g)
      g.send :[]=, 2
      g.pop
    else
      case value
      when HashLiteral
        value.merge_entries_bytecode(g)
      else
        g.push_rubinius
        g.find_const :Runtime
        g.swap
        value.bytecode(g)
        g.send :splat_hash_value, 2
      end
    end

    i += 2
  end
end

#defined(g) ⇒ Object



192
193
194
# File 'lib/rubinius/code/ast/literals.rb', line 192

def defined(g)
  g.push_literal "expression"
end

#merge_entries_bytecode(g) ⇒ Object



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
186
187
188
189
190
# File 'lib/rubinius/code/ast/literals.rb', line 161

def merge_entries_bytecode(g)
  count = @array.size
  i = 0

  while i < count
    key = @array[i]
    value = @array[i + 1]
    if key
      g.push_rubinius
      g.find_const :Runtime
      g.swap
      key.bytecode(g)
      value.bytecode(g)
      g.send :splat_hash_entry, 3
    else
      case value
      when HashLiteral
        value.merge_entries_bytecode(g)
      else
        g.push_rubinius
        g.find_const :Runtime
        g.swap
        value.bytecode(g)
        g.send :splat_hash_value, 2
      end
    end

    i += 2
  end
end

#to_sexpObject



196
197
198
# File 'lib/rubinius/code/ast/literals.rb', line 196

def to_sexp
  @array.inject([:hash]) { |s, x| s << (x ? x.to_sexp : [:hash_splat]) }
end