Class: Opal::Rewriters::PatternMatching

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

Defined Under Namespace

Classes: PatternConverter

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

#initializePatternMatching

Returns a new instance of PatternMatching.



8
9
10
11
# File 'lib/opal/rewriters/pattern_matching.rb', line 8

def initialize
  @depth = 0
  super
end

Instance Method Details

#on_case_match(node) ⇒ Object

case a; in b; end



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/opal/rewriters/pattern_matching.rb', line 39

def on_case_match(node)
  @depth += 1

  cmvar = :"$cmvar#{@depth}"

  from, *cases, els = *node

  if els
    process els
  else
    els = raise_no_matching_pattern_error(cmvar)
  end

  s(:begin,
    s(:lvasgn, cmvar, from),
    single_case_match(cmvar, *cases, els)
  )
end

#on_match_pattern(node) ⇒ Object

a => b



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/opal/rewriters/pattern_matching.rb', line 14

def on_match_pattern(node)
  from, pat = *node

  s(:begin,
    s(:lvasgn, :"$pmvar", from),
    s(:if,
      convert_full_pattern(from, pat),
      nil,
      raise_no_matching_pattern_error(:"$pmvar")
    )
  )
end

#on_match_pattern_p(node) ⇒ Object

a in b



28
29
30
31
32
33
34
35
36
# File 'lib/opal/rewriters/pattern_matching.rb', line 28

def on_match_pattern_p(node)
  from, pat = *node

  s(:if,
    convert_full_pattern(from, pat),
    s(:true),
    s(:false)
  )
end