Class: Idl::IfBodyAst

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

Instance Method Summary collapse

Constructor Details

#initialize(input, interval, body_stmts) ⇒ IfBodyAst

Returns a new instance of IfBodyAst.



5296
5297
5298
5299
5300
5301
5302
# File 'lib/idl/ast.rb', line 5296

def initialize(input, interval, body_stmts)
  if body_stmts.empty?
    super("", 0...0, EMPTY_ARRAY)
  else
    super(input, interval, body_stmts)
  end
end

Instance Method Details

#execute(symtab) ⇒ void

This method returns an undefined value.

“execute” the statement by updating the variables in the symbol table

Parameters:

  • symtab (SymbolTable)

    The symbol table for the context

Raises:

  • ValueError if some part of the statement cannot be executed at compile time



5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
# File 'lib/idl/ast.rb', line 5368

def execute(symtab)
  err = nil
  stmts.each do |s|
    value_result = value_try do
      if s.is_a?(Returns)
        value_result = value_try do
          v = s.return_value(symtab)
          break unless v.nil? # nil means this is a conditional return and the condition is false

        end
        value_else(value_result) do
          # not known, keep going
          err = :value_error
        end
      else
        s.execute(symtab)
      end
    end
    value_else(value_result) do
      # keep going so that we invalidate everything
      err = :value_error
    end
  end
  throw err unless err.nil?
end

#execute_unknown(symtab) ⇒ Object

nothing to do for a function call



5395
5396
5397
5398
5399
# File 'lib/idl/ast.rb', line 5395

def execute_unknown(symtab)
  stmts.each do |s|
    s.execute_unknown(symtab)
  end
end

#return_value(symtab) ⇒ Integer, ...

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



5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
# File 'lib/idl/ast.rb', line 5318

def return_value(symtab)
  symtab.push(self)
  begin
    stmts.each do |s|
      if s.is_a?(Returns)
        v = s.return_value(symtab)
        unless v.nil?
          return v
        end
      else
        s.execute(symtab)
      end
    end
  ensure
    symtab.pop
  end

  nil
end

#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



5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
# File 'lib/idl/ast.rb', line 5339

def return_values(symtab)
  values = []
  symtab.push(self)
  begin
    value_result = value_try do
      stmts.each do |s|
        if s.is_a?(Returns)
          value_result = value_try do
            v = s.return_value(symtab)
            unless v.nil?
              return values.push(v).uniq
            end
          end
          value_else(value_result) do
            values += s.return_values(symtab)
          end
        else
          s.execute(symtab)
        end
      end
    end
  ensure
    symtab.pop
  end

  values.uniq
end

#stmtsObject



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

def stmts = @children

#to_idlString

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

Returns:

  • (String)

    IDL code for the node



5402
5403
5404
# File 'lib/idl/ast.rb', line 5402

def to_idl
  stmts.map(&:to_idl).join("")
end

#type_check(symtab) ⇒ void

This method returns an undefined value.

type check this node and all children

Calls to #type and/or #value may depend on type_check being called first with the same symtab. If not, those functions may raise an AstNode::InternalError

Parameters:

Raises:



5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
# File 'lib/idl/ast.rb', line 5305

def type_check(symtab)
  symtab.push(self)

  begin
    stmts.each do |s|
      s.type_check(symtab)
    end
  ensure
    symtab.pop
  end
end