Class: Idl::TernaryOperatorExpressionAst
- Includes:
- Rvalue
- Defined in:
- lib/idl/ast.rb
Overview
Represents a ternary operator
for example:
condition ? a : b
(a < b) ? c : d
Instance Method Summary collapse
- #condition ⇒ Object
- #false_expression ⇒ Object
-
#initialize(input, interval, condition, true_expression, false_expression) ⇒ TernaryOperatorExpressionAst
constructor
A new instance of TernaryOperatorExpressionAst.
-
#to_idl ⇒ String
Return valid IDL representation of the node (and its subtree).
- #true_expression ⇒ Object
-
#type(symtab) ⇒ Type
Given a specific symbol table, return the type of this node.
-
#type_check(symtab) ⇒ void
type check this node and all children.
-
#value(symtab) ⇒ Object
Return the compile-time-known value of the node.
-
#values(symtab) ⇒ Array<Integer>, ...
Return a complete list of possible compile-time-known values of the node, or raise a ValueError if the full list cannot be determined.
Constructor Details
#initialize(input, interval, condition, true_expression, false_expression) ⇒ TernaryOperatorExpressionAst
Returns a new instance of TernaryOperatorExpressionAst.
3635 3636 3637 |
# File 'lib/idl/ast.rb', line 3635 def initialize(input, interval, condition, true_expression, false_expression) super(input, interval, [condition, true_expression, false_expression]) end |
Instance Method Details
#condition ⇒ Object
3631 |
# File 'lib/idl/ast.rb', line 3631 def condition = @children[0] |
#false_expression ⇒ Object
3633 |
# File 'lib/idl/ast.rb', line 3633 def false_expression = @children[2] |
#to_idl ⇒ String
Return valid IDL representation of the node (and its subtree)
3714 |
# File 'lib/idl/ast.rb', line 3714 def to_idl = "#{condition.to_idl} ? #{true_expression.to_idl} : #{false_expression.to_idl}" |
#true_expression ⇒ Object
3632 |
# File 'lib/idl/ast.rb', line 3632 def true_expression = @children[1] |
#type(symtab) ⇒ Type
Given a specific symbol table, return the type of this node.
Should not be called until #type_check is called with the same arguments
3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 |
# File 'lib/idl/ast.rb', line 3667 def type(symtab) condition.type_check(symtab) value_result = value_try do cond = condition.value(symtab) # if the condition is compile-time-known, only check the used field if cond return true_expression.type(symtab) else return false_expression.type(symtab) end end value_else(value_result) do t = if true_expression.type(symtab).kind == :bits && false_expression.type(symtab).kind == :bits true_width = true_expression.type(symtab).width false_width = false_expression.type(symtab).width if true_width == :unknown || false_width == :unknown Type.new(:bits, width: :unknown) else Type.new(:bits, width: [true_width, false_width].max) end else true_expression.type(symtab).clone end if condition.type(symtab).const? && true_expression.type(symtab).const? && false_expression.type(symtab).const? t.make_const end return t end end |
#type_check(symtab) ⇒ void
3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 |
# File 'lib/idl/ast.rb', line 3640 def type_check(symtab) condition.type_check(symtab) if condition.type(symtab).kind == :bits type_error "ternary selector must be bool (maybe you meant '#{condition.text_value} != 0'?)" else type_error "ternary selector must be bool" unless condition.type(symtab).kind == :boolean end value_result = value_try do cond = condition.value(symtab) # if the condition is compile-time-known, only check the used field cond ? true_expression.type_check(symtab) : false_expression.type_check(symtab) end value_else(value_result) do true_expression.type_check(symtab) false_expression.type_check(symtab) unless true_expression.type(symtab).equal_to?(false_expression.type(symtab)) # we'll allow dissimilar if they are both bits type unless true_expression.type(symtab).kind == :bits && false_expression.type(symtab).kind == :bits type_error "True and false options must be same type (have #{true_expression.type(symtab)} and #{false_expression.type(symtab)})" end end end end |
#value(symtab) ⇒ Object
Return the compile-time-known value of the node
3699 3700 3701 |
# File 'lib/idl/ast.rb', line 3699 def value(symtab) condition.value(symtab) ? true_expression.value(symtab) : false_expression.value(symtab) end |
#values(symtab) ⇒ Array<Integer>, ...
Return a complete list of possible compile-time-known values of the node, or raise a ValueError if the full list cannot be determined
For most AstNodes, this will just be a single-entry array
3704 3705 3706 3707 3708 3709 3710 3711 |
# File 'lib/idl/ast.rb', line 3704 def values(symtab) value_result = value_try do return condition.value(symtab) ? true_expression.values(symtab) : false_expression.values(symtab) end value_else(value_result) do (true_expression.values(symtab) + false_expression.values(symtab)).uniq end end |