-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_logger.py
221 lines (194 loc) · 9.05 KB
/
type_logger.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import cmp.visitor as visitor
from AST import (
AssignNode, AtomicNode, AttrDeclarationNode, BinaryNode,
BlockNode, CallNode, CaseDeclarationNode,
ClassDeclarationNode, FuncDeclarationNode, HyphenNode, IfDeclarationNode,
InstantiateNode, IsVoidDeclarationNode, LetDeclarationNode, NotNode, ProgramNode,
VarDeclarationNode, WhileDeclarationNode)
from cmp.semantic import Context, Scope
class TypeLogger(object):
def __init__(self, context) -> None:
self.context:Context = context
@visitor.on('node')
def visit(self, node, scope, tabs):
pass
@visitor.when(ProgramNode)
def visit(self, node, scope:Scope, tabs=0):
ans = '\t' * tabs + f'\\__ProgramNode [<class> ... <class>]'
statements = '\n'.join(self.visit(child, scope.next_child(), tabs + 1) for child in node.declarations)
return f'{ans}\n{statements}'
@visitor.when(ClassDeclarationNode)
def visit(self, node, scope, tabs=0):
parent = '' if node.parent is None else f": {node.parent}"
attr_list = self.context.get_type(node.id).attributes
name_list = ["self"]
for attr in attr_list:
name_list.append(attr.name)
format_scope = defined_format(name_list, scope, tabs)
ans = '\t' * tabs + f'\\__ClassDeclarationNode: class {node.id} {parent} {{ <feature> ... <feature> }}'
ans += format_scope
features = '\n'.join(self.visit(child, scope, tabs + 1) for child in node.features)
return f'{ans}\n{features}'
@visitor.when(AttrDeclarationNode)
def visit(self, node, scope:Scope, tabs=0):
extra = computed_info(node)
ans = '\t' * tabs + f'\\__AttrDeclarationNode: {node.id} : {node.type}' + extra
if node.expr != None:
ans += f"\n{self.visit(node.expr, scope, tabs +1)}"
return f'{ans}'
@visitor.when(VarDeclarationNode)
def visit(self, node, scope, tabs=0):
extra = computed_info(node)
ans = '\t' * tabs + f'\\__VarDeclarationNode: {node.id} : {node.type} = <expr>' + extra
if node.expr != None:
ans += f'\n{self.visit(node.expr, scope, tabs + 1)}'
return f'{ans}'
@visitor.when(BlockNode)
def visit(self, node, scope, tabs=0):
extra = computed_info(node)
ans = '\t' * tabs + '\\__BlockNode: { <expr>; ... <expr>; }' + extra
body = '\n'.join(self.visit(child, scope, tabs + 1) for child in node.body)
return f'{ans}\n{body}'
@visitor.when(IfDeclarationNode)
def visit(self, node, scope, tabs=0):
ifexpr = self.visit(node.ifexpr, scope, tabs + 1)
thenexpr = self.visit(node.thenexpr, scope, tabs + 1)
elseexpr = self.visit(node.elseexpr, scope, tabs + 1)
extra = computed_info(node)
ans = '\t' * tabs + f'\\__IfDeclarationNode: if <expr> then <expr> else <expr>{extra}\n'
ifs = '\t' * (tabs + 1) + f'if:\n{ifexpr}\n'
ths = '\t' * (tabs + 1) + f'then:\n{thenexpr}\n'
els = '\t' * (tabs + 1) + f'else:\n{elseexpr}'
ans = ans + ifs + ths + els
return ans
@visitor.when(CaseDeclarationNode)
def visit(self, node, scope:Scope, tabs=0):
extra = computed_info(node)
header = '\t' * tabs + f'\\__CaseDeclarationNode: case <expr> of ( <var> => <expr> ...){extra}\n'
caseexpr = self.visit(node.expr, scope, tabs + 1)
case = '\t' * (tabs + 1) + f'case:\n{caseexpr}\n'
casevars = '\n'.join(self.visit(child, scope.next_child(), tabs + 1) for child in node.casevars)
of = '\t' * (tabs + 1) + f'of:\n{casevars}'
return header + case + of
@visitor.when(LetDeclarationNode)
def visit(self, node, scopex, tabs=0):
scope = scopex.next_child()
extra = computed_info(node)
header = '\t' * tabs + f'\\__LetDeclarationNode: Let (<var> <- <expr> ...) in <expr>{extra}\n'
name_list =[]
for name_var in node.letvars:
name_list.append(name_var.id)
format_scope =defined_format(name_list, scope, tabs)
letvars = '\n'.join(self.visit(child, scope, tabs + 1) for child in node.letvars)
expr = self.visit(node.expr, scope, tabs + 1)
let = '\t' * (tabs + 1) + f'let: \n{letvars}\n'
inx = '\t' * (tabs + 1) + f'in: \n{expr}'
return header + let + inx
@visitor.when(WhileDeclarationNode)
def visit(self, node, scope, tabs=0):
extra = computed_info(node)
header = '\t' * tabs + f'\\__WhileDeclarationNode: while <expr> loop ( <expr> ... <expr> ){extra}\n'
body = self.visit(node.bodyexpr, scope, tabs + 1)
whilex = self.visit(node.whileexpr, scope, tabs + 1) + '\n'
text1 = '\t' * (tabs + 1) + f'while:\n {whilex}'
text2 = '\t' * (tabs + 1) + f'loop:\n {body}'
return header + text1 + text2
@visitor.when(AssignNode)
def visit(self, node, scope, tabs=0):
extra = computed_info(node)
ans = '\t' * tabs + f'\\__AssignNode: {node.id} = <expr>' + extra
expr = self.visit(node.expr, scope, tabs + 1)
return f'{ans}\n{expr}'
@visitor.when(FuncDeclarationNode)
def visit(self, node, scopex, tabs=0):
scope = scopex.next_child()
extra = computed_info(node)
params = ', '.join(':'.join(param) for param in node.params)
ans = '\t' * tabs + f'\\__FuncDeclarationNode: {node.id}({params}) : {node.type}' + extra
name_list = []
for param in node.params:
name_list.append(param[0])
format_scope = defined_format(name_list, scope, tabs)
ans += format_scope
body = '\n' + self.visit(node.body, scope, tabs + 1)
return f'{ans}{body}'
@visitor.when(IsVoidDeclarationNode)
def visit(self, node, scope, tabs=0):
extra = computed_info(node)
ans1 = '\t' * tabs + f'\\__IsVoidNode: isvoid <expr>' + extra
ans2 = self.visit(node.lex, scope, tabs+1)
return ans1 + "\n" + ans2
@visitor.when(HyphenNode)
def visit(self, node, scope, tabs=0):
extra = computed_info(node)
ans1 = '\t' * tabs + f'\\__HyphenNode: ~ <expr>' + extra
ans2 = self.visit(node.lex, scope, tabs+1)
return ans1 + "\n" + ans2
@visitor.when(NotNode)
def visit(self, node, scope, tabs=0):
extra = computed_info(node)
ans1 = '\t' * tabs + f'\\__NotNode: not <expr>' + extra
ans2 = self.visit(node.lex, scope, tabs+1)
return ans1 + "\n" + ans2
@visitor.when(IsVoidDeclarationNode)
def visit(self, node, scope, tabs=0):
extra = computed_info(node)
ans1 = '\t' * tabs + f'\\__IsVoidNode: isvoid <expr>' + extra
ans2 = self.visit(node.lex, scope, tabs+1)
return ans1 + "\n" + ans2
@visitor.when(BinaryNode)
def visit(self, node, scope, tabs=0):
extra = computed_info(node)
ans = '\t' * tabs + f'\\__<expr> {node.__class__.__name__} <expr>' + extra
left = self.visit(node.left, scope, tabs + 1)
right = self.visit(node.right, scope, tabs + 1)
return f'{ans}\n{left}\n{right}'
@visitor.when(AtomicNode)
def visit(self, node, scope, tabs=0):
extra = computed_info(node)
return '\t' * tabs + f'\\__ {node.__class__.__name__}: {node.lex}' + extra
@visitor.when(CallNode)
def visit(self, node, scope, tabs=0):
extra = computed_info(node)
extra2 = ""
if node.obj:
extra2 = node.computed_obj_type.name + "."
ans = '\t' * tabs + f'\\__CallNode: {extra2}{node.id}(<expr>, ..., <expr>)' + extra
args = '\n'.join(self.visit(arg, scope, tabs + 1) for arg in node.args)
if len(node.args) > 0:
return f'{ans}\n{args}'
return f"{ans}"
@visitor.when(InstantiateNode)
def visit(self, node, scope, tabs=0):
return '\t' * tabs + f'\\__ InstantiateNode: new {node.lex}()'
def defined_format(name_list:list, scope:Scope, tabs=0):
if len(name_list) == 0:
return ""
header = '\n'+'\t' * tabs +f" Variables Defined:" + "\n" + "\t" * tabs + " | "
defined = str('\n' + '\t' * tabs+f" | ").join([defined_info(name, scope) for name in name_list])
end = '\n' + '\t' * tabs+f" -----------------------------------"
return header + defined + end
def defined_info(name:str, scope:Scope, only_local=True):
if only_local and not scope.is_local(name):
return f"<Variable \"{name}\" not defined locally>"
var = scope.find_variable(name)
if not only_local and not var:
return f"<Variable \"{name}\" not defined>"
try:
return f"{var.name}:{var.type.name}"
except AttributeError:
return f"<Error while accessing Variable \"{var.name}\" type: \"{var.type}\">"
def computed_info(node):
try:
if node.type != "AUTO_TYPE":
return ""
except AttributeError:
pass
try:
node.computed_type
try:
return f" -> {node.computed_type.name}"
except AttributeError:
return f" -> <Error While accessing Computed Type Name({node.computed_type})>"
except AttributeError:
return " -> <Not Computed>"