-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandleFunctions.py
162 lines (132 loc) · 3.73 KB
/
handleFunctions.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
import settings as s
from constants import *
def handleInput(typeOfInput):
# Receives a string
if typeOfInput == typeChar:
inp = input('>>')
for i in inp:
try:
s.stack.append(ord(i))
except:
print("Not and ordinal number :", i, "str :",inp)
# Receives a int
elif typeOfInput == typeInt:
inp = input('>>')
try:
s.stack.append(int(inp))
except:
print("Invalid integer value", inp)
exit(1)
s.point += 1
def handleOutput(stackPointer, typeOfOutput):
item = s.stack[int(stackPointer)]
if typeOfOutput == typeChar:
print(chr(item),end='')
elif typeOfOutput == typeInt:
print(item,end='')
s.point += 1
def handleStackPop(val):
try:
s.stack.pop(int(val))
s.point += 1
except IndexError:
print('Pop from empty stack at', val)
exit(1)
def handleStackPush(val):
# Maybe enable any type of push?
s.stack.append(int(val))
s.point += 1
def handleIncrease(line):
add = 0
try:
if line[2][0] == sPointer:
add = s.stack[int(line[2][1:])]
else:
add = int(line[2])
s.stack[int(line[1])] = s.stack[int(line[1])] + add
s.point += 1
except IndexError:
print("Invalid decrease at line", s.point)
exit(0)
def handleDecrease(line):
add = 0
try:
if line[2][0] == sPointer:
add = s.stack[int(line[2][1:])]
else:
add = int(line[2])
s.stack[int(line[1])] = s.stack[int(line[1])] - add
s.point += 1
except IndexError:
print("Invalid decrease at line", s.point)
exit(0)
def handleSet(line):
val = 0
try:
if line[2][0] == sPointer:
val = s.stack[int(line[2][1:])]
else:
val = int(line[2])
s.stack[int(line[1])] = val
s.point += 1
except IndexError:
print("Invalid set at line", s.point)
exit(0)
def handleIf(line, typeOfCondition):
try:
if line[2][0] == sPointer:
check = s.stack[int(line[2][1:])]
else:
check = int(line[2])
if typeOfCondition == cEqual:
if s.stack[int(line[1])] == check:
handleGoto(line[4])
else:
s.point += 1
if typeOfCondition == cNotEqual:
if s.stack[int(line[1])] != check:
handleGoto(line[4])
else:
s.point += 1
if typeOfCondition == cBigger:
if s.stack[int(line[1])] > check:
handleGoto(line[4])
else:
s.point += 1
if typeOfCondition == cSmaller:
if s.stack[int(line[1])] < check:
handleGoto(line[4])
else:
s.point += 1
except IndexError:
print("Invalid if/nif/bif/sif:", line, typeOfCondition, "at line", s.point)
def handleGoto(goto):
try:
indexToJump = int(goto)
s.point = indexToJump
return
except ValueError:
pass
try:
indexToJump = s.gotoMap[goto]
s.point = indexToJump
return
except KeyError:
print("Invalid goto index or label at line", s.point)
exit(1)
def handlePrintStack(typeOfOutput):
for x in s.stack:
if typeOfOutput:
if typeOfOutput == typeInt:
print(x, end='')
else:
print(chr(x), end='')
print()
s.point += 1
def handleStackSwitch(stack):
try:
s.stack = s.dictOfStacks[stack]
except KeyError:
s.dictOfStacks[stack] = []
s.stack = s.dictOfStacks[stack]
s.point += 1