I'm creating my own programming language. The interpreter is written in Python.
I'm trying to add an 'if/else' command. The syntax is:
if "hello" == $myString |> goto somewhere
else |> goto somewhere_else
Instead of implementing the operator logic on my own, I decided to just use the Python exec()
command. This is what I am doing (simplified):
task = task.split(' |> ', 1) # Split on first occurence of "|> "
clause = task[0]
operation = task[1]
clauseCheck = False
clause = clause + ": clauseCheck = True" # So we can run it as python code to eval
eval(clause) # if the clause evals to true, clauseCheck will be True
if clauseCheck:
parse(operation)
And here's the error I'm getting (using if 7 != 2 |> ...
):
Traceback (most recent call last):
...
File "interpret.py", line 139, in parse
eval(clause) # if the clause evals to true, clauseCheck will be True
File "<string>", line 1
if 7 != 2: print('True') ^ SyntaxError: invalid syntax
I've tried everything I can think of - I've added a newline to the string given to eval()
with and without indentation, I've tried just printing 'true' instead of setting a variable and I've tried with a different if statement.
How can I get my code to properly evaluate the if statements?