-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathreference.py
More file actions
executable file
·212 lines (144 loc) · 5.23 KB
/
reference.py
File metadata and controls
executable file
·212 lines (144 loc) · 5.23 KB
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
"""
Each node has a set of attributes that allows for quick access to properties and
other node of interest. For example, if `node` has a name, it can be referred
to by `node.name`. Another example is to access the parent node by
`node.parent`.
Note that, if a reference does not exist, the node itself will be returned.
"""
groups = [
"Assign", "Assigns", "Branch", "For", "Func", "Main",
"Set", "Cset", "Fset", "Nset", "Sset",
"Get", "Cget", "Fget", "Nget", "Sget",
"Statement", "Switch", "Tryblock", "Matrix",
"While", "Block", "Node", "Transpose", "Ctranspose",
]
nondeclares = ("Program", "Project", "Include", "Includes", "Struct", "Structs")
structvars = ("Fvar", "Fget", "Fset", "Nget", "Nset", "Sget", "Sset")
class Property_reference(object):
"general property node"
def __init__(self, name, default=None):
self.name = name
def __get__(self, instance, owner):
return instance.prop[self.name]
def __set__(self, instance, value):
instance.prop[self.name] = value
class Recursive_property_reference(object):
"recursive property node"
def __init__(self, name):
self.name = name
def __get__(self, instance, owner):
a = instance.prop[self.name]
if not (a is None):
return a
assert not (instance is instance.parent)
a = Recursive_property_reference.__get__(self, instance.parent, owner)
instance.prop[self.name] = a
return a
def __set__(self, instance, value):
instance.prop[self.name] = value
class File_reference(object):
def __get__(self, instance, owner):
if hasattr(instance, "_file"):
return instance._file
if instance.cls == "Program":
file_name = instance.name
else:
file_name = instance.program.name
instance._file = file_name
return file_name
class Line_reference(object):
def __get__(self, instance, owner):
if hasattr(instance, "_line"):
return instance._line
if instance.cls == "Project":
line = 0
elif instance.cls == "Funcs":
line = 1
else:
pline = instance.parent.line
pcur = instance.parent.cur
cur = instance.cur
if pcur == cur:
line = pline
else:
line = pline + instance.program.code.count("\n", pcur, cur)
instance._line = line
return line
class Group_reference(object):
def __get__(self, instance, owner):
if hasattr(instance, "_group"):
return instance._group
if instance.parent.cls in groups:
group = instance.parent
else:
group = instance.parent.group
instance._group = group
return group
class Func_reference(object):
def __get__(self, instance, owner):
if hasattr(instance, "_func"):
return instance._func
if instance.cls in ("Func", "Main", "Program"):
func = instance
else:
func = instance.parent.func
instance._func = func
return func
class Program_reference(object):
def __get__(self, instance, owner):
if hasattr(instance, "_program"):
return instance._program
if instance.cls == "Program":
program = instance
else:
program = instance.parent.program
instance._program = program
return program
class Project_reference(object):
def __get__(self, instance, owner):
if hasattr(instance, "_project"):
return instance._project
if instance.cls == "Project":
project = instance
else:
project = instance.parent.project
instance._project = project
return project
class Names(object):
def __get__(self, instance, owner):
return [i.prop["name"] for i in instance.children]
class Declare_reference(object):
def __get__(self, instance, owner):
if hasattr(instance, "_declare"):
return instance._declare
if instance.cls in nondeclares:
return instance
if instance.cls in structvars or\
instance.backend in ("structs", "struct"):
if instance.cls in ("Nget", "Nset"):
if instance[0].cls == "String":
value = instance[0]["value"]
else:
return instance
else:
value = instance.value
if instance not in instance.program[3]:
return instance
struct = instance.program[3][instance]
if value not in struct.names:
return instance
out = struct[struct.names.index(value)]
instance._declare = out
return out
elif instance.parent.cls in "Struct":
return instance
else:
if instance in instance.func[0]:
out = instance.func[0][instance]
instance._declare = out
return out
if instance in instance.func[2]:
out = instance.func[2][instance]
instance._declare = out
return out
return instance