-
Notifications
You must be signed in to change notification settings - Fork 0
/
programext.py
executable file
·730 lines (564 loc) · 22.2 KB
/
programext.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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#!/usr/bin/python
#
# exp.py - Classes to represent underlying data structures for the grammar
# below, for the mini-compiler.
#
# Kurt Schmidt
# 8/07
#
# DESCRIPTION:
# Just a translation of the C++ implementation by Jeremy Johnson (see
# programext.cpp)
#
# EDITOR: cols=80, tabstop=2
#
# NOTES
# environment:
# a dict
#
# Procedure calls get their own environment, can not modify enclosing env
#
# Grammar:
# program: stmt_list
# stmt_list: stmt ';' stmt_list
# | stmt
# stmt: assign_stmt
# | define_stmt
# | if_stmt
# | while_stmt
# assign_stmt: IDENT ASSIGNOP expr
# define_stmt: DEFINE IDENT PROC '(' param_list ')' stmt_list END
# if_stmt: IF expr THEN stmt_list ELSE stmt_list FI
# while_stmt: WHILE expr DO stmt_list OD
# param_list: IDENT ',' param_list
# | IDENT
# expr: expr '+' term
# | expr '-' term
# | term
# term: term '*' factor
# | factor
# factor: '(' expr ')'
# | NUMBER
# | IDENT
# | funcall
# funcall: IDENT '(' expr_list ')'
# expr_list: expr ',' expr_list
# | expr
#
import sys
import copy
import itertools
import logging
logging.basicConfig(
format = "%(levelname) -4s %(message)s",
level = logging.DEBUG
)
log = logging.getLogger('programext')
#### CONSTANTS ################
# the variable name used to store a proc's return value
returnSymbol = 'return'
tabstop = ' ' # 2 spaces
###### CLASSES ##################
class Expr :
'''Virtual base class for expressions in the language'''
def __init__( self ) :
raise NotImplementedError(
'Expr: pure virtual base class. Do not instantiate' )
def eval( self, nt, ft ) :
'''Given an environment and a function table, evaluates the expression,
returns the value of the expression (an int in this grammar)'''
raise NotImplementedError(
'Expr.eval: virtual method. Must be overridden.' )
def display( self, nt, ft, depth=0 ) :
'For debugging.'
raise NotImplementedError(
'Expr.display: virtual method. Must be overridden.' )
def pythonListToList(self, inputList):
listLen = len(inputList)
outerSeq = None
i = 0
while( i < listLen) :
val = inputList[i]
# check to see if the current element is a native python type
if isinstance(val,int) :
# convert to Number
currentElem = Number(val)
elif isinstance(val, list) :
# convert to List
currentElem = self.pythonListToList(val)
else :
# it's not a native python type
currentElem = val
innerSeq = Sequence(currentElem)
if(outerSeq is not None) :
outerSeq = Sequence(outerSeq,innerSeq)
else :
outerSeq = Sequence(innerSeq)
i = (i+1)
createdList = List(outerSeq)
return createdList
class Element( Expr ) :
'''Lists or integers'''
def __init__( self, v=0 ) :
print("Element ctor")
self.value = v
def eval( self, nt, ft ) :
return self.value.eval(nt,ft)
def display( self, nt, ft, depth=0 ) :
print "%s%i" % (tabstop*depth, self.value)
class Number( Element ) :
'''Just integers'''
def __init__( self, v=0 ) :
self.value = v
def eval( self, nt, ft ) :
return self.value
def display( self, nt, ft, depth=0 ) :
print "%s%i" % (tabstop*depth, self.value)
class List( Element ) :
def __init__( self, s=None ) :
self.values = list()
if(s is not None):
if (isinstance(s,Sequence)) :
self.unPackSequence(s)
else :
self.values.append(s)
def unPackSequence(self,seq):
""" Loops through the sequence, pulling out
lists and numbers and appends to one list.
:param seq: Sequence Object.
"""
for val in seq.values:
# If this is a Sequence, we need to loop
# through all of the values.
if (isinstance(val,Sequence)):
for y in val.values:
# Need to check for a nested Sequence..
if (isinstance(y,Sequence)):
self.unPackSequence(y)
# Check for a nested list..
elif (isinstance(y,List)):
# Append the nested list..
self.values.append(y)
else:
# Number object, just add to the list.
self.values.append(y)
elif(isinstance(val,List)):
# Append the nested list..
self.values.append(val)
else:
# Number object, just add to the list.
self.values.append(val)
def eval( self, nt, ft ) :
evaledList = copy.deepcopy(self.values)
for i in xrange(len(evaledList)) :
if evaledList[i] is not None:
if isinstance(evaledList[i], FunCall) :
funCallValue = evaledList[i].eval(nt, ft)
if isinstance(funCallValue, List) :
evaledList[i] = funCallValue.eval(nt, ft)
else :
evaledList[i] = funCallValue
else :
evaledList[i] = evaledList[i].eval(nt,ft)
return evaledList
def display( self, nt, ft, depth=0 ) :
for val in self.values :
val.display(nt,ft,depth+1)
def __str__(self):
'''Define a repr to have pretty printing of lists. Otherwise, we get
the memory addr, which doesn't work out so well when trying to compare
test results.
'''
return "List with %d elements" % len(self.values)
class Sequence( Expr ) :
def __init__( self, e, s=None ) :
self.values = list()
self.insertHead(e)
if(s is not None):
self.appendTail(s)
def insertHead( self , e ) :
self.values.insert(0,e)
def appendTail ( self, e ) :
self.values.append(e)
def eval( self, nt, ft ) :
evaledSeq = list()
for val in self.values :
evaledSeq.append(val.eval(nt,ft))
return evaledSeq
#for val in self.values :
# yield val.eval(nt,ft)
def display( self, nt, ft, depth=0 ) :
if self.values is not None :
for val in self.values :
val.display(nt, ft, depth)
else :
print("Empty")
class Ident( Expr ) :
'''Stores the symbol'''
def __init__( self, name ) :
self.name = name
def __str__(self):
return self.name
def eval( self, nt, ft ) :
return nt[ self.name ]
def display( self, nt, ft, depth=0 ) :
print "%s%s" % (tabstop*depth, self.name)
class Times( Expr ) :
'''expression for binary multiplication'''
def __init__( self, lhs, rhs ) :
'''lhs, rhs are Expr's, the operands'''
# test type here?
# if type( lhs ) == type( Expr ) :
self.lhs = lhs
self.rhs = rhs
def eval( self, nt, ft ) :
return self.lhs.eval( nt, ft ) * self.rhs.eval( nt, ft )
def display( self, nt, ft, depth=0 ) :
print "%sMULT" % (tabstop*depth)
self.lhs.display( nt, ft, depth+1 )
self.rhs.display( nt, ft, depth+1 )
#print "%s= %i" % (tabstop*depth, self.eval( nt, ft ))
class Plus( Expr ) :
'''expression for binary addition'''
def __init__( self, lhs, rhs ) :
self.lhs = lhs
self.rhs = rhs
def eval( self, nt, ft ) :
return self.lhs.eval( nt, ft ) + self.rhs.eval( nt, ft )
def display( self, nt, ft, depth=0 ) :
print "%sADD" % (tabstop*depth)
self.lhs.display( nt, ft, depth+1 )
self.rhs.display( nt, ft, depth+1 )
#print "%s= %i" % (tabstop*depth, self.eval( nt, ft ))
class Minus( Expr ) :
'''expression for binary subtraction'''
def __init__( self, lhs, rhs ) :
self.lhs = lhs
self.rhs = rhs
def eval( self, nt, ft ) :
return self.lhs.eval( nt, ft ) - self.rhs.eval( nt, ft )
def display( self, nt, ft, depth=0 ) :
print "%sSUB" % (tabstop*depth)
self.lhs.display( nt, ft, depth+1 )
self.rhs.display( nt, ft, depth+1 )
#print "%s= %i" % (tabstop*depth, self.eval( nt, ft ))
class Concat( Expr ) :
'''expression for list concatenation'''
def __init__( self, lhs, rhs ) :
self.lhs = lhs
self.rhs = rhs
def eval( self, nt, ft) :
if not (isinstance(self.lhs, Ident) or isinstance(self.lhs, List) or isinstance(self.lhs, FunCall)) :
raise Exception("List concatenation requires two Lists")
if not (isinstance(self.rhs, Ident) or isinstance(self.rhs, List) or isinstance(self.rhs, FunCall)) :
raise Exception("List concatenation requires two Lists")
if isinstance(self.lhs, Ident) :
# since it's an Ident, it needs two-level evaluation to get to the native python list
lhsIdent = self.lhs.eval(nt, ft)
lhsListEval = lhsIdent.eval(nt, ft)
if not isinstance(lhsListEval, list) :
raise Exception("Identity must be a list for || operator")
elif isinstance(self.lhs, FunCall) :
# since it's a FunCall, it needs two-level evaluation to get to the native python list
lhsFunc = self.lhs.eval(nt, ft)
lhsListEval = lhsFunc.eval(nt, ft)
if not isinstance(lhsListEval, list) :
raise Exception("Function must return a List for || operator")
else :
# only requires one-level of evaluation to get to the native python list
lhsListEval = self.lhs.eval(nt, ft)
if isinstance(self.rhs, Ident) :
# since it's an Ident, it needs two-level evaluation to get to the native python list
rhsIdent = self.rhs.eval(nt, ft)
rhsListEval = rhsIdent.eval(nt, ft)
if not isinstance(rhsListEval, list) :
raise Exception("Identity must be a list for || operator")
elif isinstance(self.rhs, FunCall) :
# since it's a FunCall, it needs two-level evaluation to get to the native python list
rhsFunc = self.rhs.eval(nt, ft)
rhsListEval = rhsFunc.eval(nt, ft)
if not isinstance(rhsListEval, list) :
raise Exception("Function must return a List for || operator")
else :
# only requires one-level of evaluation to get to the native python list
rhsListEval = self.rhs.eval(nt, ft)
extendedList = lhsListEval + rhsListEval
return self.pythonListToList(extendedList)
def display( self, nt, ft, depth=0 ) :
print "%sCONCAT" % (tabstop*depth)
self.lhs.display( nt, ft, depth+1 )
self.rhs.display( nt, ft, depth+1 )
class FunCall( Expr ):
'''stores a function call:
- its name, and arguments'''
def __init__( self, name, argList ) :
self.name = name
self.argList = argList
def car( self, nt, ft ) :
if not(len(self.argList) == 1) :
raise Exception("Car function requires exactly 1 argument")
listArg = self.argList[0]
listPassed = None
if(isinstance(listArg,Ident)) :
# We were passed an Ident
listPassed = self.argList[0].eval(nt,ft)
elif(isinstance(listArg,List)) :
# We were passed a List object
listPassed = listArg
if not(isinstance(listPassed,List)) :
raise Exception("Can only call car on List")
# We have a parsed List object. Call eval to get a native list
evaledList = listPassed.eval(nt,ft)
if(len(evaledList) < 1) :
raise Exception("Can't call car on empty List")
return evaledList[0]
def cdr( self, nt, ft):
listArg = self.argList[0]
listPassed = None
if(isinstance(listArg,Ident)) :
# We were passed an Ident
# listPassed = self.argList[0].eval(nt,ft)
listPassed = evalIdent(listArg, nt, ft)
elif(isinstance(listArg,List)) :
# We were passed a List object
listPassed = listArg
if not(isinstance(listPassed,List)) :
raise Exception("Can only call cdr on List")
# We have a parsed List object. Call eval to get a native list
evaledList = listPassed.eval(nt,ft)
if(len(evaledList) < 1) :
raise Exception("Can't call cdr on empty List")
return self.pythonListToList(evaledList[1:])
def nullp( self, nt, ft ):
'Returns 1 if the List is Null, otherwise 0'
try:
the_list = self.argList[0].eval(nt,ft).eval(nt,ft);
except:
#It's not a list, so therefore, it's not null
return 0;
else:
if not the_list:
return 1
else:
return 0
def listp( self, nt, ft ):
"Returns 1 if a list, otherwise 0"
try:
evaledArg = self.argList[0].eval(nt,ft)
if isinstance(evaledArg, List) or isinstance(evaledArg, list) :
return 1
else:
return 0
except:
return 0
def intp( self, nt, ft ):
"Returns 1 if it is Number, otherwise 0"
try:
if isinstance(self.argList[0], Number):
return 1
else:
return 0
except:
return 0
def cons( self, nt, ft ) :
'''Returns a new list, with element prepended to existing list'''
if not(len(self.argList) == 2) :
raise Exception("Cons function requires exactly 2 arguments")
# evaluate the first argument
arg1 = self.argList[0]
if (isinstance(arg1, Ident) or isinstance(arg1, FunCall)) :
object = arg1.eval(nt, ft)
if not (isinstance(object, int) or isinstance(object, list)) :
# needs to be evaluated twice to get to native python type
evalObject = object.eval(nt,ft)
else :
evalObject = object
else :
# only needs to be evaluated once to get to native python type
evalObject = arg1.eval(nt,ft)
if not(isinstance(evalObject, list) or isinstance(evalObject, int)) :
raise Exception("Can only cons an object onto a List")
# evaluate the second argument
arg2 = self.argList[1]
if (isinstance(arg2, Ident) or isinstance(arg2, FunCall)) :
# needs to be evaluated twice to get to the native python type
destList = arg2.eval(nt,ft)
if isinstance(destList, int) :
raise Exception("Can only cons an object onto a List")
evalDestList = destList.eval(nt,ft)
else :
evalDestList = arg2.eval(nt,ft)
if not(isinstance(evalDestList, list)) :
raise Exception("Can only cons an object onto a List")
# arguments check out, so create a new list based on evalDestList
# then insert evalObject at the head of the list
newList = evalDestList
newList.insert(0, evalObject)
return self.pythonListToList(newList)
#return newList
def eval( self, nt, ft ) :
func = getattr(self, self.name, None)
# Is this function defined in this class?
if func:
# It is, so call it (like car, cdr, etc...)
return func(nt,ft)
# Otherwise, call the function from the function table
else :
return ft[ self.name ].apply( nt, ft, self.argList )
def display( self, nt, ft, depth=0 ) :
print "%sFunction Call: %s, args:" % (tabstop*depth, self.name)
for e in self.argList :
e.display( nt, ft, depth+1 )
#-------------------------------------------------------
class Stmt :
'''Virtual base class for statements in the language'''
def __init__( self ) :
raise NotImplementedError(
'Stmt: pure virtual base class. Do not instantiate' )
def eval( self, nt, ft ) :
'''Given an environment and a function table, evaluates the expression,
returns the value of the expression (an int in this grammar)'''
raise NotImplementedError(
'Stmt.eval: virtual method. Must be overridden.' )
def display( self, nt, ft, depth=0 ) :
'For debugging.'
raise NotImplementedError(
'Stmt.display: virtual method. Must be overridden.' )
class AssignStmt( Stmt ) :
'''adds/modifies symbol in the current context'''
def __init__( self, name, rhs ) :
'''stores the symbol for the l-val, and the expressions which is the
rhs'''
self.name = name
self.rhs = rhs
def eval( self, nt, ft ) :
if(isinstance(self.rhs.eval(nt,ft),list)) :
# We shouldn't eval the list at assignment time, per instructions
nt[ self.name ] = self.rhs
else :
nt[ self.name ] = self.rhs.eval( nt, ft )
def display( self, nt, ft, depth=0 ) :
print "%sAssign: %s :=" % (tabstop*depth, self.name)
self.rhs.display( nt, ft, depth+1 )
class DefineStmt( Stmt ) :
'''Binds a proc object to a name'''
def __init__( self, name, proc ) :
self.name = name
self.proc = proc
def eval( self, nt, ft ) :
ft[ self.name ] = self.proc
def display( self, nt, ft, depth=0 ) :
print "%sDEFINE %s :" % (tabstop*depth, self.name)
self.proc.display( nt, ft, depth+1 )
class IfStmt( Stmt ) :
def __init__( self, cond, tBody, fBody ) :
'''expects:
cond - expression (integer)
tBody - StmtList
fBody - StmtList'''
self.cond = cond
self.tBody = tBody
self.fBody = fBody
def eval( self, nt, ft ) :
if self.cond.eval( nt, ft ) > 0 :
self.tBody.eval( nt, ft )
else :
self.fBody.eval( nt, ft )
def display( self, nt, ft, depth=0 ) :
print "%sIF" % (tabstop*depth)
self.cond.display( nt, ft, depth+1 )
print "%sTHEN" % (tabstop*depth)
self.tBody.display( nt, ft, depth+1 )
print "%sELSE" % (tabstop*depth)
self.fBody.display( nt, ft, depth+1 )
class WhileStmt( Stmt ) :
def __init__( self, cond, body ) :
self.cond = cond
self.body = body
def eval( self, nt, ft ) :
while self.cond.eval( nt, ft ) > 0 :
self.body.eval( nt, ft )
def display( self, nt, ft, depth=0 ) :
print "%sWHILE" % (tabstop*depth)
self.cond.display( nt, ft, depth+1 )
print "%sDO" % (tabstop*depth)
self.body.display( nt, ft, depth+1 )
#-------------------------------------------------------
class StmtList :
'''builds/stores a list of Stmts'''
def __init__( self ) :
self.sl = []
def insert( self, stmt ) :
self.sl.insert( 0, stmt )
def eval( self, nt, ft ) :
for s in self.sl :
s.eval( nt, ft )
def display( self, nt, ft, depth=0 ) :
print "%sSTMT LIST" % (tabstop*depth)
for s in self.sl :
s.display( nt, ft, depth+1 )
class Proc :
'''stores a procedure (formal params, and the body)
Note that, while each function gets its own environment, we decided not to
allow side-effects, so, no access to any outer contexts. Thus, nesting
functions is legal, but no different than defining them all in the global
environment. Further, all calls are handled the same way, regardless of
the calling environment (after the actual args are evaluated); the proc
doesn't need/want/get an outside environment.'''
def __init__( self, paramList, body ) :
'''expects a list of formal parameters (variables, as strings), and a
StmtList'''
self.parList = paramList
self.body = body
def apply( self, nt, ft, args ) :
newContext = {}
# sanity check, # of args
if len( args ) is not len( self.parList ) :
print "Param count does not match:"
sys.exit( 1 )
# bind parameters in new name table (the only things there right now)
# use zip, bastard
for i in range( len( args )) :
newContext[ self.parList[i] ] = args[i].eval( nt, ft )
# evaluate the function body using the new name table and the old (only)
# function table. Note that the proc's return value is stored as
# 'return in its nametable
self.body.eval( newContext, ft )
if newContext.has_key( returnSymbol ) :
return newContext[ returnSymbol ]
else :
print "Error: no return value"
sys.exit( 2 )
def display( self, nt, ft, depth=0 ) :
print "%sPROC %s :" % (tabstop*depth, str(self.parList))
self.body.display( nt, ft, depth+1 )
class Program :
def __init__( self, stmtList ) :
self.stmtList = stmtList
self.nameTable = {}
self.funcTable = {}
def eval( self ) :
self.stmtList.eval( self.nameTable, self.funcTable )
def dump( self ) :
print "Dump of Symbol Table"
for k in self.nameTable :
if(isinstance(self.nameTable[k],List) or isinstance(self.nameTable[k],FunCall)):
print("Print List")
print " %s -> %s " % ( str(k), self.nameTable[k].eval(self.nameTable,self.funcTable))
else :
print " %s -> %s " % ( str(k), str(self.nameTable[k]) )
print "Function Table"
for k in self.funcTable :
print " %s" % str(k)
def display( self, depth=0 ) :
print "%sPROGRAM :" % (tabstop*depth)
self.stmtList.display( self.nameTable, self.funcTable )
# FUNCTIONS
def evalIdent(ident, nt, ft):
orig = ident
while (isinstance(ident, Ident) and not isinstance(ident, List)):
ident = ident.eval(nt, ft)
if not isinstance(ident,List):
return orig.pythonListToList(ident)
else:
return ident