def tof(c):
if eval(c) == True:
return True
elif eval(c) == False:
return False
def interpret(code):
lines = code.splitlines()
for line in lines:
if line.startswith("//") or line.strip() == "" or line.startswith(" "):
continue
if line.startswith("write(") and line.endswith(")"):
if line[6] == '"' and line[-2] == '"':
print(line[7:-2])
elif line[6:-1].isdigit():
print(line[6:-1])
else: # not done yet (variables)
v = line[6:-1]
exec(f"print({v})")
if line.startswith("if ") and line.endswith(" then"):
one = line.split("if ")[1]
c = one.split(" then")[0]
if tof(c) == True:
for x in lines:
if x.startswith(" "):
interpret(x.split(" ")[1])
elif tof(c) == False:
continue
if line[2] == "=" or line[1] == "=": # not done yet (variables)
if line[2] == "=":
a = line.split(" = ")[0]
b = line.split(" = ")[1]
exec(f"{a} = {b}")
code = '''
if 1 + 1 == 2 then
write("This should execute once!")
if 1 + 3 == 4 then
// asterisk are there to tell which message is which.
write("This should execute once! **")
'''
interpret(code)
I tried breaking the loop as soon as it sees an unindented piece of code, but if I put something like this:
if True then
write("Hello, World!")
write("Stops here :(")
write("Never gets here..")
0 comments:
Post a Comment
Thanks