-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.py
143 lines (141 loc) · 4.54 KB
/
operations.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
def perform_operation(oper: str,stack: [], m: [], func_map: {}):
if oper == 'def':
check_if_enough_stack(stack, 1, oper)
check_valid_types(stack, [list])
func = stack.pop()
func_map[func[0]] = func[1:]
m.pop(0)
elif oper in {">", "<", "=="}:
check_if_enough_stack(stack, 2, oper)
check_valid_types(stack, [int, int])
last = stack.pop()
pre_last = stack.pop()
math_oper = {
">": pre_last > last,
"==": pre_last == last,
"<": pre_last < last
}
stack.append(math_oper[oper])
m.pop(0)
elif oper == "if":
check_if_enough_stack(stack, 2, oper)
check_valid_types(stack, [list, list])
m.pop(0)
false_branch, true_branch, result = stack.pop(), stack.pop(), stack.pop()
m[:0] = true_branch if result else false_branch
elif oper == "first":
check_if_enough_stack(stack, 1, oper)
check_valid_types(stack, [list])
stack_top = stack.pop()
if len(stack_top) < 1:
print(f"Not enough elements inside array for first operation. Stack: {stack}")
exit(1)
stack.append(stack_top[0])
m.pop(0)
elif oper == "rest":
check_if_enough_stack(stack, 1, oper)
check_valid_types(stack, [list])
stack_top = stack.pop()
if len(stack_top) < 1:
print(f"Not enough elements inside array for rest operation. Stack: {stack}")
exit(1)
sliced_stack = stack_top[1:]
stack.append(sliced_stack)
m.pop(0)
elif oper == "cons":
check_if_enough_stack(stack, 2, oper)
check_valid_types(stack, [list, int])
stack_top = stack.pop()
sliced_stack = [stack.pop()] + stack_top
stack.append(sliced_stack)
m.pop(0)
# no type check needed
elif oper == "dup":
check_if_enough_stack(stack, 1, oper)
stack.append(stack[-1])
m.pop(0)
# no type check needed
elif oper == "drop":
check_if_enough_stack(stack,1, oper)
stack.pop()
m.pop(0)
# no type check needed
elif oper == "swap":
check_if_enough_stack(stack, 2, oper)
last = stack.pop()
pre_last = stack.pop()
stack.append(last)
stack.append(pre_last)
m.pop(0)
elif oper in ["+", "-", "*", "/"]:
check_if_enough_stack(stack, 2, oper)
check_valid_types(stack, [int, int])
a, b = stack.pop(), stack.pop()
math_oper = {
"+": a + b,
"-": b - a,
"*": a * b,
"/": b / a
}
stack.append(math_oper[oper])
m.pop(0)
elif oper == "rot":
check_if_enough_stack(stack, 3, oper)
last = stack.pop()
stack.insert(-2, last)
m.pop(0)
elif oper == "null":
check_if_enough_stack(stack, 1, oper)
check_valid_types(stack, [list])
last = stack.pop()
stack.append(len(last) == 0)
m.pop(0)
elif oper == "stop":
print("Stop oper")
exit(1)
elif oper == "dip":
m.pop(0)
action = stack.pop()
last = stack.pop()
m.insert(0, last)
m[:0] = action
elif oper == "rolldown": # eq to rot rot
# X Y Z -> Y Z X moves y and z down, moves x up
m.pop(0)
z = stack.pop()
y = stack.pop()
x = stack.pop()
stack.append(y)
stack.append(z)
stack.append(x)
elif oper == 'i':
m.pop(0)
check_if_enough_stack(stack, 2, oper)
check_valid_types(stack, [list])
l = stack.pop()
if len(l) < 1:
print(f"Not enough elements inside array for rest operation. Stack: {stack}")
exit(1)
m[:0] = l
elif oper in func_map:
m.pop(0)
m[:0] = func_map[oper]
else:
print(func_map)
print(f"No operation found: {oper} {repr(oper)}")
exit(1)
def check_if_enough_stack(stack:[], required: int, operation: str):
if len(stack) < required:
print(f"Not enough elements on stack for operation {operation}. \n Stack: {stack}")
exit(1)
# 12 1 [1 2 3]
def check_valid_types(stack:[], types_array: [type]):
# iterable
for i, elem in enumerate(types_array):
if len(stack) == 0:
print("Stack len equal zero")
exit(1)
stack_type = type(stack[len(stack) - 1 - i])
if stack_type != elem:
print(f"Incorrect type on stack, expected {elem}, received {stack_type}")
exit(1)