Class: LatticeRefRewriter

Inherits:
SexpProcessor
  • Object
show all
Defined in:
lib/bud/rewrite.rb

Overview

Rewrite references to lattice identifiers that appear in rule bodies. A reference to a lattice identifier returns the associated lattice wrapper. When the identifier appears at the top-level of the rule RHS, that is fine (since we want the wrapper to do wiring). But for references that appear inside rule bodies, we want to instead fetch the current value associated with the lattice wrapper.

Instance Method Summary collapse

Constructor Details

#initialize(bud_instance) ⇒ LatticeRefRewriter

Returns a new instance of LatticeRefRewriter



413
414
415
416
417
418
419
# File 'lib/bud/rewrite.rb', line 413

def initialize(bud_instance)
  super()
  self.require_empty = false
  self.expected = Sexp
  @bud_instance = bud_instance
  @elem_stack = []
end

Instance Method Details

#is_lattice?(op) ⇒ Boolean

Returns:

  • (Boolean)


458
459
460
# File 'lib/bud/rewrite.rb', line 458

def is_lattice?(op)
  @bud_instance.lattices.has_key? op.to_sym
end

#process_array(exp) ⇒ Object



430
431
432
433
# File 'lib/bud/rewrite.rb', line 430

def process_array(exp)
  new_body = exp.sexp_body.map {|t| push_and_process(t)}
  return s(:array, *new_body)
end

#process_call(exp) ⇒ Object



440
441
442
443
444
445
446
447
448
# File 'lib/bud/rewrite.rb', line 440

def process_call(exp)
  tag, recv, op, *args = exp

  if recv.nil? and args.empty? and is_lattice?(op) and @elem_stack.size > 0
    return s(:call, exp, :current_value)
  else
    return s(tag, process(recv), op, *(args.map{|a| process(a)}))
  end
end

#process_hash(exp) ⇒ Object



435
436
437
438
# File 'lib/bud/rewrite.rb', line 435

def process_hash(exp)
  new_body = exp.sexp_body.map {|t| push_and_process(t)}
  return s(:hash, *new_body)
end

#process_iter(exp) ⇒ Object



421
422
423
424
425
426
427
428
# File 'lib/bud/rewrite.rb', line 421

def process_iter(exp)
  tag, recv, iter_args, body = exp
  new_body = push_and_process(body)
  if (iter_args == 0)
    iter_args = [:args]
  end
  return s(tag, process(recv), process(iter_args), new_body)
end

#push_and_process(exp) ⇒ Object

Raises:



450
451
452
453
454
455
456
# File 'lib/bud/rewrite.rb', line 450

def push_and_process(exp)
  obj_id = exp.object_id
  @elem_stack.push(obj_id)
  rv = process(exp)
  raise Bud::Error unless @elem_stack.pop == obj_id
  return rv
end