Repository navigation
Expand file tree
/
Copy pathsparse.py
More file actions
executable file
·415 lines (367 loc) · 13.2 KB
/
Copy pathsparse.py
File metadata and controls
executable file
·415 lines (367 loc) · 13.2 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env python
'''
This is a Symbolic logic PARSEr (SPARSE)
'''
import platform
import traceback
import argparse
import sys
import dbg
colors = {'blue': '\033[31;1;34m',
'yellow': '\033[31;1;33m',
'green': '\033[31;1;32m',
'red': '\033[31;1m',
'purple':'\033[1;35m',
'cyan':'\033[1;36m',
'bold': '\033[0;1m',
'none': '\033[0m'}
def error(txt):
print(f"{colors['red']}ERROR: {txt}{colors['none']}")
def warn(txt):
print(f"{colors['yellow']}WARNING: {txt}{colors['none']}")
def pad(s, val, l, left=False):
while len(s) < l:
if left:
s = val + s
else:
s += val
return s
def by_two(ls):
rtnLs = []
i = 0
while i+1 <= len(ls)-1:
rtnLs.append([ls[i], ls[i+1]])
i += 1
return rtnLs
def split_not_in(s, val, startIgnore, endIgnore):
if s in [None, ""]:
return s
ignore = False
returnList = []
curSlice = ""
for i in s:
if i == val and not ignore:
returnList.append(curSlice)
curSlice = ""
else:
curSlice += i
if i == startIgnore:
ignore = True
elif i == endIgnore:
ignore = False
returnList.append(curSlice)
return returnList
def arg_sub(function, args):
if args[0] == "{":
args = args[1:]
if args[-1] == "}":
args = args[:-1]
args = split_not_in(args, ",", "{", "}")
functionBody = userFns[function].text
argNames = userFns[function].arguments
d.dbg(f"functionBody = {functionBody}; argNames = {argNames}")
subbedFunction = ""
for char in functionBody:
d.dbg(f"subbedFunction = {subbedFunction}")
d.dbg(f"Working on {char}")
if char in argNames:
d.dbg(f"argumnet, appending ({args[argNames.index(char)]})")
subbedFunction += args[argNames.index(char)]
else:
d.dbg(f"not argument")
subbedFunction += char
if subbedFunction[-1] == "}":
subbedFunction = subbedFunction[:-1]
d.dbg(f"returning {subbedFunction}", clr='yellow')
return "("+subbedFunction+")"
functionReplacements = {
"&":" and ",
"^":" and ",
"v":" or ",
"!":" not "
}
functionOutputReplacements = {
1:True,
0:False,
True:True,
False:False
}
argumentReplacements = {
"True":"True",
"False":"False",
"T":"True",
"F":"False",
"t":"True",
"f":"False",
"0":"0",
"1":"1"
}
possibleArguments = "ABCDEGHIJKLMNOPQRSUVWXYZabcdeghijklmnopqrsuwxyz"
userFns = {}
class Sentence:
global userFns
def __init__(self, name, body):
self.name = name
self.ogText = body.lstrip()
self.name = self.name.replace(" ", "")
self.text = gen_func_helper(body)
self.arguments = []
for c in self.text:
if c in possibleArguments and c not in self.arguments and c not in argumentReplacements:
self.arguments.append(c)
self.arguments = sorted(self.arguments)
formattedInput = ""
for c in self.text:
if c in functionReplacements:
formattedInput += functionReplacements[c]
elif c in argumentReplacements:
formattedInput += argumentReplacements[c]
else:
formattedInput += c
self.lambdaFormat = f"f = lambda {','.join(self.arguments)}: {formattedInput}"
if VERBOSE:
print(f"Defining '{self.name}' as '{self.lambdaFormat}'.")
t = {}
exec(self.lambdaFormat, t)
for i in t:
self.fn = t[i]
userFns[self.name] = self
def gen_table(self):
truthDict = {}
curArgs = [False]*len(self.arguments)
i = 0
while True:
nStr = pad(str(bin(i))[2:], "0", len(self.arguments), left=True)
for idx,c in enumerate(nStr):
if c == "1":
curArgs[idx] = True
elif c == "0":
curArgs[idx] = False
truthDict[nStr] = self.run(curArgs)
if curArgs == [True]*len(self.arguments):
break
i += 1
return truthDict
def table(self):
truthDict = self.gen_table()
sec1, sec2 = f" {','.join(self.arguments)} ", f" ({self.name}) {self.ogText} "
header = sec1 + "|" + sec2
sectionLens = [len(sec1), len(sec2)]
header = "|" + header + "|"
separator = '+'+'-'*(sectionLens[0])+'+'+'-'*(sectionLens[1])+'+'
print(separator)
print(header)
print(separator)
r = {"0":"F","1":"T"}
for i in truthDict:
out = "| "
for c in i:
out += r[c]+","
out = out[:-1]
out = pad(out," ",sectionLens[0]+1)
out += "| "
out += f"{truthDict[i]}"
out = pad(out, " ", sectionLens[0]+sectionLens[1]+2) + "|"
print(out)
print(separator)
def run(self, args):
for idx,a in enumerate(args):
if a in argumentReplacements:
args[idx] = argumentReplacements[a]
d.dbg(f"Running {self.lambdaFormat} with {args}")
result = self.fn(*args)
if result in functionOutputReplacements:
return functionOutputReplacements[result]
else:
error(f"The function {self.text} gave bad output '{result}'.")
def print_func(self):
cols = [["Function Name"],["User Definition"],["Fully Substituted Definition"],["Lambda Function Definition"]]
for i in range(len(cols)):
cols[i].append("-"*len(cols[i][0]))
cols[0].append(self.name)
cols[1].append(self.ogText)
cols[2].append(self.text)
cols[3].append(self.lambdaFormat)
longests = [0]*len(cols)
for i in range(len(cols)):
for e in cols[i]:
if len(e) > longests[i]:
longests[i] = len(e)
print()
for i in range(len(cols[0])):
for idx in range(len(cols)):
print(pad(cols[idx][i], " ", longests[idx]+1),end='\t')
print()
print()
def __eq__(self,other):
return (self.gen_table() == other.gen_table())
def list_funcs():
out = []
for func in userFns:
out.append(f"{userFns[func].name}")
if out == []:
return
longest = 0
for func in out:
if len(func) > longest:
longest = len(func)
print()
for idx,func in enumerate(out):
out[idx] = pad(func, " ", longest)
out[idx] += f" : {userFns[func].ogText}"
print(out[idx])
print()
def print_table(fToTable):
if fToTable in userFns:
t = userFns[fToTable]
else:
t = Sentence("anon",fToTable)
t.table()
def load(file):
print(f"Loading file '{file}'...")
try:
with open(file, "r") as f:
wholeFile = f.readlines()
for ln in wholeFile:
parse(ln)
print("Done!")
except Exception as e:
error(f"failed to load file with exception {e}")
def assign(name,text):
t = Sentence(name, text)
userFns[t.name] = t
def equivalent(fns):
for idx,f in enumerate(fns):
fns[idx] = Sentence("anon",f)
result = True
for c in by_two(fns):
if c[0] != c[1]:
result = False
break
print(result)
def clear_screen():
print("\033[2J\033[H", end="", flush=True)
def display_help():
cols = [["COMMAND"],["DESCRIPTION"],["ARGUMENTS"]]
for i in range(len(cols)):
cols[i].append("-"*len(cols[i][0]))
for c in commandHelps:
cols[0].append(c)
cols[1].append(commandHelps[c][0])
cols[2].append(commandHelps[c][1])
longests = [0,0,0]
for i in range(len(cols)):
for e in cols[i]:
if len(e) > longests[i]:
longests[i] = len(e)
print()
for i in range(len(cols[0])):
for idx in range(len(cols)):
print(pad(cols[idx][i], " ", longests[idx]+1),end='\t')
print()
print()
def gen_func_helper(i):
parsedString = i
if "{" in i:
for fn in userFns:
start = 0
end = start + len(fn)
while end < len(i):
end = start + len(fn)
d.dbg(f"fn = {fn} ; chunk = {parsedString[start:end]}")
if parsedString[start:end] == fn:
if parsedString[end] == "{":
d.dbg("made it here")
# we have arguments
argsEnd = end+1
argString = "{"
depth = 1
while depth >= 1 and argsEnd < len(parsedString):
if parsedString[argsEnd] == "{":
depth += 1
elif parsedString[argsEnd] == "}":
depth -= 1
d.dbg(f"added {i[argsEnd]} to argString")
argString += parsedString[argsEnd]
argsEnd += 1
d.dbg(f"substituting with fn = {fn}, argString = {argString}")
substitutedFunction = arg_sub(fn, argString)
parsedString = parsedString[:start] + substitutedFunction + parsedString[argsEnd:]
d.dbg(f"parsedString = {parsedString}",clr='yellow')
d.dbg(f"substituedFunction = {substitutedFunction}")
return gen_func_helper(parsedString)
elif parsedString[end] in functionReplacements:
# there are no arguments so just append the functions whole body
parsedString = parsedString[:start] + userFns[fn].text + parsedString[end:]
return gen_func_helper(parsedString)
start += 1
else:
for fn in userFns:
if fn in i:
i = i.replace(fn, "("+userFns[fn].text+")")
return gen_func_helper(i)
return i
commandHelps = {
"help":["Display this help menu.","none"],
"cls, clear":["Clears screen.","none"],
"list":["Lists defined function names and bodies.","none"],
"table":["Displays truth table for given function.","1 (function to table)"],
"load":["Execute every line in a given file like it had been typed into SPARSE.","1 (file to load)"],
"print":["Prints function body and fully substituted definition for function.","1 (function to print)"]
}
commands = {
"help":lambda i : display_help(),
"cls":lambda i : clear_screen(),
"clear":lambda i : clear_screen(),
"list":lambda i : list_funcs(),
"table":lambda i : error("'table' takes a function to table.") if len(i.split(" ")) != 2 else print_table(i.split(" ")[1]),
"load":lambda i : error("'load' takes a file to load") if len(i.split(" ")) != 2 else load(i.split(" ")[1]),
"print":lambda i : error("'print' takes a function to print") if len(i.split(" ")) != 2 else userFns[i.split(" ")[1]].print_func(),
"==":lambda i : error("you must have two or more functions for comparison") if len(i.split("==")) < 2 else equivalent([ f.replace(" ","") for f in i.split("==")]),
"=":lambda i : error("you must have a name and a body for a function definition") if (len([a for a in i.split("=") if a.replace(" ", "") != ""]) != 2) else assign(i.split("=")[0].replace(" ",""),i.split("=")[1]),
"{":lambda i : print(Sentence("anon",i).run([])),
"exit":lambda i : (print("Bye!"),exit())
}
def parse(i):
i = i.strip()
if i == "":
return
try:
for c in commands:
if c in i:
commands[c](i)
return
error(f"SPARSE couldn't figure out what you wanted")
except Exception as e:
error(f"There was an exception '{e}' during execution.")
if ARGS.devmode:
traceback.print_exc()
if platform.system() == "Linux" or platform.system() == "Darwin":
import readline
VERSION = 2.0
VERBOSE = False
DEV_MODE = False
parser = argparse.ArgumentParser(description="Symbolic logic PARSEr (SPARSE) version 1")
parser.add_argument("--devmode", action="store_true", help="Turn on developer mode.")
parser.add_argument("--verbose", action="store_true", help="Be more verbose about what SPARSE is doing.")
parser.add_argument("-f", "--file", type=str, help="Run SPARSE on a file then quit.")
ARGS = parser.parse_args()
if __name__ == "__main__":
VERBOSE, DEV_MODE = ARGS.verbose, ARGS.devmode
if DEV_MODE:
VERBOSE = True
if not VERBOSE:
d = dbg.debug(args=" : -10 sps=t suppressWarnings=t suppressInfo=t")
else:
d = dbg.debug(args=sys.argv)
if ARGS.file != None:
load(ARGS.file)
else:
print(f"Welcome to SPARSE (Symbolic logic PARSEr) version {VERSION}. Type 'help' for a list of commands and uses.")
print("Read the README.md for explanation of syntax and usage. Enjoy!")
print("AUTHOR: James Burkett (Xtreme Software Developers) ; (xtremesoftwaredev@gmail.com)")
while True:
i = input("sparse> ")
parse(i)
else:
warn("Not running as main")