-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathDenotation2.4.py
189 lines (144 loc) · 5.08 KB
/
Denotation2.4.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
##Denotation 2.4
##指称语义
##python 3.4.1
class Number(object):
def __init__(self, value):
self.value = value
def to_python(self):
return repr(self.value)
class Boolean(object):
def __init__(self, value):
self.value = value
def to_python(self):
return repr(self.value)
class Variable(object):
def __init__(self, name):
self.name = name
def to_python(self):
return self.name
class Add(object):
def __init__(self, left, right):
self.left = left
self.right = right
def to_python(self):
left = repr(self.left.to_python())
right = repr(self.right.to_python())
return 'eval({left}, globals()) + eval({right}, globals())'.format(**locals())
class Multiply(object):
def __init__(self, left, right):
self.left = left
self.right = right
def to_python(self):
left = repr(self.left.to_python())
right = repr(self.right.to_python())
return 'eval({left}, globals()) * eval({right}, globals())'.format(**locals())
class LessThan(object):
def __init__(self, left, right):
self.left = left
self.right = right
def to_python(self):
left = repr(self.left.to_python())
right = repr(self.right.to_python())
return 'eval({left}, globals()) < eval({right}, globals())'.format(**locals())
class Assign(object):
def __init__(self, name, expression):
self.name = name
self.expression = expression
def to_python(self):
name = self.name
expression = repr(self.expression.to_python())
return '{name} = eval({expression}, globals())'.format(**locals())
class DoNothing(object):
def to_python(self):
return ''
class If(object):
def __init__(self, condition, consequence, alternative):
self.condition = condition
self.consequence = consequence
self.alternative = alternative
def to_python(self):
condition = self.condition.to_python()
consequence = self.consequence.to_python()
alternative = self.alternative.to_python()
return 'if {condition} :\n {consequence}\nelse:\n {alternative}'.format(**locals())
class Sequence(object):
def __init__(self, first, second):
self.first = first
self.second = second
def to_python(self):
first = self.first.to_python()
second = self.second.to_python()
return '{first}\n{second}'.format(**locals())
class While(object):
def __init__(self, condition, body):
self.condition = condition
self.body = body
def to_python(self):
condition = self.condition.to_python()
body = self.body.to_python()
return 'while {condition}:\n {body}'.format(**locals())
##test
##数值表达式
proc = Number(5).to_python()
print(proc)
##python的解释器中执行表达式的函数是eval()
num = eval(proc,{})
print(num, end = '\n\n')
##布尔值表达式
proc = Boolean(True).to_python()
print(proc)
thing = eval(proc,{})
print(thing, end = '\n\n')
##变量表达式
proc = Variable('x').to_python()
print(proc)
var = eval(proc,{'x':7})
print(var, end = '\n\n')
##环境
environment = {'x':3}
print(environment, end = '\n\n')
##加法表达式
proc = Add(Variable('x'), Number(1)).to_python()
print(proc)
result = eval(proc, environment)
print(result, end = '\n\n')
##乘法表达式
proc = Multiply(Variable('x'), Number(8)).to_python()
print(proc)
result = eval(proc, environment)
print(result, end = '\n\n')
##比较表达式
proc = LessThan(Variable('x'), Add(Multiply(Variable('x'), Number(8)), Number(3))).to_python()
print(proc)
result = eval(proc, environment)
print(result, end = '\n\n')
##赋值语句
environment = {'x':3}
statement = Assign('y', Number(5)).to_python()
print(statement)
##python的解释器中执行语句的函数是exec()
exec(statement, environment)
##print(environment, end = '\n\n')
##经过exec()后的environment字典,为了方便演示,下面的语句不显示内建的__builtins__对象名称与属性
print(dict([(k, v) for k, v in environment.items() if not k == '__builtins__']), end = '\n\n')
##if语句
environment = {'x':3, 'y':5}
statement = If(LessThan(Variable('x'), Variable('y')), Assign('z', Number(1)), Assign('z', Number(0))).to_python()
print(statement)
exec(statement, environment)
print(dict([(k, v) for k, v in environment.items() if not k == '__builtins__']), end = '\n\n')
##语句序列
environment = {'x':3, 'y':5, 'z':6}
statement = Sequence(Assign('x', Number(1)), Assign('y', Number(2))).to_python()
print(statement)
exec(statement , environment)
print(dict([(k, v) for k, v in environment.items() if not k == '__builtins__']), end = '\n\n')
#while语句
environment = {'x':1}
statement = While(
LessThan(Variable('x'), Number(8)),
Assign('x', Multiply(Variable('x'), Number(3)))
).to_python()
print(statement)
exec(statement , environment)
print(dict([(k, v) for k, v in environment.items() if not k == '__builtins__']), end = '\n\n')