Class: RubyNext::Language::Rewriters::Predicates::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-next/language/rewriters/2.7/pattern_matching.rb

Direct Known Subclasses

CaseIn, Noop

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



115
116
117
118
119
120
121
122
123
124
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 115

def initialize
  # total number of predicates
  @count = 0
  # cache of all predicates by path
  @predicates_by_path = {}
  # all predicates and their dirty state
  @store = {}

  @current_path = []
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



112
113
114
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 112

def count
  @count
end

#current_pathObject (readonly)

Returns the value of attribute current_path.



112
113
114
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 112

def current_path
  @current_path
end

#predicates_by_pathObject (readonly)

Returns the value of attribute predicates_by_path.



112
113
114
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 112

def predicates_by_path
  @predicates_by_path
end

#storeObject (readonly)

Returns the value of attribute store.



112
113
114
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 112

def store
  @store
end

#terminatedObject (readonly) Also known as: terminated?

Returns the value of attribute terminated.



112
113
114
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 112

def terminated
  @terminated
end

Instance Method Details

#popObject



135
136
137
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 135

def pop
  current_path.pop
end

#pred?(name) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 151

def pred?(name)
  predicates_by_path.key?(current_path + [name])
end

#predicate_clause(name, node) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 143

def predicate_clause(name, node)
  if pred?(name)
    read_pred(name)
  else
    write_pred(name, node)
  end
end

#process(ast) ⇒ Object



174
175
176
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 174

def process(ast)
  Processor.new(self).process(ast)
end

#push(path) ⇒ Object



131
132
133
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 131

def push(path)
  current_path << path
end

#read_pred(name) ⇒ Object



155
156
157
158
159
160
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 155

def read_pred(name)
  lvar = predicates_by_path.fetch(current_path + [name])
  # mark as used
  store[lvar] = true
  s(:lvar, lvar)
end

#reset!Object



126
127
128
129
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 126

def reset!
  @current_path = []
  @terminated = false
end

#terminate!Object



139
140
141
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 139

def terminate!
  @terminated = true
end

#write_pred(name, node) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ruby-next/language/rewriters/2.7/pattern_matching.rb', line 162

def write_pred(name, node)
  return node if terminated?
  @count += 1
  lvar = :"__p_#{count}__"
  predicates_by_path[current_path + [name]] = lvar
  store[lvar] = false

  s(:lvasgn,
    lvar,
    node)
end