我正在制作一个程序,在python中生成一个随机的Brainf*ck程序并运行它。我有一个解释器,我需要它来检查长循环(超过5秒)。
翻译是:
while i < len(code):
if code[i] == '<':
if pointerLocation > 0:
pointerLocation -= 1
elif code[i] == '>':
pointerLocation += 1
if len(array) <= pointerLocation:
array.append(0)
elif code[i] == '+':
array[pointerLocation] += 1
elif code[i] == '-':
if array[pointerLocation] > 0:
array[pointerLocation] -= 1
elif code[i] == '.':
print(array[pointerLocation], chr(array[pointerLocation]))
result += chr(array[pointerLocation])
elif code[i] == ',':
#x = input("Input (1 CHARACTER!):")
x = 'h'
#increase time if user inputs
t1 += 5
try:
y = int(x)
except ValueError:
y = ord(x)
array[pointerLocation] = y
elif code[i] == '[':
if array[pointerLocation] == 0:
open_braces = 1
while open_braces > 0 and i+1 < len(code):
i += 1
if code[i] == '[':
open_braces += 1
elif code[i] == ']':
open_braces -= 1
elif code[i] == ']':
# you don't need to check array[pointerLocation] because the matching '[' will skip behind this instruction if array[pointerLocation] is zero
open_braces = 1
while open_braces > 0:
if t > 5:
return [result,code,t]
print(123)
i -= 1
if code[i] == '[':
open_braces -= 1
elif code[i] == ']':
open_braces += 1
# i still gets incremented in your main while loop
i -= 1
i += 1很抱歉有这么长的片段,但我相信这一切都是必要的。
发布于 2022-08-27 19:43:39
当您进入循环5秒时设置一个告警。当你退出时取消它。如果信号触发,你就处理它。
一个较弱的版本是获得进入循环的时间,然后检查每个迭代,看看每次迭代是否超过5秒。
https://stackoverflow.com/questions/73513930
复制相似问题