-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartcalculator7.py
More file actions
230 lines (180 loc) · 5.69 KB
/
smartcalculator7.py
File metadata and controls
230 lines (180 loc) · 5.69 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
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
from enum import Enum
class TokenType(Enum):
NUMBER = 1
IDENTIFIER = 2
ASSIGNMENT = 3
OPERATOR = 4
UNKNOWN = 5
class Token:
def __init__(self, ttype, value):
self.ttype = ttype
self.value = value
class InvalidAssignment(Exception):
pass
class InvalidIdentifier(Exception):
pass
class InvalidExpression(Exception):
pass
class UnknownVariable(Exception):
pass
class Stack:
def __init__():
self.storage = []
def pop():
return self.storage.pop()
def push(x):
self.storage.append(x)
def read_token(s, i):
if s[i].isdigit():
return read_number(s, i)
elif s[i].isalpha():
return read_identifier(s, i)
elif (s[i] in "+-"):
return read_operators(s, i)
elif (s[i] == "="):
return read_assignment(s, i)
else:
return read_unknown(s, i)
def skip_whitespaces(s, i):
while (i < len(s)) and (s[i] == " "):
i += 1
return i
def read_number(s, i):
tmp = ""
while ((i < len(s)) and (s[i].isdigit())):
tmp += s[i]
i += 1
return(i, Token(TokenType.NUMBER, int(tmp)))
def read_identifier(s, i):
tmp = ""
while (i < len(s)) and (s[i].isalnum()):
tmp += s[i]
i += 1
return (i, Token(TokenType.IDENTIFIER, tmp))
def valid_identifier(name):
for ch in name:
if (not ch.isalpha()):
return False
return True
def read_assignment(s, i):
if (i < len(s)) and (s[i] == "="):
i += 1
return (i, Token(TokenType.ASSIGNMENT, "="))
def switch(op):
if (op == " +"):
return "-"
elif (op == "-"):
return "+"
return None
def read_operators(s, i):
op = None
'''while (i < len(s)) and (s[i] in " +-"):
if (op is None) and (s[i] != " "):
op = s[i]
elif (s[i] == "-"):
op = switch(op)
i += 1'''
if (i < len(s)) and (s[i] in "+-"):
op = s[i]
i += 1
return (i, Token(TokenType.OPERATOR, op))
def read_unknown(s, i):
tmp = ""
while (i < len(s)) and (s[i] not in " +-="):
tmp += s[i]
i += 1
return (i, Token(TokenType.UNKNOWN, tmp))
def do_operation(x, y, op):
if (op == "+"):
return x + y
elif (op == "-"):
return x - y
def find_assignment(tokens):
if len(tokens) == 0:
return False
for i in range(len(tokens)):
if tokens[i].ttype == TokenType.ASSIGNMENT:
if (tokens[0].ttype != TokenType.IDENTIFIER):
raise InvalidAssignment
if (not valid_identifier(tokens[0].value)):
raise InvalidIdentifier
if (i != 1):
raise InvalidAssignment
rhs = tokens[2:]
if (len(rhs) == 0) or (find_assignment(rhs)):
raise InvalidAssignment
try:
variables[tokens[0].value] = evaluate(rhs)
except (InvalidIdentifier):
raise InvalidAssignment
except (InvalidExpression):
raise InvalidAssignment
return True
def evaluate(tokens):
result = 0
op = "+"
if len(tokens) == 0:
return None
for i in range(0, len(tokens)):
t = tokens[i]
if (t.ttype == TokenType.IDENTIFIER):
if (not valid_identifier(tokens[0].value)):
raise InvalidIdentifier
if (not t.value in variables):
raise UnknownVariable
elif (i > 0) and (tokens[i - 1].ttype != TokenType.OPERATOR):
raise InvalidExpression
result = do_operation(result, variables[t.value], op)
elif (t.ttype == TokenType.NUMBER):
if (i > 0) and (tokens[i - 1].ttype != TokenType.OPERATOR):
raise InvalidExpression
result = do_operation(result, t.value, op)
elif (t.ttype == TokenType.OPERATOR):
if (i > 0) and (tokens[i - 1].ttype == TokenType.OPERATOR):
raise InvalidExpression
op = t.value
else:
raise InvalidExpression
if (tokens[-1].ttype == TokenType.OPERATOR):
raise InvalidExpression
#print(result)
return result
help_string = "The program calculates the sum of numbers"
unknown_command_string = "Unknown command"
invalid_expression_string = "Invalid expression"
variables = {}
while (True):
user_input = input().strip()
if len(user_input) == 0:
continue
if (user_input.startswith("/")):
cmd_str = user_input
if cmd_str == "/help":
print(help_string)
elif (cmd_str == "/exit"):
print("Bye!")
break
else:
print(unknown_command_string)
else:
result = 0
tokens = []
i = 0
while (i < len(user_input)):
(i, t) = read_token(user_input, i)
tokens.append(t)
i = skip_whitespaces(user_input, i)
#print([(t.ttype, t.value) for t in tokens])
try:
if (not find_assignment(tokens)):
r = evaluate(tokens)
if (not r is None):
print(r)
except (InvalidIdentifier):
print("Invalid identifier")
except (InvalidAssignment):
print("Invalid assignment")
except (UnknownVariable):
print("Unknown variable")
except (InvalidExpression):
print("Invalid expression")