class SFRP::Mono::Node

Constants

NodeRef

Attributes

str[R]

Public Class Methods

new( str, type_str, node_refs, eval_func_str, init_func_str = nil ) click to toggle source
# File lib/sfrp/mono/node.rb, line 8
def initialize(
  str, type_str, node_refs, eval_func_str, init_func_str = nil
)
  @str = str
  @type_str = type_str
  @node_refs = node_refs
  @eval_func_str = eval_func_str
  @init_func_str = init_func_str
end

Public Instance Methods

==(other) click to toggle source
# File lib/sfrp/mono/node.rb, line 22
def ==(other)
  comp == other.comp
end
comp() click to toggle source
# File lib/sfrp/mono/node.rb, line 18
def comp
  [@str, @type_str, @node_refs, @eval_func_str, @init_func_str]
end
gen_evaluate_stmt(set, stmts) click to toggle source

Generate ststement to evaluate this node.

# File lib/sfrp/mono/node.rb, line 85
def gen_evaluate_stmt(set, stmts)
  arg_exps = @node_refs.map do |node_ref|
    n = set.node(node_ref.node_str)
    node_ref.last ? n.low_node_ref_last_str : n.low_node_ref_current_str
  end
  call_exp = set.func(@eval_func_str).low_call_exp(arg_exps)
  stmts << L.stmt("#{low_node_ref_current_str} = #{call_exp}")
end
gen_initialize_stmt(set, stmts) click to toggle source

Generate a statement of initialization if needed.

# File lib/sfrp/mono/node.rb, line 71
def gen_initialize_stmt(set, stmts)
  return unless initialized?
  call_exp = set.func(@init_func_str).low_call_exp([])
  stmts << L.stmt("#{low_node_str}[l] = #{call_exp}")
end
gen_node_var_declaration(set, stmts) click to toggle source

Generate declaration for variable to hold value of node.

# File lib/sfrp/mono/node.rb, line 78
def gen_node_var_declaration(set, stmts)
  type = set.type(@type_str)
  size = (initialized? ? '2' : '1')
  stmts << L.stmt("#{type.low_type_str} #{low_node_str}[#{size}]")
end
gen_node_var_mark_stmt(set, stmts) click to toggle source

Generate statement to mark node.

# File lib/sfrp/mono/node.rb, line 95
def gen_node_var_mark_stmt(set, stmts)
  return unless initialized?
  return unless set.type(@type_str).need_mark?(set)
  mark_func_str = set.type(@type_str).low_mark_func_str
  stmts << L.stmt("#{mark_func_str}(#{low_node_str}[l])")
end
initialized?() click to toggle source

Is this node initialized?

# File lib/sfrp/mono/node.rb, line 27
def initialized?
  @init_func_str
end
low_node_ref_current_str() click to toggle source

Return referrence name of current evaluated value of this node.

# File lib/sfrp/mono/node.rb, line 50
def low_node_ref_current_str
  index = (initialized? ? 'c' : '0')
  "#{low_node_str}[#{index}]"
end
low_node_ref_last_str() click to toggle source

Return referrence name of last evaluated value of this node.

# File lib/sfrp/mono/node.rb, line 56
def low_node_ref_last_str
  "#{low_node_str}[l]"
end
low_node_str() click to toggle source

Name of variable to hold current and last evaluated value of this node.

# File lib/sfrp/mono/node.rb, line 32
def low_node_str
  @str
end
memory_used_to_eval_node(set) click to toggle source
# File lib/sfrp/mono/node.rb, line 36
def memory_used_to_eval_node(set)
  set.func(@eval_func_str).memory(set)
end
memory_used_to_hold_node(set) click to toggle source
# File lib/sfrp/mono/node.rb, line 45
def memory_used_to_hold_node(set)
  set.type(@type_str).memory(set)
end
memory_used_to_init_node(set) click to toggle source
# File lib/sfrp/mono/node.rb, line 40
def memory_used_to_init_node(set)
  return Memory.empty unless initialized?
  set.func(@init_func_str).memory(set)
end
sorted_node_strs(set) click to toggle source

Return a list of nodes sorted by evaluation-order including this node. The list includes only nodes (recursively) depended by this node. So if you want to get a list including all nodes, you must call this method for an output node.

# File lib/sfrp/mono/node.rb, line 64
def sorted_node_strs(set)
  cur = current_referred_node_strs(set)
  last = last_referred_node_strs(set)
  cur + (last - cur)
end

Protected Instance Methods

current_referred_node_strs(set, visited = {}) click to toggle source

Return a list of (recursively) current-referred nodes including myself.

# File lib/sfrp/mono/node.rb, line 105
def current_referred_node_strs(set, visited = {})
  return [] if visited.key?(@str)
  visited[@str] = true
  prereq_node_strs = @node_refs.reject(&:last).flat_map do |r|
    set.node(r.node_str).current_referred_node_strs(set, visited)
  end
  prereq_node_strs + [@str]
end
last_referred_node_strs(set, visited = {}) click to toggle source

Return a list of (recursively) last-referred nodes.

# File lib/sfrp/mono/node.rb, line 115
def last_referred_node_strs(set, visited = {})
  return [] if visited.key?(@str)
  visited[@str] = true
  rec = @node_refs.reject(&:last).flat_map do |r|
    set.node(r.node_str).last_referred_node_strs(set, visited)
  end
  (@node_refs.select(&:last).map(&:node_str) + rec).uniq
end