Class: Hind::SCIP::GlobalState

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/hind/scip/global_state.rb

Instance Method Summary collapse

Constructor Details

#initializeGlobalState

Returns a new instance of GlobalState.



11
12
13
# File 'lib/hind/scip/global_state.rb', line 11

def initialize
  reset
end

Instance Method Details

#add_ancestor(qualified_name, mixed_in_symbol) ⇒ Object



21
22
23
24
# File 'lib/hind/scip/global_state.rb', line 21

def add_ancestor(qualified_name, mixed_in_symbol)
  @ancestors[qualified_name] ||= []
  @ancestors[qualified_name] << mixed_in_symbol
end

#add_symbol(name, symbol) ⇒ Object



34
35
36
# File 'lib/hind/scip/global_state.rb', line 34

def add_symbol(name, symbol)
  @symbols[name] = symbol
end

#emitted?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/hind/scip/global_state.rb', line 30

def emitted?(symbol)
  @emitted_symbols.include?(symbol)
end

#find_symbol(name, current_scope) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hind/scip/global_state.rb', line 46

def find_symbol(name, current_scope)
  # Handle both constants (::) and methods (#)
  is_method = name.start_with?('#')
  sep = is_method ? '' : '::'

  # 1. Lexical Scope search
  qualified_name = current_scope.empty? ? name : "#{current_scope}#{sep}#{name}"
  qualified_name = name if current_scope.empty? && is_method
  return @symbols[qualified_name] if @symbols.key?(qualified_name)

  # 2. Ancestor chain search for current scope
  if !current_scope.empty?
    found = resolve_in_ancestors(name, current_scope, seen: Set.new)
    return found if found
  end

  # 3. Parent Lexical Scopes
  scope_parts = current_scope.split('::')
  while scope_parts.size > 0
    scope_parts.pop
    prefix = scope_parts.join('::')

    qualified_name = prefix.empty? ? name : "#{prefix}#{sep}#{name}"
    qualified_name = name if prefix.empty? && is_method
    return @symbols[qualified_name] if @symbols.key?(qualified_name)

    # Search ancestors of the parent scope too
    if !prefix.empty?
      found = resolve_in_ancestors(name, prefix, seen: Set.new)
      return found if found
    end
  end

  nil
end

#get_symbol(name) ⇒ Object



38
39
40
# File 'lib/hind/scip/global_state.rb', line 38

def get_symbol(name)
  @symbols[name]
end

#has_symbol?(name) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/hind/scip/global_state.rb', line 42

def has_symbol?(name)
  @symbols.key?(name)
end

#mark_emitted(symbol) ⇒ Object



26
27
28
# File 'lib/hind/scip/global_state.rb', line 26

def mark_emitted(symbol)
  @emitted_symbols.add(symbol)
end

#resetObject



15
16
17
18
19
# File 'lib/hind/scip/global_state.rb', line 15

def reset
  @symbols = {} # {qualified_name => symbol_string}
  @ancestors = {} # {qualified_name => [symbol_strings]}
  @emitted_symbols = Set.new
end