-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.py
147 lines (132 loc) · 3.44 KB
/
load.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
import sys
from psparser import read
from psoperators import PSOperators
from colors import *
testinput1 = """
/x 4 def
/g { x stack } def
/f { /x 7 def g } def
f
"""
testinput2 = """
/x 4 def
(static_y) dup 7 120 put /x exch def
/g { x stack } def
/f { /x (dynamic_x) def g } def
f
"""
testinput3 = """
/m 50 def
/n 100 def
/egg1 {/m 25 def n} def
/chic
{ /n 1 def
/egg2 { (egg2) n stack} def
n m
egg1
m
egg2
} def
n
chic
"""
testinput4 = """
/x 10 def
/A { x } def
/C { /x 40 def A stack } def
/B { /x 30 def /A { x 2 mul } def C } def
B
"""
testinput5 = """
/x 2 def
/n 5 def
/A { 1 n -1 1 {pop x mul} for} def
/C { /n 3 def /x 40 def A stack } def
/B { /x 30 def /A { x } def C } def
B
"""
testinput6 = """
/out true def
/xand { true eq {pop false} {pop true} ifelse dup /x exch def stack} def
/myput { out dup /x exch def xand } def
/f { /out false def myput } def
false f
"""
testinput7 = """
/x 1 dict def
x /i 22 put
/A { (global) x /i get } def
/C { /x 1 dict def x /i 33 put A stack } def
/B { /x 1 dict def x /i 11 put /A { (function B) x /i get } def C } def
B
"""
testinput8 = """
/x 1 dict def
/a 10 def
/A { x /m 0 put } def
/C { /x 1 dict def x /m 9 put A a x /m get stack } def
/B { /x 1 dict def /A { x /m 99 put } def /a 5 def C } def
B
"""
testinput9 = """
/And { AndOp1 } def
/AndOp1 { AndOp2 AndFalse ifelse } def
/AndOp2 { AndTrue AndFalse ifelse } def
/AndTrue { True } def
/AndFalse { False } def
True True And
False True And
False False And
stack
"""
testinput10 = """
/Or { OrOp1 } def
/OrOp1 { OrOp1T OrOp1F ifelse} def
/OrOp1T { True } def
/OrOp1F { OrOp2 } def
/OrOp2 { OrOp2T OrOp2F ifelse} def
/OrOp2T { True } def
/OrOp2F { False } def
True True Or
False True Or
False False Or
stack
"""
testinput11 = """
/Or { OrOp1 } def
/OrOp1 { OrOp1T OrOp1F ifelse} def
/OrOp1T { True } def
/OrOp1F { OrOp2 } def
/OrOp2 { OrOp2T OrOp2F ifelse} def
/OrOp2T { True } def
/OrOp2F { False } def
True True Or
False True Or
False False Or
stack
"""
tests = [testinput1,testinput2,testinput3,testinput4,testinput5,testinput6,testinput7,testinput8,testinput9,testinput10,testinput11]
# program start
if __name__ == '__main__':
ps_env_s = PSOperators("static")
ps_env_d = PSOperators("dynamic")
testnum = 1
for testcase in tests:
try:
print("\n-- TEST {} --".format(testnum))
expr_list = read(testcase)
print("\nSTATIC")
# interpret using static scoping rule
for expr in expr_list:
expr.eval(ps_env_s)
print("\nDYNAMIC")
# interpret using dynamic scoping rule
for expr in expr_list:
expr.eval(ps_env_d)
# clear the Stack objects
except (SyntaxError, NameError, TypeError, Exception) as err:
print(type(err).__name__ + ':', err)
testnum += 1
# clear the Stack objects
ps_env_s.clearBoth()
ps_env_d.clearBoth()