-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
54 lines (46 loc) · 2.12 KB
/
main.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
from CPUClass import CPU
initCpu = CPU()
print("Hi welcome to the Arithmetic CPU where you can send out mathematically instructions to a fake python CPU. So exciting right?")
print("To start trying adding 10 to the memory address 1: store 10 1")
print("TO EXIT PLEASE TYPE: BYE")
def runControlUnit():
instruction = input(
"Please enter your instructions, if you're lost please read the read me!\n")
if instruction.lower() == "bye":
quit()
# A lot of error handling to prevent many things from going wrong and giving the user feedback.
try:
result = initCpu.controlUnit(instruction)
except Exception as e:
if str(e) == "Invalid length of instruction":
instruction = input(
"Invalid length of instruction please read the read me and try again\n")
runControlUnit()
elif str(e) == "Invalid instruction":
instruction = input(
"Invalid instruction please read the read me and try again\n")
runControlUnit()
elif str(e) == "Divied by zero":
instruction = input(
"Divied by zero please pick a different store or store a none zero variable\n")
runControlUnit()
elif str(e) == "Invalid Store":
instruction = input("Invalid store address! please try again\n")
runControlUnit()
elif str(e) == "No variable":
instruction = input(
"No variable stored there! Please store one or try a different store\n")
runControlUnit()
elif isinstance(e, ValueError) and "invalid literal for int()" in str(e):
instruction = input(
"Invalid input: expected a number but got a string or float/decimal. Please try again.\n")
runControlUnit()
elif isinstance(e, IndexError):
instruction = input(
"Index error: this means you added a semicolon to the end of a query. Don't do that, it's wrong stop. Try again!\n")
runControlUnit()
else:
raise Exception(e)
print(result)
runControlUnit()
runControlUnit()