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

- (LatticeRefRewriter) initialize(bud_instance)

A new instance of LatticeRefRewriter



407
408
409
410
411
412
413
# File 'lib/bud/rewrite.rb', line 407

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

Instance Method Details

- (Boolean) is_lattice?(op)

Returns:

  • (Boolean)


449
450
451
# File 'lib/bud/rewrite.rb', line 449

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

- (Object) process_array(exp)



421
422
423
424
# File 'lib/bud/rewrite.rb', line 421

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

- (Object) process_call(exp)



431
432
433
434
435
436
437
438
439
# File 'lib/bud/rewrite.rb', line 431

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

- (Object) process_hash(exp)



426
427
428
429
# File 'lib/bud/rewrite.rb', line 426

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

- (Object) process_iter(exp)



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

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

- (Object) push_and_process(exp)

Raises:



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

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