-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
224 lines (146 loc) · 3.91 KB
/
commands.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
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
stack = []
memory = [0 for i in range(256)]
# check the stack has enough items, and their type, then pop them
def typecheckandpop(length, funcname, typecheck=True):
if len(stack) < length:
print(funcname + " Error: Too few items on stack")
return []
if typecheck:
for i in range(length):
if not isinstance(stack[-i - 1], int):
print(funcname, "Error:", stack[-1], "is not of type int.")
return []
ret = [stack.pop() for i in range(length)]
return ret
def POP():
if not (ret := typecheckandpop(1, "POP", False)):
return # short circuit if stack is empty
return ret[0]
# only used internally for now, in code it's just #100 for example
def PUSH(i):
stack.append(i)
def ADD():
if not (ret := typecheckandpop(2, "ADD")):
return
PUSH(ret[0] + ret[1])
def SUB():
if not (ret := typecheckandpop(2, "SUB")):
return
PUSH(ret[1] - ret[0])
def MUL():
if not (ret := typecheckandpop(2, "MUL")):
return
PUSH(ret[1] * ret[0])
def DIV():
if not (ret := typecheckandpop(2, "DIV")):
return
# short circuit on divide by 0
if ret[0] == 0:
print("DIV Error: Divide by 0")
PUSH(ret[1])
PUSH(ret[0])
return
PUSH(ret[1] // ret[0])
def SFT():
if not (ret := typecheckandpop(2, "SFT")):
return
left = (ret[0] & 0xF0) >> 4
right = ret[0] & 0x0F
ret[1] = ret[1] >> right
ret[1] = ret[1] << left
PUSH(ret[1])
def MOD():
if not (ret := typecheckandpop(2, "MOD")):
return
# short circuit on divide by 0
if ret[0] == 0:
print("MOD Error: Divide by 0")
PUSH(ret[1])
PUSH(ret[0])
return
PUSH(ret[1] % ret[0])
def DUP():
if not (ret := typecheckandpop(1, "DUP", False)):
return
PUSH(ret[0])
PUSH(ret[0])
def SWP():
if not (ret := typecheckandpop(2, "SWP", False)):
return
PUSH(ret[0])
PUSH(ret[1])
def STA():
if not (ret := typecheckandpop(2, "STA")):
return
if ret[0] < 0 or ret[0] > 255:
print("STA Error: Address out of range")
return # only allowing 255 vars for now
memory[ret[0]] = ret[1]
def LDA():
if not (ret := typecheckandpop(1, "LDA")):
return
if ret[0] < 0 or ret[0] > 255:
print("STA Error: Address out of range")
return # only allowing 255 vars for now
PUSH(memory[ret[0]])
def INC():
if not (ret := typecheckandpop(1, "INC")):
return
PUSH(ret[0] + 1)
def DEC():
if not (ret := typecheckandpop(1, "DEC")):
return
PUSH(ret[0] - 1)
def NIP():
if not (ret := typecheckandpop(2, "NIP", False)):
return
# throw away ret[1]
PUSH(ret[0])
# a b -- a b a
def OVR():
if not (ret := typecheckandpop(2, "OVR", False)):
return
PUSH(ret[1])
PUSH(ret[0])
PUSH(ret[1])
# a b c -- b c a
def ROT():
if not (ret := typecheckandpop(3, "ROT", False)):
return
PUSH(ret[1])
PUSH(ret[0])
PUSH(ret[2])
def EQU():
if not (ret := typecheckandpop(2, "EQU", False)):
return
PUSH(int(ret[1] == ret[0]))
def NEQ():
if not (ret := typecheckandpop(2, "NEQ", False)):
return
PUSH(int(ret[1] != ret[0]))
def GTH():
if not (ret := typecheckandpop(2, "GTH")):
return
PUSH(int(ret[1] > ret[0]))
def LTH():
if not (ret := typecheckandpop(2, "LTH")):
return
PUSH(int(ret[1] < ret[0]))
def AND():
if not (ret := typecheckandpop(2, "AND")):
return
PUSH(ret[1] & ret[0])
def ORA():
if not (ret := typecheckandpop(2, "ORA")):
return
PUSH(ret[1] | ret[0])
def EOR():
if not (ret := typecheckandpop(2, "EOR")):
return
PUSH(ret[1] ^ ret[0])
# I like this name better. Probably confusing to have two...
def XOR():
EOR()
# clear the stack
def CLS():
stack.clear()