Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

If statements in python interpreter not working

+5 votes
asked Mar 23 by Teddy (220 points)
my interpreter for a simple asm like language,

import random
import time

def tokenize_string(input_string):
  return input_string.split()

conditions = []
vars = {}

def runLine(line):
  if all(item == True for item in conditions):
    match line[0]:
      case "print":
        print(runLine(line[1:]), end="")
      case "println":
        print(runLine(line[1:]))
      case "pspace":
        print(" ", end="")
      case "input":
        return input()
      case "var":
        if len(line) > 2 and line[2] == "=":
          vars[line[1]] = runLine(line[3:])
        else:
          return vars.get(line[1], None)
      case "val":
        return " ".join(line[1:])
      case "add":
        return float(runLine(line[1:])) + float(runLine(line[3:]))
      case "sub":
        return float(runLine(line[1:])) - float(runLine(line[3:]))
      case "mul":
        return float(runLine(line[1:])) * float(runLine(line[3:]))
      case "div":
        return float(runLine(line[1:])) / float(runLine(line[3:]))
      case "random":
        return random.randint(int(runLine(line[1:])), int(runLine(line[3:])))
      case "wait":
        time.sleep(float(runLine(line[1:])))
      case "if":
        if runLine(line[1:]) == runLine(line[3:]):
          conditions.append(True)
        else:
          conditions.append(False)
      case "end":
        conditions.pop(-1)
      case _:
        print(f"\nError, Unknown action: \"{line[0]}\"")
        quit()
        

with open(input(), "r") as f:
    for line in f:
        tokens = tokenize_string(line.strip())
        runLine(tokens)
time.sleep(1)

the ifs add ether a true or a false to a list, if all of the list is true the code runs
here is some code that detects if 1 == 1, then prints 1

if val 1 val 1
    print val 1
end

1 Answer

+1 vote
answered Apr 1 by Raman Parjapati (160 points)
import random
import time

def tokenize_string(input_string):
    return input_string.split()

conditions = []
vars = {}

def runLine(line):
    if all(item == True for item in conditions):
        match line[0]:
            case "print":
                print(runLine(line[1:]), end="")
            case "println":
                print(runLine(line[1:]))
            case "pspace":
                print(" ", end="")
            case "input":
                return input()
            case "var":
                if len(line) > 2 and line[2] == "=":
                    vars[line[1]] = runLine(line[3:])
                else:
                    return vars.get(line[1], None)
            case "val":
                return " ".join(line[1:])
            case "add":
                return float(runLine(line[1:])) + float(runLine(line[3:]))
            case "sub":
                return float(runLine(line[1:])) - float(runLine(line[3:]))
            case "mul":
                return float(runLine(line[1:])) * float(runLine(line[3:]))
            case "div":
                return float(runLine(line[1:])) / float(runLine(line[3:]))
            case "random":
                return random.randint(int(runLine(line[1:])), int(runLine(line[3:])))
            case "wait":
                time.sleep(float(runLine(line[1:])))
            case "if":
                if runLine(line[1:]) == runLine(line[3:]):
                    conditions.append(True)
                else:
                    conditions.append(False)
            case "end":
                conditions.pop(-1)
            case _:
                print(f"\nError, Unknown action: \"{line[0]}\"")
                quit()

# Example code
tokens = tokenize_string("val 1 val 1 if print val 1 end")
runLine(tokens)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...