Class: Opal::Rewriters::PatternMatching::PatternConverter

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

Constant Summary

Constants inherited from Base

Base::DUMMY_LOCATION

Instance Attribute Summary

Attributes inherited from Base

#current_node

Instance Method Summary collapse

Methods inherited from Base

#append_to_body, #begin_with_stmts, #dynamic!, #error, #on_top, #prepend_to_body, #process, s, #s, #stmts_of

Constructor Details

#initialize(pat) ⇒ PatternConverter

Returns a new instance of PatternConverter.



118
119
120
121
# File 'lib/opal/rewriters/pattern_matching.rb', line 118

def initialize(pat)
  @pat = pat
  @variables = []
end

Instance Method Details

#on_array_pattern(node, tail = false) ⇒ Object

[0, 1, 2] or [*, 0, 1] or [0, 1, *]



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/opal/rewriters/pattern_matching.rb', line 197

def on_array_pattern(node, tail = false)
  children = *node
  children << s(:match_rest) if tail

  fixed_size = true
  array_size = 0

  children = children.each do |i|
    case i.type
    when :match_rest
      fixed_size = false
    else
      array_size += 1
    end
  end

  array(
    s(:sym, :array),
    to_ast(fixed_size),
    to_ast(array_size),
    to_ast(children.map(&method(:process)))
  )
end

#on_array_pattern_with_tail(node) ⇒ Object

[0, 1, 2,]



222
223
224
# File 'lib/opal/rewriters/pattern_matching.rb', line 222

def on_array_pattern_with_tail(node)
  on_array_pattern(node, true)
end

#on_const_pattern(node) ⇒ Object

MyStructName



192
193
194
# File 'lib/opal/rewriters/pattern_matching.rb', line 192

def on_const_pattern(node)
  array(s(:sym, :all), *node.children.map(&method(:process)))
end

#on_find_pattern(node) ⇒ Object

[*, a, b, *]



256
257
258
259
260
261
262
# File 'lib/opal/rewriters/pattern_matching.rb', line 256

def on_find_pattern(node)
  children = *node

  children = children.map(&method(:process))

  array(s(:sym, :find), array(*children))
end

#on_hash_pattern(node) ⇒ Object

b:



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/opal/rewriters/pattern_matching.rb', line 227

def on_hash_pattern(node)
  children = *node

  any_size = children.empty? ? to_ast(false) : to_ast(true)

  children = children.map do |i|
    case i.type
    when :pair
      array(i.children[0], process(i.children[1]))
    when :match_var
      array(s(:sym, i.children[0]), process(i))
    when :match_nil_pattern
      any_size = to_ast(false)
      nil
    when :match_rest
      # Capturing rest?
      if i.children.first
        any_size = process(i.children.first)
      else
        any_size = to_ast(true)
      end
      nil
    end
  end.compact

  array(s(:sym, :hash), any_size, array(*children))
end

#on_literal(node) ⇒ Object Also known as: on_int, on_float, on_complex, on_rational, on_array, on_str, on_dstr, on_xstr, on_sym, on_irange, on_erange, on_const, on_regexp, on_lambda, on_begin



152
153
154
# File 'lib/opal/rewriters/pattern_matching.rb', line 152

def on_literal(node)
  array(s(:sym, :lit), node)
end

#on_match_alt(node) ⇒ Object

{} | []



187
188
189
# File 'lib/opal/rewriters/pattern_matching.rb', line 187

def on_match_alt(node)
  array(s(:sym, :any), *node.children.map(&method(:process)))
end

#on_match_as(node) ⇒ Object

[...] => a



145
146
147
148
149
150
# File 'lib/opal/rewriters/pattern_matching.rb', line 145

def on_match_as(node)
  pat, save = *node

  process(save)
  array(s(:sym, :save), process(pat))
end

#on_match_rest(node) ⇒ Object

*



178
179
180
181
182
183
184
# File 'lib/opal/rewriters/pattern_matching.rb', line 178

def on_match_rest(node)
  if node.children.empty?
    array(s(:sym, :rest))
  else
    array(s(:sym, :rest), process(node.children.first))
  end
end

#on_match_var(node) ⇒ Object

a



136
137
138
139
140
141
142
# File 'lib/opal/rewriters/pattern_matching.rb', line 136

def on_match_var(node)
  var, = *node

  @variables << var

  s(:sym, :var)
end

#on_pin(node) ⇒ Object

^a



173
174
175
# File 'lib/opal/rewriters/pattern_matching.rb', line 173

def on_pin(node)
  on_literal(node.children.first)
end

#patternObject



127
128
129
# File 'lib/opal/rewriters/pattern_matching.rb', line 127

def pattern
  @outpat
end

#run!Object



123
124
125
# File 'lib/opal/rewriters/pattern_matching.rb', line 123

def run!
  @outpat = process(@pat)
end

#variablesObject



131
132
133
# File 'lib/opal/rewriters/pattern_matching.rb', line 131

def variables
  @variables.map { |i| s(:lvasgn, i) }
end