Class: Idl::UnknownLiteral
- Inherits:
-
Object
- Object
- Idl::UnknownLiteral
- Defined in:
- lib/idlc/ast.rb
Instance Attribute Summary collapse
-
#known_value ⇒ Object
readonly
Returns the value of attribute known_value.
-
#unknown_mask ⇒ Object
readonly
Returns the value of attribute unknown_mask.
Instance Method Summary collapse
- #&(other) ⇒ Object
- #<<(shamt) ⇒ Object
- #<=(other) ⇒ Object
- #==(other) ⇒ Object
- #bit_length ⇒ Object
-
#initialize(known_value, unknown_mask) ⇒ UnknownLiteral
constructor
A new instance of UnknownLiteral.
- #to_s ⇒ Object
- #zero? ⇒ Boolean
- #|(other) ⇒ Object
Constructor Details
#initialize(known_value, unknown_mask) ⇒ UnknownLiteral
Returns a new instance of UnknownLiteral.
7364 7365 7366 7367 |
# File 'lib/idlc/ast.rb', line 7364 def initialize(known_value, unknown_mask) @known_value = known_value @unknown_mask = unknown_mask end |
Instance Attribute Details
#known_value ⇒ Object (readonly)
Returns the value of attribute known_value.
7363 7364 7365 |
# File 'lib/idlc/ast.rb', line 7363 def known_value @known_value end |
#unknown_mask ⇒ Object (readonly)
Returns the value of attribute unknown_mask.
7363 7364 7365 |
# File 'lib/idlc/ast.rb', line 7363 def unknown_mask @unknown_mask end |
Instance Method Details
#&(other) ⇒ Object
7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 |
# File 'lib/idlc/ast.rb', line 7372 def &(other) if other.is_a?(Integer) new_known_value = @known_value & other new_unknown_mask = @unknown_mask & other if new_unknown_mask.zero? new_known_value else UnknownLiteral.new(new_known_value, new_unknown_mask) end elsif other.is_a?(UnknownLiteral) new_known_value = @known_value & other.known_value new_unknown_mask = (@unknown_mask | other.unknown_mask) & ~(~@known_value & ~ @unknown_mask) & ~(~other.known_value & ~other.unknown_mask) if new_unknown_mask.zero? new_known_value else UnknownLiteral.new(new_known_value, new_unknown_mask) end else raise "unexpected" end end |
#<<(shamt) ⇒ Object
7434 7435 7436 7437 7438 7439 7440 7441 7442 |
# File 'lib/idlc/ast.rb', line 7434 def <<(shamt) if shamt.is_a?(Integer) new_known_value = @known_value << shamt new_unknown_mask = @unknown_mask << shamt UnknownLiteral.new(new_known_value, new_unknown_mask) else raise "cannot left shift by unknown amount" end end |
#<=(other) ⇒ Object
7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 |
# File 'lib/idlc/ast.rb', line 7402 def <=(other) throw(:value_error, :unknown_value) unless @unknown_mask.zero? if other.is_a?(Integer) @known_value <= other elsif other.is_a?(UnknownLiteral) throw(:value_error, :unknown_value) unless other.unknown_mask.zero? @known_value <= other.known_value else raise "unexpected" end end |
#==(other) ⇒ Object
7393 7394 7395 7396 7397 7398 7399 7400 7401 |
# File 'lib/idlc/ast.rb', line 7393 def ==(other) if other.is_a?(Integer) @known_value == other && @unknown_mask.zero? elsif other.is_a?(UnknownLiteral) (@known_value & ~@unknown_mask) == (other.known_value & ~other.unknown_mask) && @unknown_mask == other.unknown_mask else raise "unexpected" end end |
#bit_length ⇒ Object
7368 7369 7370 |
# File 'lib/idlc/ast.rb', line 7368 def bit_length [@known_value.bit_length, @unknown_mask.bit_length].max end |
#to_s ⇒ Object
7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 |
# File 'lib/idlc/ast.rb', line 7443 def to_s known_str = @known_value.to_s(2).reverse x = @unknown_mask.to_s(2).reverse v = [] ([known_str.size, x.size].max).times do |i| if i >= known_str.size v << ((x[i] == "1") ? "x" : "0") elsif i >= x.size v << known_str[i] else if x[i] == "1" v << "x" else v << known_str[i] end end end "#{[known_str.size, x.size].max}'b#{v.reverse.join("")}" end |
#zero? ⇒ Boolean
7371 |
# File 'lib/idlc/ast.rb', line 7371 def zero? = false |
#|(other) ⇒ Object
7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 |
# File 'lib/idlc/ast.rb', line 7413 def |(other) if other.is_a?(Integer) new_known_value = @known_value | other new_unknown_mask = @unknown_mask & ~other if new_unknown_mask.zero? new_known_value else UnknownLiteral.new(new_known_value, new_unknown_mask) end elsif other.is_a?(UnknownLiteral) new_known_value = @known_value | other.known_value new_unknown_mask = (@unknown_mask | other.unknown_mask) & ~(@known_value & ~@unknown_mask) & ~(other.known_value & ~other.unknown_mask) if new_unknown_mask.zero? new_known_value else UnknownLiteral.new(new_known_value, new_unknown_mask) end else raise "unexpected" end end |