Class: TlaParserS::SymbolTable

Inherits:
Object
  • Object
show all
Includes:
Utils::MyLogger
Defined in:
lib/semantics/symbol_table.rb

Constant Summary collapse

PROGNAME =

Logger

nil

Constants included from Utils::MyLogger

Utils::MyLogger::LOGFILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::MyLogger

#getLogger, #logfile

Constructor Details

#initialize(options = {}) ⇒ SymbolTable


constructore



18
19
20
21
22
23
24
# File 'lib/semantics/symbol_table.rb', line 18

def initialize( options={} )

  @logger = getLogger( PROGNAME, options )
  @logger.info( "#{__method__} initialized symbol-table" )
  
  @stack = []
end

Instance Attribute Details

#stackObject (readonly)

Array as symbol table context



7
8
9
# File 'lib/semantics/symbol_table.rb', line 7

def stack
  @stack
end

Instance Method Details

#addContext(tree_node, moduleName = nil) ⇒ Object

‘symbol_definitions` - method returning hash entries (:value,:node_type)

Parameters:

  • tree_node (SyntaxNode)

    node implementing



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/semantics/symbol_table.rb', line 49

def addContext( tree_node, moduleName=nil )

  if tree_node && tree_node.respond_to?(:symbol_definitions)
    tree_node.symbol_definitions.each do |symbol_definition|
      @logger.debug( "#{__method__} moduleName #{moduleName}, symbol_definition=#{symbol_definition}" ) if @logger.debug?
      entry = {
        :context_type => tree_node.node_type,            
        :context => tree_node.name,
        :symbol_type => symbol_definition[:node_type],
        :symbol_name => symbol_definition[:value],
      }
      # optionally add moduleName
      entry[:module] = moduleName if moduleName

      # output before tree
      @logger.debug( "#{__method__} @#{stack.length} entry=#{entry}, parse tree=#{symbol_definition[:tree] ? 'given' : 'not given' } " ) if @logger.debug?

      # syntax tree defined only on procedure & macros (and variable defs)
      entry[:tree] = symbol_definition[:tree] if symbol_definition[:tree]

      addEntry( entry )
    end
  end
end

#addEntry(entry) ⇒ Object

Add a hash (with :symbol_type, :context_type, :context, :symbol_name) to ‘currentContext’

Parameters:

  • entry (Hash)

    add hash to ‘currentContext’



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/semantics/symbol_table.rb', line 34

def addEntry( entry )
  # get refeence to current contex
  # do not ouput :tree property on info stack
  @logger.info "#{__method__} @[#{@stack.length}] #{entry.inject({}){|e,(k,v)| e[k] = ( k == :tree ? true : v ); e } }"
  @logger.debug "#{__method__} tree = #{entry[:tree].inspect}"  if @logger.debug?
  # @logger.debug "#{__method__} @[#{@stack.length}] #{entry}"

  context = currentContext
  context[entry[:symbol_name]] = entry
  
end

#contextLevelsObject



167
168
169
# File 'lib/semantics/symbol_table.rb', line 167

def contextLevels
  0
end

#currentContextObject



155
156
157
# File 'lib/semantics/symbol_table.rb', line 155

def currentContext
  return stack.last
end

#depthObject



26
27
28
# File 'lib/semantics/symbol_table.rb', line 26

def depth
  @stack.length
end

#dumpContext(&blk) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/semantics/symbol_table.rb', line 159

def dumpContext( &blk )
  stack.reverse_each.with_index do |context, i|
    yield i, context if blk
    @logger.debug "Context #{i}" if @logger.debug?
    @logger.debug context.to_yaml if @logger.debug?
  end
end

#entries(level = 1) ⇒ String:Array

Returns of entry names.

Parameters:

  • level (int) (defaults to: 1)

    number of levels to recurse (default 1)

Returns:

  • (String:Array)

    of entry names



98
99
100
101
102
103
104
105
106
107
# File 'lib/semantics/symbol_table.rb', line 98

def entries( level=1)
  entries = []
  stack.reverse_each do |context|
    # add key to entries unless it is already there
    context.keys().each {  |k| entries << k unless entries.include?( k ) }
    level -= 1
    break if level <= 0
  end
  entries
end

#popContextObject



86
87
88
89
90
91
92
93
# File 'lib/semantics/symbol_table.rb', line 86

def popContext
  if stack.length == 0 then
    msg = "Stack underflow"
    @logger.error( "#{__method__} #{msg}" )        
    raise SymbolTableException.new msg
  end
  @stack.pop
end

#pushContext(tree_node, moduleName = nil) ⇒ Object

Create new context: add one more level to symbol table, add entries from ‘tree_node.symbol_definitions’

Parameters:

  • tree_node (SyntaxNode)

    define initial context content



78
79
80
81
82
83
84
# File 'lib/semantics/symbol_table.rb', line 78

def pushContext( tree_node, moduleName=nil )
  
  context = {}
  @stack.push( context )
  
  addContext( tree_node, moduleName )
end

#resolveContext(identifier) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/semantics/symbol_table.rb', line 121

def resolveContext( identifier )
  stack.reverse_each do |context|
    # @logger.debug "context=#{context}, identifier=#{identifier}"
    return context[identifier] if context.has_key?( identifier ) 
  end
  return nil
end

#resolveModule(moduleName) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/semantics/symbol_table.rb', line 142

def resolveModule( moduleName )
  entries = []
  stack.reverse_each do |context|
    context.each do |symbol,entry|
      @logger.debug( "#{__method__} symbol=#{symbol}, entry=#{entry[:module]}" )  if @logger.debug?
      # entries << entry if entry[:module] == moduleName 
      entries << entry if !entry[:module].nil? && entry[:module].end_with?( moduleName )
    end
  end
  @logger.debug( "#{__method__} entries-->#{entries.join(',')}" ) if @logger.debug?
  return entries
end

#symbolsHash:Array

Returns of symbol table entries.

Returns:

  • (Hash:Array)

    of symbol table entries



110
111
112
113
114
115
116
117
# File 'lib/semantics/symbol_table.rb', line 110

def symbols
  entries = {}
  stack.reverse_each do |context|
    # add key to entries unless it is already there
    context.keys().each { |k|  entries[k] = context[k] }
  end
  entries
end