Class: Idl::PostIncrementExpressionAst

Inherits:
AstNode
  • Object
show all
Includes:
Executable
Defined in:
lib/idlc/ast.rb,
lib/idlc/passes/prune.rb,
lib/idlc/passes/prune.rb,
lib/idlc/passes/gen_adoc.rb

Overview

represents a post-increment expression

for example:

i++

Constant Summary

Constants inherited from AstNode

AstNode::Bits1Type, AstNode::Bits32Type, AstNode::Bits64Type, AstNode::BoolType, AstNode::ConstBoolType, AstNode::PossiblyUnknownBits1Type, AstNode::PossiblyUnknownBits32Type, AstNode::PossiblyUnknownBits64Type, AstNode::ReachableFunctionCacheType, AstNode::StringType, AstNode::VoidType

Instance Attribute Summary

Attributes inherited from AstNode

#children, #input, #interval, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Executable

#executable?

Methods inherited from AstNode

#always_terminates?, #declaration?, #executable?, extract_base_var_name, #find_ancestor, #find_dst_registers, #find_referenced_csrs, #find_src_registers, #freeze_tree, #gen_option_adoc, #input_file, input_from_source_yaml, #inspect, #internal_error, interval_from_source_yaml, #lineno, #lines_around, #pass_find_return_values, #path, #print_ast, #reachable_exceptions, #reachable_functions, #set_input_file, #set_input_file_unless_already_set, #source_line_file_offsets, #source_starting_offset, #source_yaml, #starting_line, #text_value, #truncation_warn, #type_error, #unindent, value_else, #value_else, value_error, #value_error, value_try, #value_try, write_back_nested

Constructor Details

#initialize(input, interval, rval) ⇒ PostIncrementExpressionAst

Returns a new instance of PostIncrementExpressionAst.



5938
5939
5940
# File 'lib/idlc/ast.rb', line 5938

def initialize(input, interval, rval)
  super(input, interval, [rval])
end

Class Method Details

.from_h(yaml, source_mapper) ⇒ Object



5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
# File 'lib/idlc/ast.rb', line 5986

def self.from_h(yaml, source_mapper)
  raise "Bad YAML" unless yaml.key?("kind") && yaml.fetch("kind") == "post_increment_expr"

  input = input_from_source_yaml(yaml.fetch("source"), source_mapper)
  interval = interval_from_source_yaml(yaml.fetch("source"))
  PostIncrementExpressionAst.new(
    input, interval,
    AstNode.from_h(yaml.fetch("expr"), source_mapper)
  )
end

Instance Method Details

#const_eval?(symtab) ⇒ Boolean

Returns:

  • (Boolean)


5934
# File 'lib/idlc/ast.rb', line 5934

def const_eval?(symtab) = rval.const_eval?(symtab)

#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



5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
# File 'lib/idlc/ast.rb', line 5958

def execute(symtab)
  var = symtab.get(rval.text_value)

  value_result = value_try do
    internal_error "No symbol named '#{rval.text_value}'" if var.nil?

    value_error "#{rval.text_value} is not compile-time-known" if var.value.nil?

    var.value = var.value + 1
  end
  value_else(value_result) do
    var.value = nil
    value_error "#{rval.text_value} is not compile-time-known" if var.value.nil?
  end
end

#gen_adoc(indent, indent_spaces: 2) ⇒ Object



50
51
52
# File 'lib/idlc/passes/gen_adoc.rb', line 50

def gen_adoc(indent, indent_spaces: 2)
  "#{' ' * indent}#{rval.gen_adoc(indent, indent_spaces:)}++"
end

#nullify_assignments(symtab) ⇒ Object



193
194
195
196
# File 'lib/idlc/passes/prune.rb', line 193

def nullify_assignments(symtab)
  var = symtab.get(rval.text_value)
  var.value = nil unless var.nil?
end

#prune(symtab, forced_type: nil) ⇒ Object



961
962
963
964
965
966
967
968
# File 'lib/idlc/passes/prune.rb', line 961

def prune(symtab, forced_type: nil)
  new_ast = PostIncrementExpressionAst.new(input, interval, rval.dup)
  value_try do
    new_ast.execute(symtab)
  end
  # value_else: execute already sets nil on failure, nothing more to do
  new_ast
end

#rvalObject



5936
# File 'lib/idlc/ast.rb', line 5936

def rval = @children[0]

#to_hObject



5979
5980
5981
5982
5983
# File 'lib/idlc/ast.rb', line 5979

def to_h = {
  "kind" => "post_increment_expr",
  "expr" => rval.to_h,
  "source" => source_yaml
}

#to_idlObject



5976
# File 'lib/idlc/ast.rb', line 5976

def to_idl = "#{rval.to_idl}++"

#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

Parameters:

Returns:

  • (Type)

    The type of the node

Raises:



5953
5954
5955
# File 'lib/idlc/ast.rb', line 5953

def type(symtab)
  rval.type(symtab)
end

#type_check(symtab, strict:) ⇒ 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:



5943
5944
5945
5946
5947
5948
5949
5950
# File 'lib/idlc/ast.rb', line 5943

def type_check(symtab, strict:)
  rval.type_check(symtab, strict:)
  var = symtab.get(rval.text_value)
  rval_immutable =
    rval.is_a?(IdAst) && (rval.type(symtab).const? && !var.for_loop_iter?)
  type_error "Cannot increment a const variable" if rval_immutable
  type_error "Post increment variable must be integral" unless var.type.integral?
end