class While(Statement): def __init__(self, tokens): self.t = TokenHandler(tokens) def execute(self, skip): self.t.match("while", self.t.tokens) conditiontokens = [] conditionistrue = self.t.ConditionIsTrue(self.t.tokens, conditiontokens) self.t.match("do", self.t.tokens) tokensexecuted = None while(conditionistrue): copytokens = list(self.t.tokens) s = self.createStatement(copytokens) tokensexecuted = s.execute(skip) copytokens = list(conditiontokens) conditiontokens = None conditiontokens = [] conditionistrue = self.t.ConditionIsTrue(copytokens, conditiontokens) size = len(self.t.executedtokens) i = size - 1 while(i >= size - len(conditiontokens)): self.t.executedtokens.pop(i) i -= 1 self.t.moveAhead(tokensexecuted) return self.t.resetTokens()
class ProgramStatement(Statement): programName = None def __init__(self, tokens): self.t = TokenHandler(tokens) def execute(self, skip): self.t.match("program", self.t.tokens) currentToken = self.t.getCurrentToken() if not skip: self.programName = currentToken self.t.match(currentToken, self.t.tokens) copytokens = list(self.t.tokens) c = CompoundStatement(copytokens) tokensExecuted = c.execute(skip) self.t.moveAhead(tokensExecuted) return self.t.resetTokens()
class CompoundStatement(Statement): def __init__(self, tokens): self.t = TokenHandler(tokens) def execute(self, skip): self.t.match("begin", self.t.tokens) currenttoken = self.t.getCurrentToken() condition = True while condition == True: copytokens = list(self.t.tokens) currentstatement = self.createStatement(copytokens) tokensexecuted = currentstatement.execute(skip) self.t.moveAhead(tokensexecuted) currenttoken = self.t.getCurrentToken() if currenttoken == ";": condition = True self.t.match(";", self.t.tokens) else: condition = False self.t.match("end", self.t.tokens) return self.t.resetTokens()
class IF(Statement): def __init__(self, tokens): self.t = TokenHandler(tokens) def execute(self, skip): self.t.match("if", self.t.tokens) currenttoken = self.t.getCurrentToken() if(skip): while(currenttoken != "else"): self.t.match(currenttoken, self.t.tokens) currenttoken = self.t.getCurrentToken() self.t.match("else", self.t.tokens) copytokens = list(self.t.tokens) s = self.createStatement(copytokens) tokensexecuted = s.execute(skip) self.moveAhead(tokensexecuted) if(self.t.ConditionIsTrue(self.t.tokens, outcondition = [])): self.t.match("then", self.t.tokens) copytokens = list(self.t.tokens) s = self.createStatement(copytokens) tokensexecuted = s.execute(skip) self.t.moveAhead(tokensexecuted) self.t.match("else", self.t.tokens) copytokens = list(self.t.tokens) s = self.createStatement(copytokens) tokensexecuted = s.execute(True) self.t.moveAhead(tokensexecuted) else: currenttoken = self.t.getCurrentToken() while(currenttoken != "else"): self.t.match(currenttoken, self.t.tokens) currenttoken = self.t.getCurrentToken() self.t.match("else", self.t.tokens) copytokens = list(self.t.tokens) s = self.createStatement(copytokens) tokensexecuted = s.execute(skip) self.t.moveAhead(tokensexecuted) return self.t.resetTokens()