我正在创建一个控制台应用程序,其中一部分是当用户在其控制台输入的末尾键入-store in "whatever_variable"
时,它应该在键的名为v
的字典中存储一个值"whatever_variable"
,但它给了我一个SyntaxError。
我的代码是{{3}},但它有点长,所以我只会粘贴一些重要的方法
compileCode方法(将inp作为参数):
storeIn = None
shouldEcho = None
shouldPrint = None
inp = replace_first(inp, '-r"', '"')
inp = replace_all_dash(inp, "-r", "raw")
inp = replace_all_dash(inp, "-a", "alias", "v", "var")
inp = replace_all_dash(inp, "-e", "echo")
inp = replace_all_dash(inp, "-p", "print", "println")
inp = replace_all_dash(inp, "True", "yes", "positive")
inp = replace_all_dash(inp, "False", "no", "negative")
inp = replace_all_dash(inp, "None", "nothing", "empty", "undefined", "null")
spl = inp.split("-e")
if len(spl) >= 2:
i = len(spl)-1
del spl[i]
shouldEcho = True
spl = "-e".join(spl)
spl = spl.split("-p")
if len(spl) >= 2:
i = len(spl)-1
del spl[i]
shouldPrint = True
spl = "-p".join(spl)
spl = spl.split("-ifvar")
if len(spl) >= 2:
i = len(spl)-1
words = spl[i].split()
varTxt = 'var['+words.pop(i)+']'
cond = varTxt+'words'
t = test(cond)
if t != True:
return
spl = "-ifvar".join(spl)
spl = spl.split("-if")
if len(spl) >= 2:
i = len(spl)-1
cond = eval(spl.pop(i))
t = test(cond)
if t != True:
return
spl = "if".join(spl)
spl = spl.split("-store in ")
if len(spl) >= 2:
i = len(spl) - 1
storeIn = eval(spl.pop(i))
spl = "-store in ".join(spl)
spl = spl.split("-r")
if len(spl) >= 2:
cmd = ".".join(spl.pop(0).split())
args = "-r".join(spl).split("-a")
for i in range(len(args)):
if i > 0:
a = "v["+args[i]+"]"
args[i] = a
args = "".join(args)
if shouldEcho:
code = "{0}({1}, shouldEcho=True)".format(cmd, args)
elif shouldPrint:
code = "{}({}, shouldPrint=True)".format(cmd, args)
else:
code = "{0}({1})".format(cmd, args)
else:
spl = "".join(spl)
spl = spl.split("-a")
if len(spl) >= 2:
cmd = ".".join(spl[0].split())
for i in range(len(spl)):
if i > 0:
a = "v["+spl[i]+"]"
spl[i] = a
spl.pop(0)
spl = "".join(spl)
if shouldEcho:
code = "{}({}, shouldEcho=True)".format(cmd, spl)
elif shouldPrint:
code = "{}({}, shouldPrint=True)".format(cmd, spl)
else:
code = "{}({})".format(cmd, spl)
else:
spl = "".join(spl)
spl = spl.split("-f")
if len(spl) >= 2:
cmd = spl.pop(0)
arg = spl.pop(0)
cmd = "".join(cmd)
cmd = ".".join(cmd.split())
if shouldEcho:
code = "{}({}(), shouldEcho=True)".format(cmd, arg)
elif shouldPrint:
code = "{}({}(), shouldPrint=True)".format(cmd, arg)
else:
code = "{}({}())".format(cmd, arg)
else:
spl = "".join(spl)
spl = ".".join(spl.split())
if shouldEcho:
code = "{}(shouldEcho=True)".format(spl)
elif shouldPrint:
code = "{}(shouldPrint=True)".format(spl)
else:
code = "{}()".format(spl)
if type(storeIn) == str:
code = """temp = {}
v["{}"] = temp""".format(code, storeIn)
return code
runCode方法(将inp作为参数):
code = compileCode(inp)
eval(code)
echo方法(取可选参数l)
print(l, end="")
return l
和v:
的声明v = {}
我的标准是:
echo "Hello, world!" -store in "a"
printAliases
q
,错误是:
Traceback (most recent call last):
File "main.py", line 267, in <module>
playLoop()
File "main.py", line 265, in playLoop
mainLoop()
File "main.py", line 255, in mainLoop
runCode(inp)
File "main.py", line 148, in runCode
eval(code)
File "<string>", line 1
temp = echo("Hello, world!" )
^
SyntaxError: invalid syntax