-
Notifications
You must be signed in to change notification settings - Fork 1
/
declarations.py
147 lines (115 loc) · 3.91 KB
/
declarations.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
class Escopo(object):
"""docstring for Escopo."""
names = {}
def __init__(self, name):
super(Escopo, self).__init__()
self.name=name
def add(self, value):
try: #if value is iterable
for element in value:
try:
self.names[element] #has a declaration with this name
print("Same Name Variavel Error "+ str(element))
return
except:
self.names[element]= value[element]
except:
try:
self.names[value.name] #has a declaration with this name
print("Same Name Variavel Error2" + str(element))
return
except:
self.names[value.name]= value
def show(self, d):
try:
return self.names[d].value
except:
for element in self.names:
try:
return self.names[element].escopo.show(d)
except:
continue
return None
def change(self, v, value):
self.names[v].value=value
def all(self):
return self.names
def __str__(self):
tmp=self.name+"\n"
tmp+="____________________________________________________________\n"
for element in self.names:
tmp+=str(self.names[element])+"\n"
return tmp
def name(self):
return str(self.names)
class Declaration(object):
"""docstring for Declaration."""
name=''
def __init__(self):
super(Declaration, self).__init__()
def __str__(self):
return self.name
def name(self):
return self.name
class Variable(Declaration):
"""Exclusive class for control variables"""
"""docstring for Variable."""
name=''
type=''
value=None
def __init__(self, name, type, value):
super(Variable, self).__init__()
self.name = name
self.type = type
self.value = value
self.def_type(type)
def def_type(self, type):
self.type=type
if(self.value==None):
if(type=="int"):
self.value=0
elif(type=="bool"):
self.value=False
elif(type=="string"):
self.value=''
def __str__(self):
return "Variable: "+self.name+ "\n\tType: " + str(self.type) + "\n\tValue: " + str(self.value)
def __repr__(self):
return "|"+self.name+ "," + str(self.type) + "," + str(self.value)+"|"
class Function(Declaration):
"""docstring for Function."""
def __init__(self, name, type, parametros, block):
super(Function, self).__init__()
self.name = name
self.type = type
self.parametros = parametros
self.block=block
def __str__(self):
tmp="Function: "+self.name+ " \n\tType:" + self.type + "\n\tparametros:"
tmp+=str(self.parametros)+ " \n\t" +str(self.block)
return tmp
def __repr__(self):
return "|"+self.name+ "," + self.type + "," + str(self.parametros)+"|"
class Procedure(object):
"""docstring for Procedure."""
def __init__(self, name, parametros, block):
super(Procedure, self).__init__()
self.name = name
self.parametros=parametros
self.block=block
def __str__(self):
return self.name+ " " +str(self.parametros)
def __repr__(self):
return "|"+self.name+ "," + str(self.parametros)+"|"
class Block(object):
"""docstring for Block."""
def __init__(self, pai, variaveis, statements):
super(Block, self).__init__()
self.pai=pai
tmp=None
self.escopo=tmp
self.statements=statements
def __str__(self):
return "Block "+self.pai+": "+str(self.escopo)+ " \n\tStatements:"+ str(self.statements)
def __repr__(self):
return "|"+self.escopo+ " \n\tStatements:\n"+ str(self.statements)+"|"