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.
7379 7380 7381 7382 |
# File 'lib/idlc/ast.rb', line 7379 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.
7378 7379 7380 |
# File 'lib/idlc/ast.rb', line 7378 def known_value @known_value end |
#unknown_mask ⇒ Object (readonly)
Returns the value of attribute unknown_mask.
7378 7379 7380 |
# File 'lib/idlc/ast.rb', line 7378 def unknown_mask @unknown_mask end |
Instance Method Details
#&(other) ⇒ Object
7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 |
# File 'lib/idlc/ast.rb', line 7387 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
7449 7450 7451 7452 7453 7454 7455 7456 7457 |
# File 'lib/idlc/ast.rb', line 7449 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
7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 |
# File 'lib/idlc/ast.rb', line 7417 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
7408 7409 7410 7411 7412 7413 7414 7415 7416 |
# File 'lib/idlc/ast.rb', line 7408 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
7383 7384 7385 |
# File 'lib/idlc/ast.rb', line 7383 def bit_length [@known_value.bit_length, @unknown_mask.bit_length].max end |
#to_s ⇒ Object
7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 |
# File 'lib/idlc/ast.rb', line 7458 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
7386 |
# File 'lib/idlc/ast.rb', line 7386 def zero? = false |
#|(other) ⇒ Object
7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 |
# File 'lib/idlc/ast.rb', line 7428 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 |