Huit interpreter
Jump to navigation
Jump to search
The following Python script is an incomplete Huit interpreter.
prgm = input('>Huit ')
prgm += "\0" # NUL is the EOF
# Finds the location of all jump points
names = []
points = []
j = 0
k = 0
name = ""
for i in range(0, len(prgm)):
if k == 1 and prgm[i] != ")":
name += prgm[i]
if prgm[i] == "\"":
if prgm[i-1] == "\\": # To read " but ignore \" # Bug here
pass
else: j = (j + 1) % 2
if j == 0:
if prgm[i] == "(":
k = 1
elif prgm[i] == ")":
k = 0
names.append(name)
points.append(i)
name = ""
replace = ['\\n','\n','\\t','\t','\\\"','\"','\\\'','\'']
# Executes the program
floats = ['0','1','2','3','4','5','6','7','8','9','.','-']
input_string = ""
input_float = 0
char = 0
queue = []
inst = 0
def deq(repeat):
for i in range(0, repeat):
queue.pop(0)
def enq(a):
queue.append(a)
while inst < len(prgm):
if prgm[inst] == "+":
enq(queue[0] + queue[1])
deq(2)
elif prgm[inst] == "-":
enq(queue[0] - queue[1])
deq(2)
elif prgm[inst] == "*":
enq(queue[0] * queue[1])
deq(2)
elif prgm[inst] == "/":
enq(queue[0] / queue[1])
deq(2)
elif prgm[inst] == "%":
queue.append(queue[0] % queue[1])
deq(2)
elif prgm[inst] == "&":
read = ""
if prgm[inst+1] == "\"":
inst += 2
while not (prgm[inst] == "\"" and prgm[inst-1] != "\\"): # Bug here
read += prgm[inst]
inst += 1
for i in range(0, len(replace), 2): # Bug here
read = read.replace(replace[i], replace[i+1])
enq(read)
else:
inst += 1
start = inst
while prgm[inst] in floats:
if prgm[inst] == '-' and inst != start:
break # To not read minus instructions.
read += prgm[inst]
inst += 1
enq(float(read))
inst -= 1
elif prgm[inst] == ":":
enq(queue[0])
elif prgm[inst] == "?":
if len(queue) > 0:
deq(1)
elif prgm[inst] == "(":
while prgm[inst] != ")":
inst += 1
# [] is missing
elif prgm[inst] == ">":
if prgm[inst+1] == "\'":
if type(queue[0]) == float:
queue[0] = chr(int(queue[0]))
print(queue[0])
deq(1)
elif prgm[inst] == "<":
if prgm[inst+1] == "\'":
input_float = input('> ')
enq(input_float)
else:
if input_string == "":
input_string = input('> ')
char = 0
enq(input_string[char])
char += 1
elif prgm[inst] == "#": # Debug character
print("inst: %s, queue: %s" % (inst, queue))
# Add strings to input format
else:
pass
inst += 1