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)

Returns a new instance of LatticeRefRewriter



410
411
412
413
414
415
416
# File 'lib/bud/rewrite.rb', line 410

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)


452
453
454
# File 'lib/bud/rewrite.rb', line 452

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

- (Object) process_array(exp)



424
425
426
427
# File 'lib/bud/rewrite.rb', line 424

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)



434
435
436
437
438
439
440
441
442
# File 'lib/bud/rewrite.rb', line 434

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)



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

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)



418
419
420
421
422
# File 'lib/bud/rewrite.rb', line 418

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:



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

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