Class: Idl::ElseIfAst

Inherits:
AstNode show all
Includes:
Returns
Defined in:
lib/idl/ast.rb

Instance Method Summary collapse

Constructor Details

#initialize(input, interval, body_interval, cond, body_stmts) ⇒ ElseIfAst

Returns a new instance of ElseIfAst.

[View source]

5414
5415
5416
5417
# File 'lib/idl/ast.rb', line 5414

def initialize(input, interval, body_interval, cond, body_stmts)
  body = IfBodyAst.new(input, body_interval, body_stmts)
  super(input, interval, [cond, body])
end

Instance Method Details

#bodyObject

[View source]

5412
# File 'lib/idl/ast.rb', line 5412

def body = @children[1]

#condObject

[View source]

5411
# File 'lib/idl/ast.rb', line 5411

def cond = @children[0]

#return_value(symtab) ⇒ Integer, ... Originally defined in module Returns

Evaluate the compile-time return value of this node, or, if the node does not return (e.g., because it is an IfAst but there is no return on the taken path), execute the node and update the symtab

Parameters:

  • symtab (SymbolTable)

    The symbol table for the context

Returns:

  • (Integer)

    The return value, if it is integral

  • (Boolean)

    The return value, if it is boolean

  • (nil)

    if the return value is not compile-time-known

Raises:

  • ValueError if, during evaluation, a node without a compile-time value is found

#return_values(symtab) ⇒ Array<Integer>, Array<Boolean>

Evaluate all possible compile-time return values of this node, or, if the node does not return (e.g., because it is an IfAst but there is no return on a possible path), execute the node and update the symtab

Parameters:

  • symtab (SymbolTable)

    The symbol table for the context

Returns:

  • (Array<Integer>)

    The possible return values. Will be an empty array if there are no return values

  • (Array<Boolean>)

    The possible return values. Will be an empty array if there are no return values

Raises:

  • ValueError if, during evaluation, a node without a compile-time value is found

[View source]

5435
5436
5437
5438
5439
5440
5441
5442
5443
# File 'lib/idl/ast.rb', line 5435

def return_values(symtab)
  value_result = value_try do
    return cond.value(symtab) ? body.return_values(symtab) : EMPTY_ARRAY
  end
  value_else(value_result) do
    # might be taken, so add the possible return values
    body.return_values(symtab)
  end
end

#to_idlString

Return valid IDL representation of the node (and its subtree)

Returns:

  • (String)

    IDL code for the node

[View source]

5446
5447
5448
# File 'lib/idl/ast.rb', line 5446

def to_idl
  " else if (#{cond.to_idl}) { #{body.to_idl} }"
end

#type_check(symtab) ⇒ Object

[View source]

5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
# File 'lib/idl/ast.rb', line 5419

def type_check(symtab)
  cond.type_check(symtab)

  cond_value = nil
  value_try do
    cond_value = cond.value(symtab)
  end

  unless cond.type(symtab).convertable_to?(:boolean)
    type_error "'#{cond.text_value}' is not boolean"
  end

  body.type_check(symtab) unless cond_value == false
end