Class: CSVPlusPlus::References

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_plus_plus/references.rb

Overview

References in an AST that need to be resolved

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReferences

Create an object with empty references. The caller will build them up as it depth-first-searches



46
47
48
49
# File 'lib/csv_plus_plus/references.rb', line 46

def initialize
  @functions = []
  @variables = []
end

Instance Attribute Details

#functionsArray<Entities::Function>

Functions references

Returns:



11
12
13
# File 'lib/csv_plus_plus/references.rb', line 11

def functions
  @functions
end

#variablesArray<Entities::Variable>

Variable references

Returns:



11
12
13
# File 'lib/csv_plus_plus/references.rb', line 11

def variables
  @variables
end

Class Method Details

.extract(ast, scope) ⇒ References

Extract references from an AST and return them in a new References object

Parameters:

  • ast (Entity)

    An Entity to do a depth first search on for references. Entities can be infinitely deep because they can contain other function calls as params to a function call

  • scope (Scope)

    The CodeSection containing all currently defined functions & variables

Returns:



21
22
23
24
25
26
27
28
29
30
# File 'lib/csv_plus_plus/references.rb', line 21

def self.extract(ast, scope)
  new.tap do |refs|
    ::CSVPlusPlus::Graph.depth_first_search(ast) do |node|
      next unless node.function_call? || node.variable?

      refs.functions << node if function_reference?(node, scope)
      refs.variables << node if node.variable?
    end
  end
end

Instance Method Details

#==(other) ⇒ boolean

Returns:

  • (boolean)


64
65
66
# File 'lib/csv_plus_plus/references.rb', line 64

def ==(other)
  @functions == other.functions && @variables == other.variables
end

#empty?boolean

Are there any references to be resolved?

Returns:

  • (boolean)


54
55
56
# File 'lib/csv_plus_plus/references.rb', line 54

def empty?
  @functions.empty? && @variables.empty?
end

#to_sString

Returns:

  • (String)


59
60
61
# File 'lib/csv_plus_plus/references.rb', line 59

def to_s
  "References(functions: #{@functions}, variables: #{@variables})"
end