0

I am making a console app and one part of it is that when the user types -store in "whatever_variable" at the end of their console input, it should store a value in a dictionary called v at the key of "whatever_variable" but it gives me a SyntaxError.

My code is here but it's kinda long so I will just paste in a few of the important methods

compileCode method (takes inp as an argument):

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 method (takes inp as an argument):

code = compileCode(inp)
eval(code)

echo method (takes optional argument l)

print(l, end="")
return l

and the declaration of v:

v = {}

My stdin is:

echo "Hello, world!" -store in "a"
printAliases
q

and the error is:

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
salipshitz
  • 67
  • 13
  • [Let's see](https://docs.python.org/3/library/functions.html#eval)... `eval`'s argument must be a single expression. – vaultah Oct 08 '17 at 15:59
  • Thanks. Changing it to `exec` worked. – salipshitz Oct 08 '17 at 16:02
  • Have you considered using `argparse` to define how your command line arguments should work and have clearly defined functions they can call? Your code would be more readable, more secure and reduced in size... – Jon Clements Oct 08 '17 at 16:05
  • It's extraordinarily rare that `eval` or `exec` are needed. As a Python beginner, using these is almost always a mistake. Look for a less exotic solution. – Chris Johnson Oct 08 '17 at 16:28

0 Answers0