Class: Pione::PNML::ConditionalBranch

Inherits:
PioneModel
  • Object
show all
Defined in:
lib/pione/pnml/pione-model.rb

Overview

ConditionalBranch is a class represents PIONE's conditional branch declaration.

Constant Summary collapse

TEMPLATE_IF =
<<-TXT
  if %s
  %s
  end
TXT
TEMPLATE_IF_ELSE =
<<-TXT
  if %s
  %s
  else
  %s
  end
TXT
TEMPLATE_CASE =
<<-TXT
  case %s
  %s
  end
TXT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from PioneModel

#indent

Constructor Details

#initialize(type, condition) ⇒ ConditionalBranch

Returns a new instance of ConditionalBranch.



684
685
686
687
688
# File 'lib/pione/pnml/pione-model.rb', line 684

def initialize(type, condition)
  @type = type
  @condition = condition
  @table = Hash.new {|h,k| h[k] = []}
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



681
682
683
# File 'lib/pione/pnml/pione-model.rb', line 681

def condition
  @condition
end

#tableObject (readonly)

Returns the value of attribute table.



682
683
684
# File 'lib/pione/pnml/pione-model.rb', line 682

def table
  @table
end

Instance Method Details

#as_declaration(option = {}) ⇒ Object



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# File 'lib/pione/pnml/pione-model.rb', line 690

def as_declaration(option={})
  case @type
  when :"if"
    branch_then = @table[:then].map do |rule|
      rule.as_declaration(option.merge(level: option[:level] + 1))
    end.join("\n")

    if @table[:else].empty?
      indent(Util::Indentation.cut(TEMPLATE_IF) % [@condition, branch_then], option)
    else
      branch_else = @table[:else].map do |rule|
        rule.as_declaration(option.merge(level: option[:level] + 1))
      end.join("\n")
      indent(Util::Indentation.cut(TEMPLATE_IF_ELSE) % [@condition, branch_then, branch_else], option)
    end
  when :"case"
    branches = @table.each_with_object([]) do |(val, rules), lines|
      lines << ((val == :else) ? "else" : "when %s" % val)
      level = (option[:level] || 0) + 1
      lines.concat(rules.map{|rule| rule.as_declaration(option.merge(level: level))})
    end.join("\n")
    indent(Util::Indentation.cut(TEMPLATE_CASE) % [@condition, branches], option)
  end
end