forked from JamesPane-Joyce/DCEC_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcec_container.py
executable file
·304 lines (290 loc) · 13.6 KB
/
dcec_container.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from __future__ import print_function
import pickle
from six import string_types
from six.moves import input
# We need to use the first type of import if running this script directly and the second type of
# import if we're using it in a package (such as for within Talos)
try:
import high_level_parsing
import prototypes
except ImportError:
import DCEC_Library.high_level_parsing as high_level_parsing
import DCEC_Library.prototypes as prototypes
class DCECContainer:
def __init__(self):
self.namespace = prototypes.Namespace()
self.statements = []
self.checkMap = {}
def save(self, filename):
"""
Saves a given container to file, saving both the prototypes (namespace) within the
container as well as the statements that have been added to it.
:param filename:
:return:
"""
namespace_out = open(filename + ".namespace", "w")
statements_out = open(filename + ".statements", "w")
pickle.dump(self.namespace, namespace_out)
pickle.dump(self.checkMap, statements_out)
def load(self, filename):
name_in = open(filename + ".namespace", "r")
state_in = open(filename + ".statements", "r")
namespace_in = pickle.load(name_in)
statements_in = pickle.load(state_in)
if isinstance(namespace_in, prototypes.Namespace):
self.namespace = statements_in
else:
return False
if isinstance(statements_in, dict):
self.statements = statements_in.keys()
self.checkMap = statements_in
def print_statement(self, statement, expression_type="S"):
if isinstance(statement, string_types):
return statement
if expression_type == "S":
temp = statement.create_s_expression()
elif expression_type == "F":
temp = statement.create_f_expression()
else:
print("ERROR: invalid notation type")
return False
for quant in self.namespace.quant_map.keys():
if 'QUANT' in quant:
temp = temp.replace(quant, self.namespace.quant_map[quant])
return temp
def add_statement(self, statement):
"""
Given a statement, attempts to parse the statement into the DCEC*. If there's an issue,
it'll print the issue and then return False, otherwise it'll return True and add the
statement to the Container instance
:param statement:
:return:
"""
add_atomics = {}
add_functions = {}
add_quants = {}
addee = statement
if isinstance(addee, string_types):
addee, add_quants, \
add_atomics, add_functions = high_level_parsing.tokenize_random_dcec(addee,
self.namespace)
if isinstance(addee, bool) and not addee:
print("ERROR: the statement " + str(statement) + " was not correctly formed.")
return False
elif addee == "":
return True
elif isinstance(addee, high_level_parsing.Token):
pass
else:
print("ERROR: the input " + str(statement) + " was not of the correct type.")
return False
for atomic in add_atomics.keys():
# Tokens are not currently stored
if isinstance(atomic, high_level_parsing.Token):
continue
for potentialtype in range(0, len(add_atomics[atomic])):
if (not self.namespace.no_conflict(add_atomics[atomic][0],
add_atomics[atomic][potentialtype], 0)[0]) and \
(not self.namespace.no_conflict(add_atomics[atomic][potentialtype],
add_atomics[atomic][0], 0)[0]):
print("ERROR: The atomic " + atomic + " cannot be both " +
add_atomics[atomic][potentialtype] + " and " + add_atomics[atomic][0] +
". (This is caused by assigning different sorts to two atomics inline. "
"Did you rely on the parser for sorting?)")
return False
for function in add_functions.keys():
for item in add_functions[function]:
if item[0] == "?":
print("ERROR: please define the returntype of the inline function " + function)
return False
else:
self.namespace.add_code_function(function, item[0], item[1])
for atomic in add_atomics.keys():
# Tokens are not currently stored
if isinstance(atomic, high_level_parsing.Token):
continue
elif atomic in self.namespace.atomics.keys():
if not self.namespace.no_conflict(self.namespace.atomics[atomic],
add_atomics[atomic][0], 0)[0] and \
not self.namespace.no_conflict(add_atomics[atomic][0],
self.namespace.atomics[atomic], 0)[0]:
print("ERROR: The atomic "+atomic+" cannot be both " + add_atomics[atomic][0] +
" and " + self.namespace.atomics[atomic] + ".")
return False
else:
self.namespace.add_code_atomic(atomic, add_atomics[atomic][0])
for quant in add_quants.keys():
if 'QUANT' in quant:
self.namespace.quant_map[quant] = add_quants[quant]
self.statements.append(addee)
if not isinstance(addee, string_types):
self.checkMap[addee.create_s_expression()] = addee
else:
self.checkMap[addee] = addee
return True
def sort_of(self, statement):
if isinstance(statement, string_types):
return self.namespace.atomics.get(statement)
if statement is None:
return None
if statement.function_name not in self.namespace.functions.keys():
return None
tmp_func = statement.function_name
tmp_args = statement.args
tmp_types = []
for arg in tmp_args:
tmp_types.append(self.sort_of(arg))
for x in self.namespace.functions[tmp_func]:
if len(x[1]) != len(tmp_types):
continue
else:
returner = True
for r in range(0, len(x[1])):
if not tmp_types[r] is None and self.namespace.no_conflict(tmp_types[r], x[1][r], 0)[0]:
continue
else:
returner = False
break
if returner:
return x[0]
else:
continue
return None
def sorts_of_params(self, statement):
sorts = []
if isinstance(statement, string_types):
return sorts
if statement is None:
return None
if statement.function_name not in self.namespace.functions.keys():
return None
tmp_func = statement.function_name
tmp_args = statement.args
tmp_types = []
for arg in tmp_args:
tmp_types.append(self.sort_of(arg))
for x in self.namespace.functions[tmp_func]:
if len(x[1]) != len(tmp_types):
continue
else:
returner = True
for r in range(0, len(x[1])):
if not tmp_types[r] is None and \
self.namespace.no_conflict(tmp_types[r], x[1][r], 0)[0]:
continue
else:
returner = False
break
if returner:
return x[1]
else:
continue
return None
def stupid_sort_define(self, sort, old_container):
if sort in self.namespace.sorts.keys():
return
else:
for x in old_container.namespace.sorts[sort]:
self.stupid_sort_define(x, old_container)
self.namespace.add_code_sort(sort, old_container.namespace.sorts[sort])
# TODO replace with iterator
def stupid_loop(self, token, functions, atomics, old_container):
if isinstance(token, string_types):
if old_container.sort_of(token) is None:
self.stupid_sort_define(atomics[token][0], old_container)
self.namespace.add_code_atomic(token, atomics[token][0])
else:
self.stupid_sort_define(old_container.sort_of(token), old_container)
self.namespace.add_code_atomic(token, old_container.sort_of(token))
else:
if token.function_name in ["forAll", "exists"]:
pass
elif old_container.sort_of(token) is None:
arg_types = []
for arg in token.args:
arg_types.append(atomics[arg][0])
if token in atomics.keys():
self.stupid_sort_define(atomics[token][0], old_container)
for arg in arg_types:
self.stupid_sort_define(arg, old_container)
poss = []
mapping = {}
for func in old_container.namespace.functions[token.function_name]:
deep = 0
compat, depth = old_container.namespace.no_conflict(func[0],
atomics[token][0], 0)
if not compat:
continue
else:
deep += depth
args = [atomics[arg][0] for arg in token.args]
if len(args) != len(func[1]):
continue
for y in range(0, len(func[1])):
compat, depth = old_container.namespace.no_conflict(args[y],
func[1][y], 0)
deep += depth
poss.append(deep)
mapping[deep] = func
final = mapping[min(poss)]
self.namespace.add_code_function(token.function_name, final[0], final[1])
else:
# This should never happen, but if it does make a new function
for x in functions[token.function_name]:
self.stupid_sort_define(x[0], old_container)
for y in x[1]:
self.stupid_sort_define(y, old_container)
self.namespace.add_code_function(token.function_name, x[0], x[1])
else:
self.stupid_sort_define(old_container.sort_of(token), old_container)
for x in old_container.sorts_of_params(token):
self.stupid_sort_define(x, old_container)
self.namespace.add_code_function(token.function_name, old_container.sort_of(token),
old_container.sorts_of_params(token))
for arg in token.args:
self.stupid_loop(arg, functions, atomics, old_container)
def tokenize(self, statement):
if not isinstance(statement, string_types):
return False
dcec_container = DCECContainer()
stuff = high_level_parsing.tokenize_random_dcec(statement, self.namespace)
if isinstance(stuff[0], bool) and not stuff[0]:
return False
elif stuff[0] == "":
return True
dcec_container.stupid_loop(stuff[0], stuff[3], stuff[2], self)
dcec_container.add_statement(statement)
return dcec_container
if __name__ == "__main__":
test = DCECContainer()
test.namespace.add_basic_dcec()
test.namespace.add_basic_logic()
test.namespace.add_text_function("Boolean help Object")
test.namespace.add_text_function("Boolean kind Agent")
test.namespace.add_text_function("Agent james")
#test.namespace.addTextFunction("Agent james")
#test.namespace.addTextFunction("Boolean hello Object")
#test.namespace.addTextFunction("Boolean equals Object Object")
#test.namespace.addTextAtomic("Boolean earth")
#test.namespace.addTextSort(raw_input("Enter a sort: "))
#test.namespace.addTextSort(raw_input("Enter a sort: "))
#test.namespace.addTextSort(raw_input("Enter a sort: "))
#test.tokenize(raw_input("Enter an expression: "))
test.add_statement(input("Enter an expression: "))
new = test.tokenize(input("Enter an expression: "))
#print new.statements[0].createSExpression(),new.namespace.atomics
#test.save("TEST")
print(test.statements, test.namespace.atomics, test.namespace.functions)
print(new.statements, new.namespace.atomics, new.namespace.functions)
#new.load("TEST")
#print len(test.statements)
for x in test.statements:
#print test.printStatement(x)
pass
#print test.namespace.atomics
#print test.namespace.functions
#print test.namespace.sorts
if len(test.statements) > 0:
#print test.sortsOfParams(test.statements[0])
#print test.sortOf(test.statements[0])
pass