self.checkValidVariable(statementString, variableTable) self.checkValidValue(statementString) print self.variable, self.value return validStatement def toCCode(self): if self.increment: return "{0}++;\n".format(self.variable.name) else: return "{0} += {1};\n".format(self.variable.name, self.value) def checkValidVariable(self, statementString, variableTable): """ Checks if the variable in the statement is a valid variable """ variableName = self.getVariableName(statementString) self.variable = variableTable.getVariableWithName(variableName) if self.variable is None: print variableName, " is not a recognized variable" def checkValidValue(self, statementString): """ Checks to see if the value is a valid value""" if not self.increment: self.value = statementString.split(", IM UPPIN YA BY ")[1].strip() else: self.value = 1 def getVariableName(self, statementString): """ Return the variable name in the given statment""" return statementString.split(",")[0] __statementClasses.append(VariableIncrementStatment) # Register with the Statement Factory
from Statements.statement_factory import __statementClasses class ProgramExitStatement: """ Represents the Program Exit Statement """ def __init__(self): """ """ def isValidStatement(self, statementString, variableTable): """ Returns if the string is a valid statement """ # May want this to throw an exception if the statement looks almost proper, but fails for some reason return statementString == "KTHXBYE!" def toCCode(self): """ Translates the statement to C Code """ return "return 0;\n}" __statementClasses.append(ProgramExitStatement) # Register with the Statement Factory
validStatement = re.match(r"I HAZ .+? .+", statementString) if validStatement: self.addVariableToVariableList(statementString, variableTable) return validStatement def toCCode(self): """ Translates the statement to C Code """ return "{0} {1};\n".format(self.variable.type.lower(), self.variable.name) def addVariableToVariableList(self, statementString, variableTable): """ Returns the variable name in the given statement string """ statementPieces = statementString.split() type = self.getVariableType(statementPieces) name = self.getVariableName(statementString, type) self.variable = Variable(name, type) variableTable.addVariable(self.variable) def getVariableName(self, statementString, type): """ Returns the variable name in the given statement string """ return statementString.split(type)[1].strip() def getVariableType(self, statementPieces): """ Returns the variable type in the given statement string """ for type in VariableDeclarationStatement.types: if type in statementPieces: return type # Should throw an exception if it does not have a valid type __statementClasses.append(VariableDeclarationStatement) # Register with the Statement Factory
validStatement = re.match(r".+, I HAVE .+? TO PUT IN YOU", statementString) if validStatement: self.checkValidVariable(statementString, variableTable) self.checkValidValue(statementString) print self.variable, self.value return validStatement def toCCode(self): """ Translates the statement to C Code """ return "{0} = {1};\n".format(self.variable.name, self.value) def checkValidVariable(self, statementString, variableTable): """ Checks if the variable in the statement is a valid variable """ variableName = self.getVariableName(statementString) self.variable = variableTable.getVariableWithName(variableName) if self.variable is None: print variableName, "is not a recognized variable" def checkValidValue(self, statementString): """ Checks if the value is a valid value """ valueHalf = statementString.split(", I HAVE ")[1] self.value = valueHalf.split(" TO PUT IN YOU")[0].strip() # Should check if the value is a proper value for the variable type def getVariableName(self, statementString): """ Returns the variable name in the given statement string """ return statementString.split(",")[0] __statementClasses.append(VariableAssignmentStatement) # Register with the Statement Factory