2018-02-12 18:58:50 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-02-12 16:01:48 +01:00
|
|
|
|
|
|
|
# Nga: a Virtual Machine
|
2019-01-03 15:18:17 +01:00
|
|
|
# Copyright (c) 2010 - 2019, Charles Childers
|
2020-09-28 19:50:47 +02:00
|
|
|
# Floating Point I/O by Arland Childers, (c) 2020
|
2018-02-12 16:01:48 +01:00
|
|
|
# Optimizations and process() rewrite by Greg Copeland
|
|
|
|
# -----------------------------------------------------
|
|
|
|
|
|
|
|
import os, sys, math, time, struct
|
|
|
|
from struct import pack, unpack
|
|
|
|
|
|
|
|
ip = 0
|
|
|
|
stack = [] * 128
|
|
|
|
address = []
|
|
|
|
memory = []
|
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2020-09-28 18:49:09 +02:00
|
|
|
class FloatStack(object):
|
|
|
|
def __init__(self, *d):
|
|
|
|
self.data = list(d)
|
|
|
|
|
|
|
|
def __getitem__(self, id):
|
|
|
|
return self.data[id]
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
def add(self):
|
|
|
|
self.data.append(self.data.pop() + self.data.pop())
|
|
|
|
|
|
|
|
def sub(self):
|
|
|
|
self.data.append(0 - (self.data.pop() - self.data.pop()))
|
|
|
|
|
|
|
|
def mul(self):
|
|
|
|
self.data.append(self.data.pop() * self.data.pop())
|
|
|
|
|
|
|
|
def div(self):
|
|
|
|
a, b = self.data.pop(), self.data.pop()
|
|
|
|
self.data.append(b / a)
|
|
|
|
|
|
|
|
def ceiling(self):
|
|
|
|
self.data.append(math.ceil(self.data.pop()))
|
|
|
|
|
|
|
|
def floor(self):
|
|
|
|
self.data.append(math.floor(self.data.pop()))
|
|
|
|
|
|
|
|
def eq(self):
|
2020-09-28 19:50:47 +02:00
|
|
|
return 0 - (self.data.pop() == self.data.pop())
|
2020-09-28 18:49:09 +02:00
|
|
|
|
|
|
|
def neq(self):
|
2020-09-28 19:50:47 +02:00
|
|
|
return 0 - (self.data.pop() != self.data.pop())
|
2020-09-28 18:49:09 +02:00
|
|
|
|
|
|
|
def gt(self):
|
|
|
|
a, b = self.data.pop(), self.data.pop()
|
2020-09-28 19:50:47 +02:00
|
|
|
return 0 - (b > a)
|
2020-09-28 18:49:09 +02:00
|
|
|
|
|
|
|
def lt(self):
|
|
|
|
a, b = self.data.pop(), self.data.pop()
|
2020-09-28 19:50:47 +02:00
|
|
|
return 0 - (b < a)
|
2020-09-28 18:49:09 +02:00
|
|
|
|
|
|
|
def depth(self):
|
|
|
|
return len(self.data)
|
|
|
|
|
|
|
|
def drop(self):
|
|
|
|
self.data.pop()
|
|
|
|
|
2020-09-28 19:50:47 +02:00
|
|
|
def pop(self):
|
|
|
|
return self.data.pop()
|
|
|
|
|
2020-09-28 18:49:09 +02:00
|
|
|
def swap(self):
|
|
|
|
a, b = self.data.pop(), self.data.pop()
|
|
|
|
self.data += [a, b]
|
|
|
|
|
|
|
|
def push(self, n):
|
|
|
|
self.data.append(n)
|
|
|
|
|
|
|
|
def log(self):
|
|
|
|
a, b = self.data.pop(), self.data.pop()
|
|
|
|
self.data.append(math.log(b, a))
|
|
|
|
|
|
|
|
def power(self):
|
|
|
|
a, b = self.data.pop(), self.data.pop()
|
|
|
|
self.data.append(math.pow(a, b))
|
|
|
|
|
|
|
|
def sin(self):
|
|
|
|
self.data.append(math.sin(self.data.pop()))
|
|
|
|
|
|
|
|
def cos(self):
|
|
|
|
self.data.append(math.cos(self.data.pop()))
|
|
|
|
|
|
|
|
def tan(self):
|
|
|
|
self.data.append(math.tan(self.data.pop()))
|
|
|
|
|
|
|
|
def asin(self):
|
|
|
|
self.data.append(math.asin(self.data.pop()))
|
|
|
|
|
|
|
|
def acos(self):
|
|
|
|
self.data.append(math.acos(self.data.pop()))
|
|
|
|
|
|
|
|
def atan(self):
|
|
|
|
self.data.append(math.atan(self.data.pop()))
|
|
|
|
|
|
|
|
|
2020-09-28 19:50:47 +02:00
|
|
|
floats = FloatStack()
|
|
|
|
afloats = FloatStack()
|
|
|
|
|
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
def rxDivMod(a, b):
|
2018-02-13 18:13:01 +01:00
|
|
|
x = abs(a)
|
|
|
|
y = abs(b)
|
|
|
|
q, r = divmod(x, y)
|
|
|
|
if a < 0 and b < 0:
|
|
|
|
r *= -1
|
|
|
|
elif a > 0 and b < 0:
|
|
|
|
q *= -1
|
|
|
|
elif a < 0 and b > 0:
|
|
|
|
r *= -1
|
|
|
|
q *= -1
|
|
|
|
return q, r
|
2018-02-12 16:01:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def findEntry(named):
|
|
|
|
header = memory[2]
|
|
|
|
Done = False
|
2020-09-22 14:06:40 +02:00
|
|
|
while header != 0 and not Done:
|
2018-02-12 16:01:48 +01:00
|
|
|
if named == extractString(header + 3):
|
|
|
|
Done = True
|
|
|
|
else:
|
|
|
|
header = memory[header]
|
|
|
|
return header
|
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 16:01:48 +01:00
|
|
|
def rxGetInput():
|
|
|
|
return ord(sys.stdin.read(1))
|
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-11-22 04:03:57 +01:00
|
|
|
def rxDisplayCharacter():
|
2018-02-12 16:01:48 +01:00
|
|
|
global stack
|
2018-11-22 04:03:57 +01:00
|
|
|
if stack[-1] > 0 and stack[-1] < 128:
|
|
|
|
if stack[-1] == 8:
|
|
|
|
sys.stdout.write(chr(stack.pop()))
|
|
|
|
sys.stdout.write(chr(32))
|
|
|
|
sys.stdout.write(chr(8))
|
2018-02-13 18:13:01 +01:00
|
|
|
else:
|
2018-11-22 04:03:57 +01:00
|
|
|
sys.stdout.write(chr(stack.pop()))
|
2018-02-12 16:01:48 +01:00
|
|
|
else:
|
2018-11-22 04:03:57 +01:00
|
|
|
sys.stdout.write("\033[2J\033[1;1H")
|
|
|
|
stack.pop()
|
|
|
|
sys.stdout.flush()
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_no():
|
|
|
|
pass
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_li():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
ip += 1
|
2020-09-22 14:06:40 +02:00
|
|
|
stack.append(memory[ip])
|
|
|
|
|
2018-02-13 18:13:01 +01:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_du():
|
|
|
|
global ip, memory, stack, address
|
2020-09-22 14:06:40 +02:00
|
|
|
stack.append(stack[-1])
|
|
|
|
|
2018-02-13 18:13:01 +01:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_dr():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
stack.pop()
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_sw():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
a = stack[-2]
|
|
|
|
stack[-2] = stack[-1]
|
|
|
|
stack[-1] = a
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_pu():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
address.append(stack.pop())
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_po():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
stack.append(address.pop())
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_ju():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
ip = stack.pop() - 1
|
2018-02-13 18:13:01 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_ca():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
address.append(ip)
|
|
|
|
ip = stack.pop() - 1
|
2018-02-13 18:13:01 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_cc():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
target = stack.pop()
|
|
|
|
if stack.pop() != 0:
|
|
|
|
address.append(ip)
|
|
|
|
ip = target - 1
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_re():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
ip = address.pop()
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_eq():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
a = stack.pop()
|
|
|
|
b = stack.pop()
|
|
|
|
if b == a:
|
|
|
|
stack.append(-1)
|
|
|
|
else:
|
|
|
|
stack.append(0)
|
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_ne():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
a = stack.pop()
|
|
|
|
b = stack.pop()
|
|
|
|
if b != a:
|
|
|
|
stack.append(-1)
|
|
|
|
else:
|
|
|
|
stack.append(0)
|
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_lt():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
a = stack.pop()
|
|
|
|
b = stack.pop()
|
|
|
|
if b < a:
|
|
|
|
stack.append(-1)
|
|
|
|
else:
|
|
|
|
stack.append(0)
|
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_gt():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
a = stack.pop()
|
|
|
|
b = stack.pop()
|
|
|
|
if b > a:
|
|
|
|
stack.append(-1)
|
|
|
|
else:
|
|
|
|
stack.append(0)
|
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_fe():
|
|
|
|
global ip, memory, stack, address
|
2020-09-22 14:06:40 +02:00
|
|
|
if stack[-1] == -1:
|
|
|
|
stack[-1] = len(stack) - 1
|
|
|
|
elif stack[-1] == -2:
|
|
|
|
stack[-1] = len(address)
|
|
|
|
elif stack[-1] == -3:
|
|
|
|
stack[-1] = len(memory)
|
|
|
|
elif stack[-1] == -4:
|
|
|
|
stack[-1] = -2147483648
|
|
|
|
elif stack[-1] == -5:
|
|
|
|
stack[-1] = 2147483647
|
|
|
|
else:
|
|
|
|
stack[-1] = memory[stack[-1]]
|
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
|
|
|
|
def i_st():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
mi = stack.pop()
|
|
|
|
memory[mi] = stack.pop()
|
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_ad():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
t = stack.pop()
|
2020-09-22 14:06:40 +02:00
|
|
|
stack[-1] += t
|
|
|
|
stack[-1] = unpack("=l", pack("=L", stack[-1] & 0xFFFFFFFF))[0]
|
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
|
|
|
|
def i_su():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
t = stack.pop()
|
|
|
|
stack[-1] -= t
|
2020-09-22 14:06:40 +02:00
|
|
|
stack[-1] = unpack("=l", pack("=L", stack[-1] & 0xFFFFFFFF))[0]
|
|
|
|
|
2018-02-13 18:13:01 +01:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_mu():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
t = stack.pop()
|
|
|
|
stack[-1] *= t
|
2020-09-22 14:06:40 +02:00
|
|
|
stack[-1] = unpack("=l", pack("=L", stack[-1] & 0xFFFFFFFF))[0]
|
|
|
|
|
2018-02-13 18:13:01 +01:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_di():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
a = stack[-1]
|
|
|
|
b = stack[-2]
|
2020-09-22 14:06:40 +02:00
|
|
|
stack[-1], stack[-2] = rxDivMod(b, a)
|
|
|
|
stack[-1] = unpack("=l", pack("=L", stack[-1] & 0xFFFFFFFF))[0]
|
|
|
|
stack[-2] = unpack("=l", pack("=L", stack[-2] & 0xFFFFFFFF))[0]
|
|
|
|
|
2018-02-13 18:13:01 +01:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_an():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
t = stack.pop()
|
|
|
|
stack[-1] &= t
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_or():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
t = stack.pop()
|
|
|
|
stack[-1] |= t
|
2018-02-13 18:13:01 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_xo():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
t = stack.pop()
|
|
|
|
stack[-1] ^= t
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_sh():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
t = stack.pop()
|
2020-09-21 15:31:21 +02:00
|
|
|
if t < 0:
|
2020-09-22 14:06:40 +02:00
|
|
|
stack[-1] <<= t * -1
|
2020-09-21 15:31:21 +02:00
|
|
|
else:
|
|
|
|
stack[-1] >>= t
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 18:58:50 +01:00
|
|
|
def i_zr():
|
|
|
|
global ip, memory, stack, address
|
|
|
|
if stack[-1] == 0:
|
|
|
|
stack.pop()
|
|
|
|
ip = address.pop()
|
2018-02-13 18:13:01 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2019-12-10 19:51:56 +01:00
|
|
|
def i_ha():
|
2018-02-12 18:58:50 +01:00
|
|
|
global ip, memory, stack, address
|
|
|
|
ip = 9000000
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-11-22 04:03:57 +01:00
|
|
|
def i_ie():
|
2020-09-28 19:50:47 +02:00
|
|
|
stack.append(2)
|
2018-11-22 04:03:57 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-11-22 04:03:57 +01:00
|
|
|
def i_iq():
|
2020-09-28 18:49:09 +02:00
|
|
|
device = stack.pop()
|
|
|
|
if device == 0: # generic output
|
2020-09-28 19:50:47 +02:00
|
|
|
stack.append(0)
|
|
|
|
stack.append(0)
|
2020-09-28 18:49:09 +02:00
|
|
|
if device == 1: # floating point
|
2020-09-28 19:50:47 +02:00
|
|
|
stack.append(1)
|
|
|
|
stack.append(2)
|
2018-11-22 04:03:57 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-11-22 04:03:57 +01:00
|
|
|
def i_ii():
|
2020-09-28 18:49:09 +02:00
|
|
|
device = stack.pop()
|
|
|
|
if device == 0: # generic output
|
|
|
|
rxDisplayCharacter()
|
|
|
|
if device == 1: # floating point
|
2020-09-28 19:50:47 +02:00
|
|
|
action = stack.pop()
|
|
|
|
print(floats.data, action)
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 0: # number to float
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.push(float(stack.pop()))
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 1: # string to float
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.push(float(extractString(stack.pop())))
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 2: # float to number
|
2020-09-28 19:50:47 +02:00
|
|
|
stack.append(int(floats.pop()))
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 3: # float to string
|
2020-09-28 19:50:47 +02:00
|
|
|
injectString(str(floats.pop()), stack.pop())
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 4: # add
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.add()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 5: # sub
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.sub()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 6: # mul
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.mul()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 7: # div
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.div()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 8: # floor
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.floor()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 9: # ceil
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.ceiling()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 10: # sqrt
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.sqrt()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 11: # eq
|
2020-09-28 19:50:47 +02:00
|
|
|
stack.append(floats.eq())
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 12: # -eq
|
2020-09-28 19:50:47 +02:00
|
|
|
stack.append(floats.neq())
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 13: # lt
|
2020-09-28 19:50:47 +02:00
|
|
|
stack.append(floats.lt())
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 14: # gt
|
2020-09-28 19:50:47 +02:00
|
|
|
stack.append(floats.gt())
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 15: # depth
|
2020-09-28 19:50:47 +02:00
|
|
|
stack.append(floats.depth())
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 16: # dup
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.dup()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 17: # drop
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.drop()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 18: # swap
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.swap()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 19: # log
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.log()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 20: # pow
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.pow()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 21: # sin
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.sin()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 22: # cos
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.cos()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 23: # tan
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.tan()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 24: # asin
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.asin()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 25: # atan
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.atan()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 26: # acos
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.acos()
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 27: # to alt.
|
2020-09-28 19:50:47 +02:00
|
|
|
afloats.push(floats.pop())
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 28: # from alt.
|
2020-09-28 19:50:47 +02:00
|
|
|
floats.push(afloats.pop())
|
2020-09-28 18:49:09 +02:00
|
|
|
if action == 29: # alt. depth
|
2020-09-28 19:50:47 +02:00
|
|
|
stack.append(afloats.depth())
|
2018-11-22 04:03:57 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
|
|
|
instructions = [
|
|
|
|
i_no,
|
|
|
|
i_li,
|
|
|
|
i_du,
|
|
|
|
i_dr,
|
|
|
|
i_sw,
|
|
|
|
i_pu,
|
|
|
|
i_po,
|
|
|
|
i_ju,
|
|
|
|
i_ca,
|
|
|
|
i_cc,
|
|
|
|
i_re,
|
|
|
|
i_eq,
|
|
|
|
i_ne,
|
|
|
|
i_lt,
|
|
|
|
i_gt,
|
|
|
|
i_fe,
|
|
|
|
i_st,
|
|
|
|
i_ad,
|
|
|
|
i_su,
|
|
|
|
i_mu,
|
|
|
|
i_di,
|
|
|
|
i_an,
|
|
|
|
i_or,
|
|
|
|
i_xo,
|
|
|
|
i_sh,
|
|
|
|
i_zr,
|
|
|
|
i_ha,
|
|
|
|
i_ie,
|
|
|
|
i_iq,
|
|
|
|
i_ii,
|
|
|
|
]
|
|
|
|
|
2018-02-12 16:01:48 +01:00
|
|
|
|
|
|
|
def validateOpcode(opcode):
|
|
|
|
I0 = opcode & 0xFF
|
|
|
|
I1 = (opcode >> 8) & 0xFF
|
|
|
|
I2 = (opcode >> 16) & 0xFF
|
|
|
|
I3 = (opcode >> 24) & 0xFF
|
2020-09-22 14:06:40 +02:00
|
|
|
if (
|
|
|
|
(I0 >= 0 and I0 <= 29)
|
|
|
|
and (I1 >= 0 and I1 <= 29)
|
|
|
|
and (I2 >= 0 and I2 <= 29)
|
|
|
|
and (I3 >= 0 and I3 <= 29)
|
|
|
|
):
|
2018-02-13 18:13:01 +01:00
|
|
|
return True
|
2018-02-12 16:01:48 +01:00
|
|
|
else:
|
2018-02-13 18:13:01 +01:00
|
|
|
return False
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
|
|
|
def extractString(at):
|
2018-02-12 18:58:50 +01:00
|
|
|
i = at
|
2020-09-22 14:06:40 +02:00
|
|
|
s = ""
|
|
|
|
while memory[i] != 0:
|
2018-02-12 18:58:50 +01:00
|
|
|
s = s + chr(memory[i])
|
|
|
|
i = i + 1
|
|
|
|
return s
|
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
|
|
|
def injectString(s, to):
|
2018-02-12 18:58:50 +01:00
|
|
|
global memory
|
|
|
|
i = to
|
|
|
|
for c in s:
|
|
|
|
memory[i] = ord(c)
|
|
|
|
i = i + 1
|
|
|
|
memory[i] = 0
|
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
|
|
|
def execute(word, output="console"):
|
2018-02-12 16:01:48 +01:00
|
|
|
global ip, memory, stack, address
|
|
|
|
ip = word
|
|
|
|
address.append(0)
|
2020-09-22 14:06:40 +02:00
|
|
|
notfound = memory[findEntry("err:notfound") + 1]
|
2018-02-12 16:01:48 +01:00
|
|
|
while ip < 100000 and len(address) > 0:
|
2018-11-22 04:12:56 +01:00
|
|
|
if ip == notfound:
|
2020-09-22 14:06:40 +02:00
|
|
|
print("ERROR: word not found!")
|
2018-02-12 16:01:48 +01:00
|
|
|
opcode = memory[ip]
|
|
|
|
if validateOpcode(opcode):
|
|
|
|
I0 = opcode & 0xFF
|
|
|
|
I1 = (opcode >> 8) & 0xFF
|
|
|
|
I2 = (opcode >> 16) & 0xFF
|
|
|
|
I3 = (opcode >> 24) & 0xFF
|
2020-09-22 14:06:40 +02:00
|
|
|
if I0 != 0:
|
|
|
|
instructions[I0]()
|
|
|
|
if I1 != 0:
|
|
|
|
instructions[I1]()
|
|
|
|
if I2 != 0:
|
|
|
|
instructions[I2]()
|
|
|
|
if I3 != 0:
|
|
|
|
instructions[I3]()
|
2018-02-12 16:01:48 +01:00
|
|
|
else:
|
2020-09-22 14:06:40 +02:00
|
|
|
print("Invalid Bytecode", opcode, ip)
|
2018-11-22 04:03:57 +01:00
|
|
|
ip = 2000000
|
2018-02-12 16:01:48 +01:00
|
|
|
ip = ip + 1
|
2018-11-22 04:03:57 +01:00
|
|
|
return
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-13 18:13:01 +01:00
|
|
|
def load_image():
|
|
|
|
global memory
|
2020-09-22 14:06:40 +02:00
|
|
|
cells = int(os.path.getsize("ngaImage") / 4)
|
|
|
|
f = open("ngaImage", "rb")
|
|
|
|
memory = list(struct.unpack(cells * "i", f.read()))
|
2018-02-13 18:13:01 +01:00
|
|
|
f.close()
|
|
|
|
remaining = 1000000 - cells
|
2020-09-22 14:06:40 +02:00
|
|
|
memory.extend([0] * remaining)
|
|
|
|
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2018-02-13 18:13:01 +01:00
|
|
|
def run():
|
|
|
|
Done = False
|
2020-09-22 14:06:40 +02:00
|
|
|
Interpreter = memory[findEntry("interpret") + 1]
|
2018-02-13 18:13:01 +01:00
|
|
|
while not Done:
|
2020-09-22 14:06:40 +02:00
|
|
|
Line = input("\nOk> ")
|
|
|
|
if Line == "bye":
|
|
|
|
Done = True
|
2018-02-13 18:13:01 +01:00
|
|
|
else:
|
2020-09-22 14:06:40 +02:00
|
|
|
for Token in Line.split(" "):
|
2018-02-13 18:13:01 +01:00
|
|
|
injectString(Token, 1025)
|
|
|
|
stack.append(1025)
|
|
|
|
execute(Interpreter)
|
2018-02-12 16:01:48 +01:00
|
|
|
|
2020-09-22 14:06:40 +02:00
|
|
|
|
2018-02-12 16:01:48 +01:00
|
|
|
if __name__ == "__main__":
|
2018-02-13 18:13:01 +01:00
|
|
|
load_image()
|
|
|
|
run()
|