-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmy_symbol_table.py
187 lines (148 loc) · 4.79 KB
/
my_symbol_table.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from collections import OrderedDict
from decimal import Decimal
from enum import Enum
from my_ast import Type
from my_visitor import BuiltinFuncSymbol
from my_types import *
class Symbol(object):
def __init__(self, name, symbol_type=None):
self.name = name
self.type = symbol_type
class BuiltinTypeSymbol(Symbol):
def __init__(self, name):
super().__init__(name)
def __str__(self):
return self.name
__repr__ = __str__
ANY_BUILTIN = BuiltinTypeSymbol(ANY)
INT_BUILTIN = BuiltinTypeSymbol(INT)
DEC_BUILTIN = BuiltinTypeSymbol(DEC)
FLOAT_BUILTIN = BuiltinTypeSymbol(FLOAT)
COMPLEX_BUILTIN = BuiltinTypeSymbol(COMPLEX)
BOOL_BUILTIN = BuiltinTypeSymbol(BOOL)
BYTES_BUILTIN = BuiltinTypeSymbol(BYTES)
STR_BUILTIN = BuiltinTypeSymbol(STR)
ARRAY_BUILTIN = BuiltinTypeSymbol(ARRAY)
LIST_BUILTIN = BuiltinTypeSymbol(LIST)
DICT_BUILTIN = BuiltinTypeSymbol(DICT)
ENUM_BUILTIN = BuiltinTypeSymbol(ENUM)
FUNC_BUILTIN = BuiltinTypeSymbol(FUNC)
class VarSymbol(Symbol):
def __init__(self, name, var_type, read_only=False):
super().__init__(name, var_type)
self.accessed = False
self.val_assigned = False
self.read_only = read_only
def __str__(self):
return '<{name}:{type}>'.format(name=self.name, type=self.type)
__repr__ = __str__
class CollectionSymbol(Symbol):
def __init__(self, name, var_type, item_types):
super().__init__(name, var_type)
self.item_types = item_types
self.accessed = False
self.val_assigned = False
class FuncSymbol(Symbol):
def __init__(self, name, return_type, parameters, body):
super().__init__(name, return_type)
self.parameters = parameters
self.body = body
self.accessed = False
self.val_assigned = True
def __str__(self):
return '<{name}:{type} ({params})>'.format(name=self.name, type=self.type, params=', '.join('{}:{}'.format(key, value.value) for key, value in self.parameters.items()))
__repr__ = __str__
class TypeSymbol(Symbol):
def __init__(self, name, types):
super().__init__(name, types)
self.accessed = False
def __str__(self):
return '<{name}:{type}>'.format(name=self.name, type=self.type)
__repr__ = __str__
class SymbolTable(object):
def __init__(self):
self._scope = [OrderedDict()]
self._init_builtins()
def _init_builtins(self):
self.define(ANY_BUILTIN)
self.define(INT_BUILTIN)
self.define(DEC_BUILTIN)
self.define(FLOAT_BUILTIN)
self.define(COMPLEX_BUILTIN)
self.define(BOOL_BUILTIN)
self.define(BYTES_BUILTIN)
self.define(STR_BUILTIN)
self.define(ARRAY_BUILTIN)
self.define(LIST_BUILTIN)
self.define(DICT_BUILTIN)
self.define(ENUM_BUILTIN)
self.define(FUNC_BUILTIN)
def __str__(self):
s = 'Symbols: {}'.format(self.symbols)
return s
__repr__ = __str__
@property
def symbols(self):
return [value for scope in self._scope for value in scope.values()]
@property
def keys(self):
return [key for scope in self._scope for key in scope.keys()]
@property
def items(self):
return [(key, value) for scope in self._scope for key, value in scope.items()]
@property
def top_scope(self):
return self._scope[-1] if len(self._scope) >= 1 else None
@property
def second_scope(self):
return self._scope[-2] if len(self._scope) >= 2 else None
def search_scopes(self, name):
for scope in reversed(self._scope):
if name in scope:
return scope[name]
def new_scope(self):
self._scope.append(OrderedDict())
def drop_top_scope(self):
self._scope.pop()
@property
def unvisited_symbols(self):
return [sym_name for sym_name, sym_val in self.items if not isinstance(sym_val, (BuiltinTypeSymbol, BuiltinFuncSymbol)) and not sym_val.accessed]
def define(self, symbol, level=0):
level = (len(self._scope) - level) - 1
self._scope[level][symbol.name] = symbol
def lookup(self, name):
return self.search_scopes(name)
# def infer_type(self, value):
# if isinstance(value, BuiltinTypeSymbol):
# return value
# if isinstance(value, FuncSymbol):
# return self.lookup(FUNC)
# elif isinstance(value, VarSymbol):
# return value.type
# elif isinstance(value, Type):
# return self.lookup(value.value)
# else:
# if isinstance(value, int):
# return self.lookup(INT)
# elif isinstance(value, Decimal):
# return self.lookup(DEC)
# elif isinstance(value, float):
# return self.lookup(FLOAT)
# elif isinstance(value, complex):
# return self.lookup(COMPLEX)
# elif isinstance(value, str):
# return self.lookup(STR)
# elif isinstance(value, bool):
# return self.lookup(BOOL)
# elif isinstance(value, bytes):
# return self.lookup(BYTES)
# elif isinstance(value, list):
# return self.lookup(LIST)
# elif isinstance(value, dict):
# return self.lookup(DICT)
# elif isinstance(value, Enum):
# return self.lookup(ENUM)
# elif callable(value):
# return self.lookup(FUNC)
# else:
# raise TypeError('Type not recognized: {}'.format(value))