-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculators.py
173 lines (132 loc) · 3.74 KB
/
calculators.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
from sly import Lexer, Parser
def is_number(token):
try:
float(token)
return True
except ValueError:
return False
class RPNCalculator:
operators = {
"+": lambda x, y: y + x,
"-": lambda x, y: y - x,
"*": lambda x, y: y * x,
"/": lambda x, y: y / x,
}
def __init__(self):
self.stack = []
def eval(self, expression: str):
tokens = expression.split()
for token in tokens:
if is_number(token):
self.stack.append(float(token))
else:
operands = self.stack.pop(), self.stack.pop()
result = self.operators[token](*operands)
self.stack.append(result)
return self.stack[0]
class PNCalculator:
operators = {
"+": lambda x, y: x + y,
"-": lambda x, y: x - y,
"*": lambda x, y: x * y,
"/": lambda x, y: x / y,
}
def __init__(self):
self.operand_stack = []
self.operator_stack = []
def eval(self, expr):
tokens = expr.split()
for token in tokens:
if is_number(token):
if self.operand_stack:
operand1 = float(self.operand_stack.pop())
operand2 = float(token)
operator = self.operator_stack.pop()
result = self.operators[operator](operand1, operand2)
self.operand_stack.append(result)
else:
self.operand_stack.append(token)
else:
self.operator_stack.append(token)
return self.operand_stack[0]
# SLY implementation
class CalcLexer(Lexer):
tokens = {NAME, NUMBER}
ignore = " \t"
literals = {"=", "+", "-", "*", "/", "(", ")"}
# Tokens
NAME = r"[a-zA-Z_][a-zA-Z0-9_]*"
@_(r"\d+")
def NUMBER(self, t):
t.value = int(t.value)
return t
@_(r"\n+")
def newline(self, t):
self.lineno += t.value.count("\n")
def error(self, t):
print("Illegal character '%s'" % t.value[0])
self.index += 1
class PNCalcParser(Parser):
tokens = CalcLexer.tokens
def __init__(self):
self.names = {}
@_('NAME "=" expr')
def statement(self, p):
self.names[p.NAME] = p.expr
@_("expr")
def statement(self, p):
print(p.expr)
@_('"+" expr expr')
def expr(self, p):
return p.expr0 + p.expr1
@_('"-" expr expr')
def expr(self, p):
return p.expr0 - p.expr1
@_('"*" expr expr')
def expr(self, p):
return p.expr0 * p.expr1
@_('"/" expr expr')
def expr(self, p):
return p.expr0 / p.expr1
@_("NUMBER")
def expr(self, p):
return p.NUMBER
@_("NAME")
def expr(self, p):
try:
return self.names[p.NAME]
except LookupError:
print("Undefined name '%s'" % p.NAME)
return 0
class RPNCalcParser(Parser):
tokens = CalcLexer.tokens
def __init__(self):
self.names = {}
@_('NAME "=" expr')
def statement(self, p):
self.names[p.NAME] = p.expr
@_("expr")
def statement(self, p):
print(p.expr)
@_('expr expr "+"')
def expr(self, p):
return p.expr0 + p.expr1
@_('expr expr "-"')
def expr(self, p):
return p.expr0 - p.expr1
@_('expr expr "*"')
def expr(self, p):
return p.expr0 * p.expr1
@_('expr expr "/"')
def expr(self, p):
return p.expr0 / p.expr1
@_("NUMBER")
def expr(self, p):
return p.NUMBER
@_("NAME")
def expr(self, p):
try:
return self.names[p.NAME]
except LookupError:
print("Undefined name '%s'" % p.NAME)
return 0