dry, black, basic type hints, catch ctrl-d and ctrl-c

This commit is contained in:
James Greenwood 2025-08-30 11:33:20 +01:00
parent 26aaabe6a7
commit f512069fc7

View File

@ -1,29 +1,27 @@
import operator
import readline
from typing import Callable
class FifthStack:
def __init__(self):
self.stack = []
self.commands = {
'push': self.push,
'pop': self.pop,
'swap': self.swap,
'dup': self.dup,
'+': self.add,
'-': self.sub,
'*': self.mul,
'/': self.div,
self.stack: list[int] = []
self.commands: dict[str, Callable] = {
"push": self.push,
"pop": self.pop,
"swap": self.swap,
"dup": self.dup,
}
self.binary_ops = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.floordiv,
self.binary_ops: dict[str, Callable[[int, int], int]] = {
"+": operator.add,
"-": operator.sub,
"*": operator.mul,
"/": operator.floordiv,
}
def _require(self, number, message):
if len(self.stack) < number:
print(message)
print(f"ERROR: {message}")
return False
return True
@ -33,11 +31,11 @@ class FifthStack:
def _binary_op(self, operator):
if not self._require(2, "two numbers required"):
return
a, b = self.stack.pop(), self.stack.pop()
b, a = self.stack.pop(), self.stack.pop()
try:
self.stack.append(operator(a, b))
except Exception as e:
print(f"ERROR: {e}")
except ZeroDivisionError as e:
print(f"ERROR: division by zero")
self.stack.extend([a, b])
def push(self, value):
@ -46,34 +44,22 @@ class FifthStack:
except ValueError:
print("ERROR: integer required")
def pop(self, _value):
def pop(self):
if not self.stack:
print("ERROR: stack empty")
return
self.stack.pop()
def swap(self, _value):
if not self._require(2, 'two numbers required'):
def swap(self):
if not self._require(2, "two numbers required"):
return
self.stack[-1], self.stack[-2] = self.stack[-2], self.stack[-1]
def dup(self, _value):
if self._require(1, 'stack empty'):
def dup(self):
if self._require(1, "stack empty"):
self.stack.append(self.stack[-1])
def add(self, _value):
self._binary_op(self._get_binary_op('+'))
def sub(self, _value):
self._binary_op(self._get_binary_op('-'))
def mul(self, _value):
self._binary_op(self._get_binary_op('*'))
def div(self, _value):
self._binary_op(self._get_binary_op('/'))
def execute(self, command):
def execute(self, command: str):
tokens = command.lower().strip().split()
if not tokens:
return
@ -81,21 +67,30 @@ class FifthStack:
command, argument = tokens[0], tokens[1] if len(tokens) > 1 else None
function = self.commands.get(command)
try:
if function: function(argument)
else: print("ERROR: unknown command")
except:
print("ERROR: invalid command")
if function:
if command == "push":
function(argument)
else:
function()
elif command in self.binary_ops:
self._binary_op(self._get_binary_op(command))
else:
print("ERROR: unknown command")
def main():
fifth = FifthStack()
while True:
print(f'stack is: {fifth.stack}')
command = input()
if command.lower() == 'exit':
print(f"stack is: {fifth.stack}")
try:
if command := input().strip().lower() == "exit":
break
if command:
fifth.execute(command)
except (EOFError, KeyboardInterrupt):
break
if __name__ == '__main__':
if __name__ == "__main__":
main()