Class: Mgmg::IR::Multi
- Inherits:
-
Object
- Object
- Mgmg::IR::Multi
- Defined in:
- lib/mgmg/ir.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
Class Method Summary collapse
Instance Method Summary collapse
- #evaluate(s, c) ⇒ Object
- #evaluate3(s, a, c) ⇒ Object
-
#initialize(body) ⇒ Multi
constructor
A new instance of Multi.
- #initialize_copy(other) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(body) ⇒ Multi
Returns a new instance of Multi.
78 79 80 |
# File 'lib/mgmg/ir.rb', line 78 def initialize(body) @body = body end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
81 82 83 |
# File 'lib/mgmg/ir.rb', line 81 def body @body end |
Class Method Details
.sum(a, b) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/mgmg/ir.rb', line 99 def sum(a, b) unconsts, const = [], Const.new(0) case a when Multi if a.body[0].kind_of?(Const) const.value += a.body[0].value unconsts = a.body[1..(-1)] else unconsts = a.body.dup end when Const const.value += a.value else unconsts << a end case b when Multi if b.body[0].kind_of?(Const) const.value += b.body[0].value unconsts.concat(b.body[1..(-1)]) else unconsts.concat(b.body) end when Const const.value += b.value else unconsts << b end body = ( const.value == 0 ? unconsts : [const, *unconsts] ) case body.size when 0 const when 1 body[0] else new(body) end end |
Instance Method Details
#evaluate(s, c) ⇒ Object
85 86 87 88 89 |
# File 'lib/mgmg/ir.rb', line 85 def evaluate(s, c) @body.sum do |e| e.evaluate(s, c) end end |
#evaluate3(s, a, c) ⇒ Object
90 91 92 93 94 |
# File 'lib/mgmg/ir.rb', line 90 def evaluate3(s, a, c) @body.sum do |e| e.evaluate3(s, a, c) end end |
#initialize_copy(other) ⇒ Object
82 83 84 |
# File 'lib/mgmg/ir.rb', line 82 def initialize_copy(other) @body = other.body.dup end |
#to_s ⇒ Object
95 96 97 |
# File 'lib/mgmg/ir.rb', line 95 def to_s @body.map(&:to_s).join('+') end |