class Assign: def __init__(self): self.id = None self.expr = None #Simple parsing for Assign def parse(self): self.id = Id() self.id.parse(TokList.getIdOrConst()) TokList.nextToken() TokList.match('ASSIGN', 'assign') self.expr = Expr() self.expr.parse() TokList.match('SEMICOLON', 'assign') #Simple printing for Assign def print(self): TokList.printIndent() self.id.print() print(':=', end='') self.expr.print() print(';') #Executor that determines value of an expression and then sets that value to the respective Id def exec(self): val = self.expr.exec() self.id.setValue(val)
class CaseLine: def __init__(self): self.const = None self.constList = None self.expr = None self.caseLineFollow = None #Parsing for CaseLine based on the homework def parse(self): self.const = Const() self.const.parse(TokList.getIdOrConst()) TokList.nextToken() if TokList.currentToken() == 'COMMA': self.constList = ConstList() self.constList.parse() TokList.match('COLON', 'case line') self.expr = Expr() self.expr.parse() self.caseLineFollow = CaseLineFollow() self.caseLineFollow.parse() #Simple printing def print(self): self.const.print() if self.constList is not None: self.constList.print() print(':', end='') self.expr.print() self.caseLineFollow.print() #Exectuion that returns a boolean based on idValue and constants given def exec(self, caseId): if self.const.exec() == caseId.getValue(): caseId.setValue(self.expr.exec()) return True if self.constList is not None: if self.constList.exec(caseId): caseId.setValue(self.expr.exec()) return True if self.caseLineFollow.exec(caseId): return True return False
class Case: def __init__(self): self.case_line = None self.expr = None self.id = None #Simple parse of the case statement def parse(self): TokList.match('CASE','case') self.id = Id() self.id.parse(TokList.getIdOrConst()) TokList.nextToken() TokList.match('OF','case') self.case_line = CaseLine() self.case_line.parse() TokList.match('ELSE','case') self.expr = Expr() self.expr.parse() TokList.match('END','case') TokList.match('SEMICOLON','case') #Printing with proper indentation manipulation and new line printings def print(self): TokList.printIndent() print('case ', end='') self.id.print() print(' of') TokList.increaseIndent() TokList.printIndent() self.case_line.print() #Necessary for new line printings print() TokList.printIndent() print('else ', end='') self.expr.print() print() TokList.decreaseIndent() TokList.printIndent() print('end;') #Recursive execution based on values in the case line. Id value is passed in to all versions of case def exec(self): if not (self.case_line.exec(self.id)): self.id.setValue(self.expr.exec())
class Out: def __init__(self): self.expr = None #Simple parsing for output statement def parse(self): TokList.match('OUTPUT', 'output') self.expr = Expr() self.expr.parse() TokList.match('SEMICOLON', 'output') #Simple printing def print(self): TokList.printIndent() print('output ', end='') self.expr.print() print(';') #Execution that prints the values returned by the expression evaluation def exec(self): print(self.expr.exec())
class Cmpr: def __init__(self): self.rightExpr = None self.leftExpr = None self.checkValue = None #Parsing to determine kind of comparator. Checks for middle comparison while returning an error if none are given. def parse(self): self.leftExpr = Expr() self.leftExpr.parse() if TokList.checkTok('EQUAL'): self.checkValue = '=' TokList.nextToken() elif TokList.checkTok('LESSTHAN'): self.checkValue = '<' TokList.nextToken() elif TokList.checkTok('LESSTHANEQUAL'): self.checkValue = '<=' TokList.nextToken() else: print('Error: improper syntax for comparator') exit() self.rightExpr = Expr() self.rightExpr.parse() #Simple printing def print(self): self.leftExpr.print() print(self.checkValue, end='') self.rightExpr.print() #Executor based on results gained while parsing def exec(self): if self.checkValue == '=': return self.leftExpr.exec() == self.rightExpr.exec() elif self.checkValue == '<': return self.leftExpr.exec() < self.rightExpr.exec() elif self.checkValue == '<=': return self.leftExpr.exec() <= self.rightExpr.exec()