def forLoopWithRange(node, varID, indexes, symbolTable, scope): Step = 1 if node.getSon(4).getName() == "STEP": Step = node.getSon(5).getName() lowerLimit = int(indexes[0][0]) upperLimit = int(indexes[0][2]) if lowerLimit < upperLimit: if node.getSon(4).getName() == "STEP": Step = node.getSon(5).getName() totalCycles = upperLimit-lowerLimit if Step == 1: for cycle in range(lowerLimit, upperLimit): tempSymbol = symbolTable.getSymbolByScope(varID, scope) tempSymbol.setValue(cycle) symbolTable.modifySymbol(tempSymbol) tempStatementList = node.getSon(5) statementList(tempStatementList, symbolTable, scope) symbolTable.eliminateSymbolByScope(varID, scope) else: cycle = lowerLimit while cycle < upperLimit: tempSymbol = symbolTable.getSymbolByScope(varID, scope) tempSymbol.setValue(cycle) symbolTable.modifySymbol(tempSymbol) tempStatementList = node.getSon(7) statementList(tempStatementList, symbolTable, scope) cycle += Step symbolTable.eliminateSymbolByScope(varID, scope) else: logError("Semantic: UpperLimit " + str(upperLimit) + " doesn't match LowerLimit " + str(lowerLimit))
def ifStatementBoolean(node, comparable, value, operator, symbolTable, scope): if operator == "==": if comparable == value: statementList(node.getSon(5), symbolTable, scope) else: pass elif operator == "!=": if comparable != value: statementList(node.getSon(5), symbolTable, scope) else: pass else: logError(operator + ": Operator not supported in variable " + str(type(value)))
def forLoop(node, symbolTable, scope): if isReadyForRun(): varID = node.getSon(1).getName() iterable = node.getSon(3).getSon(0).getName() symbolTable.simpleAdd(varID, 0, Types.Integer, scope) isRange = False if not isinstance(iterable, int): iterableValueLength = 0 if iterable == "indexedId": tempNode = node.getSon(3).getSon(0) tempListSymbol = symbolTable.getSymbolByScope(tempNode.getSon(0).getName(), scope) if tempListSymbol == None: tempListSymbol = symbolTable.getSymbolByScope(tempNode.getSon(0).getName(), "global") tempList = tempListSymbol.getValue() indexes = getIndexes(tempNode.getSon(1), [], symbolTable, scope) for value in indexes: if not isinstance(value, int): if value[1] == ':': isRange = True forLoopWithRange(node, varID, indexes, symbolTable, scope) break else: break if not isRange: iterableValueLength = len(getListElementByIndex(tempList, indexes)) elif iterable == "len": tempID = node.getSon(3).getSon(0).getSon(0).getName() iterableValue = symbolTable.getSymbolByScope(tempID, scope) if iterableValue == None: logError("Semantic: Variable " + str(tempID) + " was not found") iterableValueLength = len(iterableValue.getValue()) else: iterableValue = symbolTable.getSymbolByScope(iterable, scope) if iterableValue == None: iterableValue = symbolTable.getSymbolByScope(node.getSon(3).getSon(0).getName(), "global") if isinstance(iterableValue.getValue(), int): iterableValueLength = iterableValue.getValue() else: iterableValueLength = len(iterableValue.getValue()) if not isRange: Step = 1 if node.getSon(4).getName() == "STEP": Step = node.getSon(5).getName() totalCycles = iterableValueLength if Step == 1: for cycle in range(totalCycles): tempSymbol = symbolTable.getSymbolByScope(varID, scope) tempSymbol.setValue(cycle) symbolTable.modifySymbol(tempSymbol) tempStatementList = node.getSon(5) statementList(tempStatementList, symbolTable, scope) symbolTable.eliminateSymbolByScope(varID, scope) else: cycle = 0 while cycle < totalCycles: tempSymbol = symbolTable.getSymbolByScope(varID, scope) tempSymbol.setValue(cycle) symbolTable.modifySymbol(tempSymbol) tempStatementList = node.getSon(7) statementList(tempStatementList, symbolTable, scope) cycle += Step symbolTable.eliminateSymbolByScope(varID, scope) else: for cycle in range(iterable): tempSymbol = symbolTable.getSymbolByScope(varID, scope) tempSymbol.setValue(cycle) symbolTable.modifySymbol(tempSymbol) tempStatementList = node.getSon(5) statementList(tempStatementList, symbolTable, scope) symbolTable.eliminateSymbolByScope(varID, scope)
def ifStatementInteger(node, comparable, value, operator, symbolTable, scope): if operator == "==": if comparable.getValue() == value: statementList(node.getSon(5), symbolTable, scope) else: pass elif operator == '<': if comparable.getValue() < value: statementList(node.getSon(5), symbolTable, scope) else: pass elif operator == ">": if comparable.getValue() > value: statementList(node.getSon(5), symbolTable, scope) else: pass elif operator == "<=": if comparable.getValue() <= value: statementList(node.getSon(5), symbolTable, scope) else: pass elif operator == ">=": if comparable.getValue() >= value: statementList(node.getSon(5), symbolTable, scope) else: pass elif operator == "!=": if comparable.getValue() != value: statementList(node.getSon(5), symbolTable, scope) else: pass else: logError("Value type not supported")