Class: Patm::Pattern::Arr
- Inherits:
-
Patm::Pattern
- Object
- Patm::Pattern
- Patm::Pattern::Arr
- Defined in:
- lib/patm.rb
Instance Method Summary collapse
- #compile_internal(free_index, target_name = "_obj") ⇒ Object
- #execute(mmatch, obj) ⇒ Object
-
#initialize(head, rest = nil, tail = []) ⇒ Arr
constructor
A new instance of Arr.
- #inspect ⇒ Object
Methods inherited from Patm::Pattern
#&, #[], build_from, #compile, #opt, #opt?, #rest?
Constructor Details
#initialize(head, rest = nil, tail = []) ⇒ Arr
Returns a new instance of Arr.
196 197 198 199 200 |
# File 'lib/patm.rb', line 196 def initialize(head, rest = nil, tail = []) @head = head @rest = rest @tail = tail end |
Instance Method Details
#compile_internal(free_index, target_name = "_obj") ⇒ Object
223 224 225 226 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/patm.rb', line 223 def compile_internal(free_index, target_name = "_obj") i = free_index srcs = [] ctxs = [] srcs << "#{target_name}.is_a?(::Array)" size_min = @head.size + @tail.size if @rest srcs << "#{target_name}.size >= #{size_min}" else srcs << "#{target_name}.size == #{size_min}" end elm_target_name = "#{target_name}_elm" @head.each_with_index do|h, hi| if h.is_a?(Obj) s, c, i = h.compile_internal(i, "#{target_name}[#{hi}]") srcs << "#{s}" if s ctxs << c else s, c, i = h.compile_internal(i, elm_target_name) srcs << "#{elm_target_name} = #{target_name}[#{hi}]; #{s}" if s ctxs << c end end unless @tail.empty? srcs << "#{target_name}_t = #{target_name}[(-#{@tail.size})..-1]; true" @tail.each_with_index do|t, ti| s, c, i = t.compile_internal(i, elm_target_name) srcs << "#{elm_target_name} = #{target_name}_t[#{ti}]; #{s}" if s ctxs << c end end if @rest tname = "#{target_name}_r" s, c, i = @rest.compile_internal(i, tname) srcs << "#{tname} = #{target_name}[#{@head.size}..-(#{@tail.size+1})];#{s}" if s ctxs << c end [ srcs.compact.map{|s| "(#{s})"}.join(" &&\n"), ctxs.flatten(1), i ] end |
#execute(mmatch, obj) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/patm.rb', line 202 def execute(mmatch, obj) size_min = @head.size + @tail.size return false unless obj.is_a?(Array) return false unless @rest ? (obj.size >= size_min) : (obj.size == size_min) @head.zip(obj[0..(@head.size - 1)]).all? {|pat, o| pat.execute(mmatch, o) } && @tail.zip(obj[(-@tail.size)..-1]).all? {|pat, o| pat.execute(mmatch, o) } && (!@rest || @rest.execute(mmatch, obj[@head.size..-(@tail.size+1)])) end |
#inspect ⇒ Object
215 216 217 218 219 220 221 |
# File 'lib/patm.rb', line 215 def inspect if @rest (@head + [@rest] + @tail).inspect else (@head + @tail).inspect end end |