-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.py
More file actions
399 lines (355 loc) · 14.7 KB
/
Copy pathdisplay.py
File metadata and controls
399 lines (355 loc) · 14.7 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import pyxel
import time
import numpy as np
# REFACTOR both functions
def isNumber(string):
if string[0] == "+":
return False
try:
float(string)
return True
except ValueError:
return False
def isPartOfNum(chars):
"""Custom function to detect numbers (or parts of them) on a substring"""
# print("chars", f"'{chars}'")
if len(chars) == 0:
# print("ERROR: This is not a part of a number, it's an empty string")
return False
if chars[0] == "-" or chars[0] == ".":
if len(chars) == 1:
# Just the sign/period, might be a piece of a number
return True
else:
return isNumber(chars[1:])
elif chars[:2] == "-.":
if len(chars) == 2:
return True
else:
return isNumber(chars[2:])
else:
return isNumber(chars)
class Display:
def __init__(self):
# Chain of current operations
self.chain = "((3-1)*(2+3))"
self.pastOperations = []
# Cursor's position (inverse order, last position is index 0)
self.index = 0
# Draws the cursor if its position is changing
self.prevIndex = self.index
self.keyCooldown = 0.1
self.lastPressed = time.time() - self.keyCooldown
# Text color specifies if the syntax is correct (black) or not (grey)
self.color = 0
def addOperation(self, button):
if button.value == "CE":
self.clear()
elif button.value == "=":
# Container method to perform all calculation related operations
self.calc()
elif button.type == "operator":
# We remove the last operation (if any) and change it for the new one
pos = len(self.chain) - 1 - self.index
if self.index == len(self.chain):
pos = 0
if pos != -1 and len(self.chain) > 0:
if (
self.chain[pos] == "+"
or self.chain[pos] == "-"
or self.chain[pos] == "*"
or self.chain[pos] == "/"
):
# There's already an operator, we need to delete it
if pos == -1:
self.chain = self.chain[:-1]
else:
self.chain = self.chain[:pos] + self.chain[pos + 1 :]
# We add an operator, regardless if there was one before or not
self.__addStr(str(button.value))
elif len(self.chain) > 0 and button.value == "-" and self.chain[pos] != "-":
self.__addStr(str(button.value))
else:
# Normal character is added
self.__addStr(str(button.value))
self.__checkSyntax()
def calculate(self, result=0):
if self.__checkSyntax():
if "(" in self.chain:
prevChain = self.chain
# Now, 'self.chain' contains the new subchain detected
newSubchain, startIndex = self.__findSubChain(self.chain)
self.chain = newSubchain
result = self.calculate()
# A new chain is created with the result of the subchain calculation
# We delete the parenthesis and add the result
self.chain = (
prevChain[:startIndex]
+ str(result)
+ prevChain[startIndex + len(newSubchain) + 2 :]
)
print(prevChain, "--->", self.chain)
# print("Checking on new chain", chain)
return self.calculate(result=result)
else:
# Index of the next operator on the chain (following PEMDAS)
index = self.__findOperator(self.chain)
while index != -1:
leftNumber, rightNumber = self.__getNumbers(self.chain, index)
result = self.__operateNums(
float(leftNumber), self.chain[index], float(rightNumber)
)
print(leftNumber, self.chain[index], rightNumber, "=", result)
self.chain = (
self.chain[: index - len(leftNumber)]
+ str(result)
+ self.chain[index + len(rightNumber) + 1 :]
)
return self.calculate(result=result)
if index == -1:
result = float(self.chain)
if isinstance(result, (float, int)) and result.is_integer():
return int(result)
else:
return round(float(result), 5)
else:
return "Err"
def __checkSyntax(self):
"""
Checking if the current operations can be made:
- No number before/after parenthesis --> Self-corrects
- No repeated negative signs (--2 --> 2) --> Self-corrects
- Same number of opening and closing parenthesis
- At least a digit inside the parenthesis
- Correct position of decimal points (and no repeted points, 3.1.43)
- No operator on the first position (expect minus sign)
- No operators together (expect negative sign)
- No operator at the end of the string
"""
self.color = 0
if len(self.chain) == 0:
return
# No repeated negative signs (--2 --> 2) --> Self-corrects
if len(self.chain) >= 2:
for i in range(len(self.chain) - 1):
if self.chain[i] == "-" and self.chain[i + 1] == "-":
self.chain = self.chain[:i] + self.chain[i + 2 :]
break
# No number before/after parenthesis --> Self-corrects
index = 0
for i in range(self.chain.count("(")):
index = self.chain.find("(", index + 1)
if index - 1 >= 0:
if (
self.chain[index - 1] >= "0" and self.chain[index - 1] <= "9"
) or self.chain[index - 1] == ")":
self.chain = self.chain[:index] + "*" + self.chain[index:]
break
index = 0
for i in range(self.chain.count(")")):
index = self.chain.find(")", index + 1)
if index + 1 < len(self.chain):
if (
self.chain[index + 1] >= "0" and self.chain[index + 1] <= "9"
) or self.chain[index + 1] == "(":
self.chain = self.chain[: index + 1] + "*" + self.chain[index + 1 :]
break
# Checking for correct pair parenthesis use
i = 0
while i < len(self.chain):
i = self.chain.find("(", i)
if i == -1:
break
if ")" not in self.chain[i:]:
self.color = 13
return False
i += 1
if self.chain.count("(") != self.chain.count(")"):
self.color = 13
return False
# At least a digit inside the parenthesis: Searching for 'd)', where d is any digit
i = 0
while i < len(self.chain):
i = self.chain.find(")", i)
if i == -1:
break
if i - 1 >= 0:
if self.chain[i - 1] == ")":
# i += 1
break
if not (self.chain[i - 1] >= "0" and self.chain[i - 1] <= "9"):
self.color = 13
return False
i += 1
# Correct decimal points place
for i in range(len(self.chain) - 1):
if self.chain[i] == ".":
# Checking de adjacent characters
for char in self.chain[i + 1 :]:
if char in ("(", ")", "+", "-", "*", "/"):
# Finished loop, correct characters
break
elif char == ".":
self.color = 13
return False
for char in self.chain[:i][::-1]:
if char in ("(", ")", "+", "-", "*", "/"):
# Finished loop, correct characters
break
elif char == ".":
self.color = 13
return False
# No operator on the first place (minus signs can be)
if len(self.chain) > 0 and self.chain[0] in ("+", "*", "/"):
self.color = 13
return False
# No operators together (expect negative sign)
index, prevIndex = 0, 0
for i in range(len(self.chain) - 1):
if self.chain[i] in ("+", "-", "*", "/"):
operators = ["+", "-", "*", "/"]
if self.chain[i] in ("*", "/"):
operators = ["+", "*", "/"]
if self.chain[i + 1] in operators:
print("near index", i, self.chain[i], self.chain[i + 1])
self.color = 13
return False
prevIndex = index
# No operator at the end of the string
if len(self.chain) > 0 and self.chain[-1] in ("*", "/", "+", "-"):
self.color = 13
return False
return True
def __addStr(self, val):
"""Adds a string 'val' at the position of the cursor"""
if self.index == 0:
self.chain += val
pos = -1
else:
pos = len(self.chain) - self.index
self.chain = self.chain[:pos] + val + self.chain[pos:]
def calc(self):
originalChain = self.chain
result = self.calculate(result=0)
if result != "Err":
self.pastOperations.append((originalChain, str(result)))
print("\nResult:", result)
self.chain = str(result)
# Moves the cursor to its corresponding position on the new chain
self.checkCursor()
else:
print("Error trying to calculate", self.chain)
def __operateNums(self, num1: float, operator, num2: float):
match operator:
case "+":
return num1 + num2
case "-":
return num1 - num2
case "*":
return num1 * num2
case "/":
return num1 / num2
case _:
return "Err"
def __getNumbers(self, chain, index):
# Parsing left number
leftStart = 1
leftNumber = chain[index - leftStart : index]
while isPartOfNum(leftNumber) and index - leftStart >= 0:
leftStart += 1
leftNumber = chain[index - leftStart : index]
# We need to go back to the previous number (either cause it was out of the string or it no longer was a num)
leftNumber = chain[index - (leftStart - 1) : index]
# Parsing right number
rightEnd = 1
rightStart = index + 1
rightNumber = chain[rightStart : rightStart + rightEnd]
while isPartOfNum(rightNumber) and rightStart + rightEnd <= len(chain):
rightEnd += 1
rightNumber = chain[rightStart : rightStart + rightEnd]
# We need to go back to the previous number (either cause it was out of the string or it no longer was a num)
rightNumber = chain[rightStart : rightStart + rightEnd - 1]
return leftNumber, rightNumber
def __findOperator(self, chain):
"""Finds next operator, following PEMDAS rules"""
for op in ("*", "/", "+", "-"):
if op in chain:
if op == "-":
if chain.find(op) != 0:
return chain.find(op)
else:
# Trying to search for another (-) sign (as it's the last obj, it can output -1 or the correct index)
return chain.find(op, 1)
else:
return chain.find(op)
return -1
def __findSubChain(self, chain):
# Taking into account we already know there's at least a group of parenthesis
startIndex = chain.find("(")
endIndex = chain.find(")", startIndex + 1)
subchain = chain[startIndex : endIndex + 1]
# Checking one by one, taking into account there can't be any spare '(' on between (like '((3*2)-3)'
while subchain.count("(") != subchain.count(")") and endIndex < len(chain):
endIndex = chain.find(")", endIndex + 1)
subchain = chain[startIndex : endIndex + 1]
return chain[startIndex + 1 : endIndex], startIndex
def draw(self):
w = len(self.chain) * 4
x = pyxel.width - 5 - w
pyxel.text(x, 8, self.chain, self.color)
# Drawing cursor
if np.sin(pyxel.frame_count / 4) <= -0.4 or self.index != self.prevIndex:
cursorX = pyxel.width - 6 - (self.index * 4)
pyxel.line(cursorX, 6, cursorX, 14, 7)
self.prevIndex = self.index
# Drawing the past 3 operations
y = 16
operations = self.pastOperations[::-1]
for operation in operations[:3]:
opLength = len(operation[0]) + len("=") + len(operation[1])
x = pyxel.width - opLength * 4 - 6
pyxel.text(x, y, operation[0] + "=" + operation[1], 4)
y += 7
def checkCursor(self):
# Moving cursor's position
if (
pyxel.btn(pyxel.KEY_LEFT)
and time.time() - self.lastPressed > self.keyCooldown
):
self.prevIndex = self.index
self.lastPressed = time.time()
self.index = min(len(self.chain), self.index + 1)
elif (
pyxel.btn(pyxel.KEY_RIGHT)
and time.time() - self.lastPressed > self.keyCooldown
):
self.prevIndex = self.index
self.lastPressed = time.time()
self.index = max(0, self.index - 1)
elif pyxel.btnp(pyxel.KEY_UP):
# Goes completely to the left
self.prevIndex = self.index
self.index = len(self.chain)
elif pyxel.btnp(pyxel.KEY_DOWN):
# Goes completely to the right
self.prevIndex = self.index
self.index = 0
# Double checking, in case no key was pressed
self.index = min(max(0, self.index), len(self.chain))
def delete(self):
"""Deletes a character at the pointer's position"""
if self.index == 0:
self.chain = self.chain[:-1]
elif self.index != len(self.chain):
i = len(self.chain) - 1 - self.index
self.chain = self.chain[:i] + self.chain[i + 1 :]
self.__checkSyntax()
def clear(self):
self.chain = ""
self.index = 0
print()
def __str__(self):
txt = f"Display: \n Current: {self.chain}\n History:\n "
for operation in self.pastOperations:
txt += f"{operation}\n"
return txt