-
Notifications
You must be signed in to change notification settings - Fork 0
/
symtable.rb
144 lines (110 loc) · 2.31 KB
/
symtable.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#! /usr/bin/ruby
############################################################
# Universidad Simón Bolívar
# CI3175: Traductores e interpretadores
#
# Bitiondo
#
# Tabla de símbolos para Bitiondo
#
#
# David Cabeza 13-10191 <[email protected]>
# Fabiola Martínez 13-10838 <[email protected]>
############################################################
#-----------------------------------------------------------
# Simbolos para la tabla de símbolos
#-----------------------------------------------------------
class Sym
attr_reader :name, :type, :value, :size
def initialize(name, type, value, size)
@name = name
@type = type
@value = value
@size = size
end
def getName()
return @name
end
def getType()
return @type
end
def getValue()
return @value
end
def getSize()
if @size
return @size
end
return
end
def to_s
s = "Name: #{@name}, Type: #{@type}"
return s
end
end
#-----------------------------------------------------------
# Tabla de Símbolos
#-----------------------------------------------------------
class SymbolTable
def initialize(parentTable)
@symTable = Hash.new
@parentTable = parentTable
end
def insert(name, type, value, size)
if value
if size
s = Sym.new(name, type, value, size)
@symTable[name] = s
return true
end
s = Sym.new(name, type, value, nil)
@symTable[name] = s
return true
elsif not value
if size
s = Sym.new(name, type, nil, size)
@symTable[name] = s
return true
end
s = Sym.new(name, type, nil, nil)
@symTable[name] = s
return true
end
return false
end
def delete(name)
return @symTable.delete(name)
end
def update(name, type, value, size)
return insert(name, type, value, size)
end
def isMember(name)
return @symTable.has_key?(name)
end
# Returns element
def find(name)
if @symTable.has_key?(name)
return @symTable[name]
end
if @parentTable
return @parentTable.find(name)
end
# Raise exception and finaliza el programa!
return nil
end
# Returns true or false if element exists
def lookup(name)
if isMember(name)
return isMember(name)
end
if @parentTable
return @parentTable.lookup(name)
end
return false
end
def printSymTab(indent)
@symTable.each do |key, value|
puts "#{indent} #{value.to_s}"
end
end
end