Class: FuncCallNode

Inherits:
Node
  • Object
show all
Defined in:
lib/nodes/stmtnodes.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#value

Instance Method Summary collapse

Constructor Details

#initialize(name, args) ⇒ FuncCallNode

Returns a new instance of FuncCallNode.



105
106
107
108
# File 'lib/nodes/stmtnodes.rb', line 105

def initialize(name, args)
  @name = name
  @args = args
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



103
104
105
# File 'lib/nodes/stmtnodes.rb', line 103

def args
  @args
end

#nameObject

Returns the value of attribute name.



103
104
105
# File 'lib/nodes/stmtnodes.rb', line 103

def name
  @name
end

Instance Method Details

#create_tree_entryObject



118
119
120
121
122
# File 'lib/nodes/stmtnodes.rb', line 118

def create_tree_entry
  result = set_up_scope_header
  result += "Function #{@name} is called"
  TREE_ARRAY << result unless TREE_ARRAY[-1] == result
end

#evaluateObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/nodes/stmtnodes.rb', line 124

def evaluate
  func = ScopeManager.lookup_func(@name)

  function_body = func[0]
  function_param = func[1]

  ScopeManager.increment_scope_level

  if function_param.is_a?(ArrayNode)
    function_param.each do |val, index|
      function_param[index] = VariableDecNode.new(function_param[index].name, @args[index])
      function_param[index].evaluate
    end
  end

  func_return_value = function_body.evaluate

  ScopeManager.decrement_scope_level

  # If function return value is an "Assign" then we declare that variable in the global scope.
  # This should be a ScopeManager method, add_variable_to_global_scope is missing.
  old_scope_lvl = ScopeManager.scope_lvl
  if func_return_value.is_a?(VariableDecNode)
    ScopeManager.scope_lvl = 0
    func_return_value.evaluate
    ScopeManager.scope_lvl = old_scope_lvl
  end

  create_tree_entry if PRINT_TREE_FLAG
  return func_return_value
end

#to_sObject



110
111
112
113
114
115
116
# File 'lib/nodes/stmtnodes.rb', line 110

def to_s
  if @args == NilClass
    "#{name}()"
  else
    "#{name}(#{@args})"
  end
end