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



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

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)


460
461
462
# File 'lib/bud/rewrite.rb', line 460

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

#process_array(exp) ⇒ Object



432
433
434
435
# File 'lib/bud/rewrite.rb', line 432

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



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

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



437
438
439
440
# File 'lib/bud/rewrite.rb', line 437

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



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

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

#push_and_process(exp) ⇒ Object

Raises:



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

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