-
Notifications
You must be signed in to change notification settings - Fork 0
/
axioms.py
671 lines (512 loc) · 18.2 KB
/
axioms.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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
import inspect
import re
import PNLI # Peter Norvig's Lisp Interpreter
import _tkinter
from Tkinter import *
from tkSimpleDialog import askstring
from tkFileDialog import asksaveasfilename
global ifsToClose
global identifierSet
global functionSet
ifsToClose = 0
identifierSet = []
functionSet = []
def parse(rbp=0):
global token
t = token
token = next()
left = t.nud()
while rbp < token.lbp:
t = token
token = next()
left = t.led(left)
return left
class functionBuilderToken:
lbp = 5
def nud(self):
self.string = ""
global token
global ifsToClose
token = next() # advances past identifier
token = next() # advances past :
if not isinstance(token, functionBuilderToken): # if there is only one bound identifier
self.string = self.string + str(parse()) + " " #stops at comma
token = next()#advances past comma
while isinstance(token, functionBuilderToken):
while isinstance(token, functionBuilderToken): # advances past one lines All x:
token = next() # advances past All
token = next() # advances past identifier
token = next() # advances past :
self.string = self.string + str(parse()) + " " #stops at comma
token = next()#advances past comma
else:
self.string = self.string +')'*ifsToClose
token = next() # advances past >
return self
def __repr__(self):
return self.string
class functionDefinitionToken:
lbp = 10
def __init__(self, id):
self.id = id
def nud(self):
global token
global functionSet
global ifsToClose
ifsToClose = 0
self.string = "(define ("+ self.id
while not isinstance(token, operatorCloseParenToken):
self.string = self.string + " "+ str(parse())
if isinstance(token, symbolCommaToken):
token = next() # advances past comma
token = next() # advances past )
self.string = self.string + ")"
functionSet = []
functionSet.append(self.string[8:]) # ignores the (define
token = next() # advances past comma
self.string = self.string + " " + str(parse()) + ")"
return self
def __repr__(self):
return self.string
class functionCallToken:
lbp = 10
def __init__(self, id):
self.id = id
def nud(self):
global token
self.string = "(" + self.id
while not isinstance(token, operatorCloseParenToken):
self.string = self.string + " "+ str(parse())
if isinstance(token, symbolCommaToken):
token = next() # advances past comma
token = next() # advances past )
self.string = self.string + ")"
if self.string in functionSet:
if isinstance(token, symbolCommaToken):
self.string = "#t"
if isinstance(token, operatorEqualToken):
token = next() #advances past =
self.string = str(parse())
if isinstance(token, operatorIfAndOnlyIfToken):
token = next() # advances past <->
self.string = str(parse())
return self
def __repr__(self):
return self.string
class functionPrefixHeadToken:
lbp = 10
def nud(self):
global token
self.string = "(car "+ str(parse()) + ")"
token = next() # advances past )
return self
def __repr__(self):
return self.string
class functionPrefixTailToken:
lbp = 10
def nud(self):
global token
self.string = "(cdr "+ str(parse()) + ")"
token = next() # advances past )
return self
def __repr__(self):
return self.string
class functionPrefixConsToken:
lbp = 10
def nud(self):
global token
self.string = "(cons " + str(parse()) + " "
token = next() # advances past ,
self.string = self.string + str(parse()) + ")"
token = next() # advances past )
return self
def __repr__(self):
return self.string
class expressionPrefixLambdaToken:
lbp = 10
def nud(self):
global token
self.identifiers = []
self.args = []
self.string = "((lambda (" + token.symbol()
token = next() # advances past first argument
while str(token) != ":":
if str(token) == ",":
token = next()
self.string = self.string + " " + token.symbol()
token = next()
token = next() #advances past :
self.string = self.string + ") " + str(parse()) + ") " #parse() should go untill it hits the comma
token = next() #advances past comma
while str(token) != "end" and str(token) != ")" :
if str(token) == ",":
token = next() #advances past comma if there is one
self.string = self.string + " " + str(parse())
self.string = self.string + ")"
return self
def __repr__(self):
return self.string
class expressionPrefixIfToken:
lbp = 10
def nud(self):
global token
self.string = "(if " + str(parse())
token = next() # advances past comma
self.string = self.string + " " + str(parse())
token= next() # advances past comma
token = next() #advances past else
self.string = self.string + " " + str(parse()) + ")"
return self
def __repr__(self):
return self.string
class identifierToken:
def __init__(self, value):
self.value = value
def nud(self):
return self
def symbol(self):
return str(self.value)
def __repr__(self):
return "%s" % self.value
class integerToken:
def __init__(self, value):
self.value = value
def nud(self):
return self
def symbol(self):
return str(self.value)
def __repr__(self):
return "%s" % self.value
class operatorImpliesToken:
lbp = 20
def led(self, left):
global ifsToClose
ifsToClose += 1
self.first = left
self.second = parse(19)
return self
def __repr__(self):
return "(if %s %s" % (self.first, self.second)
class operatorIfAndOnlyIfToken:
lbp = 20
def led(self, left):
self.first = left
self.second = parse(19)
return self
def __repr__(self):
return "(this made it here hmm %s %s)" % (self.first, self.second)
class operatorOrToken:
lbp = 30
def led(self, left):
self.first = left
self.second = parse(30)
return self
def __repr__(self):
return "((lambda (x y) (if x #t y)) %s %s)" % (self.first, self.second)
class operatorAndToken:
lbp = 30
def led(self, left):
self.first = left
self.second = parse(30)
return self
def __repr__(self):
return "((lambda (x y) (if x y #f)) %s %s)" % (self.first, self.second)
class operatorNotToken:
lbp = 31
def nud(self):
self.first = parse(31)
return self
def led(self, left):
self.fisrt = parse(31)
return self
def __repr__(self):
return "(not %s)" % (self.first)
class operatorAssignToken:
lbp = 40
def led(self, left):
self.first = left
self.second = parse(40)
return self
def symbol(self):
return ":="
def __repr__(self):
global identifierSet
if str(self.first) in identifierSet:
return "(set! %s %s)" % (self.first, self.second)
identifierSet.append(str(self.first))
return "(define " + str(self.first) + " 0) (set! %s %s)" % (self.first, self.second)
class operatorEqualToken:
lbp = 40
def led(self, left):
self.first = left
self.second = parse(40)
return self
def __repr__(self):
return "(equal? %s %s)" % (self.first, self.second)
class operatorLessThanToken:
lbp = 40
def led(self, left):
self.first = left
self.second = parse(40)
return self
def __repr__(self):
return "(< %s %s)" % (self.first, self.second)
class operatorGreaterThanToken:
lbp = 40
def led(self, left):
self.first = left
self.second = parse(40)
return self
def __repr__(self):
return "(> %s %s)" % (self.first, self.second)
class operatorLessThanEqualToken:
lbp = 40
def led(self, left):
self.first = left
self.second = parse(40)
return self
def __repr__(self):
return "(<= %s %s)" % (self.first, self.second)
class operatorGreaterThanEqualToken:
lbp = 40
def led(self, left):
self.first = left
self.second = parse(40)
return self
def __repr__(self):
return "(>= %s %s)" % (self.first, self.second)
class operatorAddToken:
lbp = 50
def nud(self): #if function is unary
self.first = parse(70)
self.second = 0
return self
def led(self, left):
self.first = left
self.second = parse(50)
return self
def __repr__(self):
return "(+ %s %s)" % (self.first, self.second)
class operatorMinToken:
lbp = 50
def nud(self): #if function is unary
self.first = 0
self.second = parse(70)
return self
def led(self, left):
self.first = left
self.second = parse(50)
return self
def __repr__(self):
return "(- %s %s)" % (self.first, self.second)
class listOperatorAppendToken:
lbp = 50
def led(self, left):
self.first = left
self.second = parse(50)
return self
def __repr__(self):
return "(append %s %s)" % (self.first, self.second)
class operatorMulToken:
lbp = 60
def led(self, left):
self.first = left
self.second = parse(60)
return self
def __repr__(self):
return "(* %s %s)" % (self.first, self.second)
class operatorModToken:
lbp = 60
def led(self, left):
self.first = left
self.second = parse(60)
return self
def __repr__(self):
return "(mod %s %s)" % (self.first, self.second)
class operatorExponToken:
lbp = 75
def led(self, left):
self.first = left
self.second = parse(74)
return self
def __repr__(self):
return "(^ %s %s)" % (self.first, self.second)
class operatorOpenParenToken:
lbp = 80
def nud(self):
expression = parse()
global token
token = next()
return expression
class operatorOpenBracketToken: #this token could be used to do whole expresions by using a comma
lbp = 80
def nud(self):
global token
string = "(list " + str(token)
token = next() #advances the past the first element
while str(token) != "]":
if str(token) == ",":
token = next()
string = string + " "+ str(token)
token = next()
token = next()
self.first = string
return self
def __repr__(self):
return "%s)" % (self.first)
class operatorCloseBracketToken():
lbp = 0
def nud(self):
return self
def __repr__(self):
return "]"
class operatorCloseParenToken():
lbp = 0
def nud(self):
return self
def __repr__(self):
return ")"
class symbolCommaToken():
lbp = 0
def nud(self):
return self
def __repr__(self):
return ","
class symbolEmptyBracketsToken():
lbp = 0
def nud(self):
return self
def __repr__(self):
return "(list )"
class symbolColonToken():
lbp = 0
def nud(self):
return self
def __repr__(self):
return ":"
class endToken:
lbp = 0
def __repr__(self):
return "end"
class booleanFalseToken():
lbp = 0
def nud(self):
return self
def __repr__(self):
return "#f"
class booleanTrueToken():
lbp = 0
def nud(self):
return self
def __repr__(self):
return "#t"
tokenToFunctionDictionary= {"All":"functionBuilderToken()","all" :"functionBuilderToken()", "++" : "listOperatorAppendToken()","cons(" : "functionPrefixConsToken()","tail(" : "functionPrefixTailToken()","head(" : "functionPrefixHeadToken()","false" : "booleanFalseToken()","False" : "booleanFalseToken()", "true" : "booleanTrueToken()","True" : "booleanTrueToken()","if" : "expressionPrefixIfToken()", ":" : "symbolColonToken()", "[]" : "symbolEmptyBracketsToken()", "," : "symbolCommaToken()","lambda" : "expressionPrefixLambdaToken()","(lambda" : "expressionPrefixLambdaToken()","if" : "expressionPrefixIfToken()", "^" : "operatorExponToken()", "%": "operatorModToken()",":=" : "operatorAssignToken()", "]": "operatorCloseBracketToken()", "[" : "operatorOpenBracketToken()", "=" : "operatorAssignToken()", "=" : "operatorEqualToken()", "->" : "operatorImpliesToken()","<->" : "operatorIfAndOnlyIfToken()", "not" : "operatorNotToken()", "and" : "operatorAndToken()", "or" : "operatorOrToken()", "=>" : "operatorGreaterThanEqualToken()", "=<" : "operatorLessThanEqualToken()", "<" : "operatorLessThanToken()", ">" : "operatorGreaterThanToken()", "+" : "operatorAddToken()", "*" : "operatorMulToken()", "-" : "operatorMinToken()", "(" : "operatorOpenParenToken()", ")" : "operatorCloseParenToken()",None : "endToken()"}
# ((expresion)) 80 (unary - +) 70 (/ *) 60 (+ -) 50 (< > =< => =) 40 (not) 31 (and or) 30 (<-> ->) 20 (if else lambda) 10 (:=) 5
tokenPattern = re.compile("[\*+]+|[\*+%^\[\]]|[0-9]+|<[A-Za-z]+\(|[A-Za-z]+\(|[A-Za-z0-9]+|'or'|'and'|'not'|\(|\)|=<|<->|->|=>|:=|<|>|-|=|:|,")
def printTokens(program): # Used for debugging tokenizer
for token in tokenPattern.findall(program):
print token
def tokenize(program):
for token in tokenPattern.findall(program):
if token.isdigit():
yield integerToken(token)
elif token in tokenToFunctionDictionary:
yield eval(tokenToFunctionDictionary[token])
elif token.startswith("<"):
yield functionDefinitionToken(token.replace("<","").replace("(",''))
elif token.endswith("("):
yield functionCallToken(token.replace("(",""))
else:
yield identifierToken(token)
yield endToken()
def parseString(program):
if len(program) == 0:
return
global token, next
next = tokenize(program).next
token = next()
return str(parse())
def formatProgram(filename):
text = []
input = open(filename, 'r')
definitionBuilding = False
string = ""
for line in input:
if len(line) > 1:
if line.startswith("<"):
definitionBuilding = True
if definitionBuilding:
if line.endswith('>\n'):
string = string + line[:-2]+ ",>\n"
text.append(string)
string = ""
definitionBuilding = False
else:
string = string + line[:-1] + ', '
else:
text.append(line)
input.close()
output = open(filename, 'w')
for line in text:
output.write(line+ "\n")
output.close()
def parseProgram(filename):
text = []
input = open(filename, 'r')
for line in input:
if len(line) > 1:
text.append(line)
input.close()
output = open(filename, 'w')
for line in text:
output.write(parseString(line)+ "\n")
output.close()
#formatProgram("testformatinginput.txt")
#parseProgram("testformatinginput.txt")
class textFrame(Frame):
def __init__(self, parent = None):
Frame.__init__(self, parent)
self.pack(expand=YES, fill=BOTH)
self.makewidgets()
def makewidgets(self):
text = Text(self)
text.pack(side=LEFT, expand=YES, fill=BOTH)
self.text = text
def getText(self):
return self.text.get('1.0', END+'-1c')
def setText(self):
lastinput = open("lastinput", 'r')
return self.text.insert(1.0, str(lastinput.read()) )
class App(textFrame):
def __init__(self, parent = None, file = None):
frame1= Frame(parent)
frame1.pack(fill=X)
Button(frame1, text='Save As', command=self.onSave).pack(side=LEFT)
Button(frame1, text='Run Code', command=self.onRunCode).pack(side=LEFT)
textFrame.__init__(self)
# self.frame2 = Frame(parent)
# self.frame.pack(fill=X,side=BOTTOM)
self.text2 = Text(height = 8,borderwidth = 5 )
self.text2.pack()
self.setText()
def onSave(self):
filename = asksaveasfilename()
if filename:
alltext = self.textbox.gettext()
open(filename, 'w').write(alltext)
def onRunCode(self):
f = open('codeToRun.txt', 'w')
f.write(self.getText())
save = open('lastInput', 'w')
save.write(self.getText())
save.close()
f.close()
formatProgram("codeToRun.txt")
parseProgram("codeToRun.txt")
self.text2.delete(1.0, END)
self.text2.insert(1.0, PNLI.load("codeToRun.txt") )
if __name__ == '__main__':
try:
App(file=sys.argv[1]).mainloop()
except IndexError:
App().mainloop()