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

Methods inherited from Node

#to_s

Constructor Details

#initialize(name, args) ⇒ FuncCallNode

Returns a new instance of FuncCallNode.



65
66
67
68
# File 'lib/nodes/stmtnodes.rb', line 65

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

Instance Attribute Details

#argsObject

Returns the value of attribute args.



63
64
65
# File 'lib/nodes/stmtnodes.rb', line 63

def args
  @args
end

#nameObject

Returns the value of attribute name.



63
64
65
# File 'lib/nodes/stmtnodes.rb', line 63

def name
  @name
end

Instance Method Details

#evaluateObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/nodes/stmtnodes.rb', line 70

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

  return func_return_value
end