-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostfix-interpreter.py
More file actions
129 lines (101 loc) · 3.77 KB
/
Copy pathpostfix-interpreter.py
File metadata and controls
129 lines (101 loc) · 3.77 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
import Myro
import Graphics
class Interpreter:
def __init__(self):
self.operators = {'display': self.display, '+': self.add, '-': self.sub, '*': self.mult, '/': self.div, '**': self.exp, 'forward': self.forward, 'backward': self.backward, 'turnRight': self.turnRight, 'turnLeft': self.turnLeft, 'beep': self.beep, 'beep2': self.beep2, 'getLightLeft': self.getLightLeft, 'getLightCenter': self.getLightCenter, 'getLightRight': self.getLightRight, 'getRightIR': self.getRightIR, 'getLeftIR': self.getLeftIR, 'getBattery': self.getBattery, 'takePicture': self.takePicture, 'showPicture': self.showPicture, 'read': self.read, 'popOff': self.popOff, 'dup': self.dup, 'swap': self.swap}
self.stack = []
def display(self):
v = self.stack.pop()
print (v)
def add(self):
v2 = float(self.stack.pop())
v1 = float(self.stack.pop())
self.stack.append(v1 + v2)
def sub(self):
v2 = float(self.stack.pop())
v1 = float(self.stack.pop())
self.stack.append(v1 - v2)
def mult(self):
v2 = float(self.stack.pop())
v1 = float(self.stack.pop())
self.stack.append(v1 * v2)
def div(self):
v2 = float(self.stack.pop())
v1 = float(self.stack.pop())
self.stack.append(v1 / v2)
def exp(self):
v2 = float(self.stack.pop())
v1 = float(self.stack.pop())
self.stack.append(v1 ** v2)
def forward(self):
v2 = float(self.stack.pop())
v1 = float(self.stack.pop())
self.stack.append(Myro.forward(v1,v2))
def backward(self):
v2 = float(self.stack.pop())
v1 = float(self.stack.pop())
self.stack.append(Myro.backward(v1,v2))
def turnRight(self):
v2 = float(self.stack.pop())
v1 = float(self.stack.pop())
self.stack.append(Myro.turnRight(v1,v2))
def turnLeft(self):
v2 = float(self.stack.pop())
v1 = float(self.stack.pop())
self.stack.append(Myro.turnLeft(v1,v2))
def beep(self):
v2 = float(self.stack.pop())
v1 = float(self.stack.pop())
self.stack.append(Myro.beep(v1,v2))
def beep2(self):
v3 = float(self.stack.pop())
v2 = float(self.stack.pop())
v1 = float(self.stack.pop())
self.stack.append(Myro.beep(v1,v2,v3))
def getLightLeft(self):
self.stack.append(Myro.getLight('left'))
def getLightRight(self):
self.stack.append(Myro.getLight('right'))
def getLightCenter(self):
self.stack.append(Myro.getLight('center'))
def getRightIR(self):
self.stack.append(Myro.getIR('right'))
def getLeftIR(self):
self.stack.append(Myro.getIR('left'))
def getBattery(self):
self.stack.append(Myro.getBattery())
def takePicture(self):
self.stack.append(Myro.takePicture())
def showPicture(self):
if len(self.stack) == 0:
self.takePicture()
p = self.stack.pop()
Myro.show(p)
def read(self):
fileName = self.stack.pop()
file = open(fileName)
for line in file.readlines():
if line[0] != "#":
i.interpret(line)
def interpret(self, expression):
for token in expression.split():
if token in self.operators:
operator = self.operators[token]
operator()
else:
self.stack.append(token)
def popOff(self):
self.stack.pop()
def dup(self):
v = float(self.stack.pop())
self.stack.append(v)
self.stack.append(v)
def swap(self):
v2 = float(self.stack.pop())
v1 = float(self.stack.pop())
self.stack.append(v2)
self.stack.append(v1)
i = Interpreter()
#to test put
#i.interpret('programname.yp read')
#into shell