Beispiel #1
0
def check_plusminus_operator(quadruple):
  workingStack = quadruple.getWorkingStack()
  if workingStack:
    if workingStack[-1] == '+' or workingStack[-1] == '-':
      right_operand = quadruple.pilaO.pop() 
      right_type = helpers.getTypeV2(right_operand)
      left_operand = quadruple.pilaO.pop()
      left_type = helpers.getTypeV2(left_operand)
      operator = workingStack.pop()
      if quadruple.pOper:
        quadruple.pOper.pop()
      result_type = SemanticCube[operator][left_type][right_type]
      symbolTable.getCurrentScope().setLatestType(result_type)

      if result_type != 'TYPE MISMATCH':
        # get next temp memory depending on type
        keyword = ''
        if symbolTable.getCurrentScope().getContext() == 'global':
          keyword = 'global'
        else:
          keyword = 'local'
        tempAddressPointer = getPointingScope(symbolTable.getCurrentScope()).memory.memSpace[keyword][result_type]['temp']
        tempAddress = tempAddressPointer.getInitialAddress() + tempAddressPointer.getOffset()
        quadruple.pilaO.append(tempAddress)
        symbolTable.getCurrentScope().setLatestExpValue(result_type)
        quadruple.saveQuad(operator,left_operand,right_operand, tempAddress)
        global tempCounter
        symbolTable.getCurrentScope().getScopeTemps()[tempCounter] = (Variable('', result_type, 0, [], tempAddressPointer.getOffset(), False, tempAddressPointer, False))
        tempCounter += 1        
        tempAddressPointer.setOffset()
        
      else:
        raise Exception('ERROR! Type Mismatch') #type mismatch
Beispiel #2
0
def endDim(var):
  current = symbolTable.getCurrentScope()
  aux = quadruple.pilaO.pop()
  varPointerDimNodes = var.getDimensionNodes()
  keyword = ''
  if symbolTable.getCurrentScope().getContext() == 'global':
    keyword = 'global'
  else:
    keyword = 'local'
  currType = helpers.getTypeV2(quadruple.pilaO[-1])
  tempAddressPointer = getPointingScope(symbolTable.getCurrentScope()).memory.memSpace[keyword][currType]['temp']
  tempAddress = tempAddressPointer.getInitialAddress() + tempAddressPointer.getOffset()
  quadruple.saveQuad("+a", aux, varPointerDimNodes[var.getDimensions()-1].getOffset(), tempAddress)  
  global tempCounter
  symbolTable.getCurrentScope().getScopeTemps()[tempCounter] = (Variable('', currType, 0, [], tempAddressPointer.getOffset(), False, tempAddressPointer, False))
  tempCounter += 1        
  tempAddressPointer.setOffset()
  tempAddress2 = tempAddressPointer.getInitialAddress() + tempAddressPointer.getOffset()
  quadruple.saveQuad("+a", tempAddress, var.getVirtualAddress(), tempAddress2)

  symbolTable.getCurrentScope().getScopeTemps()[tempCounter] = (Variable('', currType, 0, [], tempAddressPointer.getOffset(), False, tempAddressPointer, False))
  tempCounter += 1        
  quadruple.pilaO.pop() # gets rid of dirBase
  tempAddressPointer.setOffset()
  quadruple.pilaO.append([tempAddress2])
  quadruple.pOper.pop() # eliminates fake bottom
  quadruple.pilaDim.pop()
  current.resetLatestDimension()
  return True
Beispiel #3
0
def enterCond():
    res = quadruple.pilaO.pop()
    resType = helpers.getTypeV2(res)
    if resType != 'boo':
        raise Exception("ERROR! Conditional must have boolean value")
    quadruple.saveQuad('gotoF', res, -1, TBD())
    jumps.setStackPush(quadruple.quadCounter - 1)
Beispiel #4
0
def verifyParamMatch(cont):
    incoming = quadruple.pilaO.pop()

    incoming_type = helpers.getTypeV2(incoming)
    original = symbolTable.getCurrentScope().getCurrentFunctionParams()[cont]
    if incoming_type != original.getVarType():
        raise Exception("ERROR! Parameter mismatch")
    else:
        quadruple.saveQuad('param', incoming, -1, cont + 1)
        return cont
Beispiel #5
0
def expression_evaluation(p):
  if not quadruple.pOper or quadruple.pOper[-1] == '=' or quadruple.pOper[-1] == 'print':
    if not quadruple.pOper:
      # if it comes from a conditional
      if p[-2] == 'if' or p[-3] == 'while':
        condHelpers.enterCond()
   # regular assignments
    elif quadruple.pOper[-1] == '=':
      right_operand = quadruple.pilaO.pop() # this should be a value
      left_operand = quadruple.pilaO.pop() # this should be an id
      right_type = helpers.getTypeV2(right_operand)
      left_type = helpers.getTypeV2(left_operand)   
      if right_type == left_type:
        quadruple.saveQuad('=', right_operand, -1 ,left_operand)
      else:
        raise Exception("ERROR! cannot assign type {} to {}".format(left_type, right_type))
      quadruple.pOper.pop()
    # print statements
    elif quadruple.pOper[-1] == 'print' and not symbolTable.getCurrentScope().getMatchingParams():
      print_operand = quadruple.pilaO.pop() # this should be the value to print
      quadruple.saveQuad('print', -1, -1, print_operand)
  # verifying array dimensions
  elif quadruple.pOper[-1] == '$':
    lsPointer = quadruple.pilaDim[-1]["id"].getDimensionNodes()
    currentDim = quadruple.pilaDim[-1]["dim"]
    quadruple.saveQuad("verify", quadruple.pilaO[-1], 0, lsPointer[currentDim-1].getLim()) # -1 because array index
    keyword = ''
    if symbolTable.getCurrentScope().getContext() == 'global':
      keyword = 'global'
    else:
      keyword = 'local'
    currType = helpers.getTypeV2(quadruple.pilaO[-1])
    tempAddressPointer = getPointingScope(symbolTable.getCurrentScope()).memory.memSpace[keyword][currType]['temp']
    tempAddress = tempAddressPointer.getInitialAddress() + tempAddressPointer.getOffset()
    global tempCounter
    # if array has another dimension still, multiply S1 by MDim1 
    if len(lsPointer) > currentDim: 
      aux = quadruple.pilaO.pop()
      quadruple.saveQuad("*a", aux, lsPointer[currentDim-1].getMDim(), tempAddress)      
      symbolTable.getCurrentScope().getScopeTemps()[tempCounter] = (Variable('', currType, 0, [], tempAddressPointer.getOffset(), False, tempAddressPointer, False))
      tempCounter += 1        
      quadruple.pilaO.append(tempAddress)
      tempAddressPointer.setOffset()
    # if array has more than 1 dimension, generate quads
    if currentDim > 1:
      left_operand = quadruple.pilaO.pop()
      right_operand = quadruple.pilaO.pop()
      tempAddress = tempAddressPointer.getInitialAddress() + tempAddressPointer.getOffset()
      quadruple.saveQuad("+", left_operand, right_operand, tempAddress)
      symbolTable.getCurrentScope().getScopeTemps()[tempCounter] = (Variable('', currType, 0, [], tempAddressPointer.getOffset(), False, tempAddressPointer, False))
      tempCounter += 1        
      quadruple.pilaO.append(tempAddress)
      tempAddressPointer.setOffset()
  # check that void functions do not have return values
  elif quadruple.pOper[-1] == 'return':
    if symbolTable.getCurrentScope().getScopeType() == 'void':
      if quadruple.pilaO[-1]:
        raise Exception("ERROR! Void functions cannot return values")

    expValue = quadruple.pilaO.pop()
    expValueType = helpers.getTypeV2(expValue)
    if expValueType != symbolTable.getCurrentScope().getScopeType():
      raise Exception("ERROR! Type mismatch in returning function")
    # if void, return should have no expValue
    quadruple.saveQuad('return', -1, -1, expValue)
    quadruple.pOper.pop()
Beispiel #6
0
  def execute(self):
    global arrParam
    global paramCont
    global returnVal
    operCode = self.__quadList[self.__nextPointer].getOp()
    while (operCode < 28):
      currentQuad = self.__quadList[self.__nextPointer]
      if operCode == 1: #sumar
        pointersVal = self.binaryOps(self.returnIsArray(currentQuad.getLeft()), self.returnIsArray(currentQuad.getRight()), self.returnIsArray(currentQuad.getRes()))
        pointers = self.binaryOps(self.getPointerType(pointersVal[0]), self.getPointerType(pointersVal[1]), self.getPointerType(pointersVal[2]))
        if pointers[0][pointersVal[0]] == None or pointers[1][pointersVal[1]] == None:
          raise Exception("ERROR! Adding null values")
        res = self.add(pointers[0][pointersVal[0]], pointers[1][pointersVal[1]])

        self.assignValueToDir(res, pointers[2], pointersVal[2])
        self.__nextPointer += 1
      
      elif operCode == 2: #restar
        pointersVal = self.binaryOps(self.returnIsArray(currentQuad.getLeft()), self.returnIsArray(currentQuad.getRight()), self.returnIsArray(currentQuad.getRes()))
        pointers = self.binaryOps(self.getPointerType(pointersVal[0]), self.getPointerType(pointersVal[1]), self.getPointerType(pointersVal[2]))        
        if pointers[0][pointersVal[0]] == None or pointers[1][pointersVal[1]] == None:
          raise Exception("ERROR! Subtracting null values")
        res = self.subtract(pointers[0][pointersVal[0]], pointers[1][pointersVal[1]])

        self.assignValueToDir(res, pointers[2], pointersVal[2])
        self.__nextPointer += 1

      elif operCode == 3: # multiplicar
        pointersVal = self.binaryOps(self.returnIsArray(currentQuad.getLeft()), self.returnIsArray(currentQuad.getRight()), self.returnIsArray(currentQuad.getRes()))
        pointers = self.binaryOps(self.getPointerType(pointersVal[0]), self.getPointerType(pointersVal[1]), self.getPointerType(pointersVal[2]))

        if pointers[0][pointersVal[0]] == None or pointers[1][pointersVal[1]] == None:
          raise Exception("ERROR! Multiplying null values")
          
        res = self.multiply(pointers[0][pointersVal[0]], pointers[1][pointersVal[1]])

        self.assignValueToDir(res, pointers[2], pointersVal[2])
        self.__nextPointer += 1

      elif operCode == 4: #dividir
        pointersVal = self.binaryOps(self.returnIsArray(currentQuad.getLeft()), self.returnIsArray(currentQuad.getRight()), self.returnIsArray(currentQuad.getRes()))
        pointers = self.binaryOps(self.getPointerType(pointersVal[0]), self.getPointerType(pointersVal[1]), self.getPointerType(pointersVal[2]))
        if pointers[0][pointersVal[0]] == None or pointers[1][pointersVal[1]] == None:
          raise Exception("ERROR! Dividing null values")
        res = self.divide(pointers[0][pointersVal[0]], pointers[1][pointersVal[1]])

        self.assignValueToDir(res, pointers[2], pointersVal[2])
        self.__nextPointer += 1

      elif operCode == 5: # assign
        pointerValLeft = self.returnIsArray(currentQuad.getLeft())
        pointerValRes = self.returnIsArray(currentQuad.getRes())
        pointerLeft = self.getPointerType(currentQuad.getLeft())
        pointerRes = self.getPointerType(currentQuad.getRes())
        assignTo = pointerRes
        if isinstance(currentQuad.getLeft(), str):
          assignTo[pointerValRes] = returnVal
          returnVal = None
        else:
          assignWhat = pointerLeft
          self.assign(assignWhat, pointerValLeft, assignTo, pointerValRes)
        self.__nextPointer += 1

      elif operCode == 6: # less than
        pointersVal = self.binaryOps(self.returnIsArray(currentQuad.getLeft()), self.returnIsArray(currentQuad.getRight()), self.returnIsArray(currentQuad.getRes()))
        pointers = self.binaryOps(self.getPointerType(pointersVal[0]), self.getPointerType(pointersVal[1]), self.getPointerType(pointersVal[2]))        
        if pointers[0][pointersVal[0]] == None or pointers[1][pointersVal[1]] == None:
          raise Exception("ERROR! Comparing null values")
        res = self.lt(pointers[0][pointersVal[0]], pointers[1][pointersVal[1]])

        self.assignValueToDir(res, pointers[2], pointersVal[2])
        self.__nextPointer += 1

      elif operCode == 7: # greater than
        pointersVal = self.binaryOps(self.returnIsArray(currentQuad.getLeft()), self.returnIsArray(currentQuad.getRight()), self.returnIsArray(currentQuad.getRes()))
        pointers = self.binaryOps(self.getPointerType(pointersVal[0]), self.getPointerType(pointersVal[1]), self.getPointerType(pointersVal[2]))
        if pointers[0][pointersVal[0]] == None or pointers[1][pointersVal[1]] == None:
          raise Exception("ERROR! Comparing null values")
        res = self.gt(pointers[0][pointersVal[0]], pointers[1][pointersVal[1]])

        self.assignValueToDir(res, pointers[2], pointersVal[2])
        self.__nextPointer += 1

      elif operCode == 8: # less than equals
        pointersVal = self.binaryOps(self.returnIsArray(currentQuad.getLeft()), self.returnIsArray(currentQuad.getRight()), self.returnIsArray(currentQuad.getRes()))
        pointers = self.binaryOps(self.getPointerType(pointersVal[0]), self.getPointerType(pointersVal[1]), self.getPointerType(pointersVal[2]))
        if pointers[0][pointersVal[0]] == None or pointers[1][pointersVal[1]] == None:
          raise Exception("ERROR! Comparing null values")
        res = self.lte(pointers[0][pointersVal[0]], pointers[1][pointersVal[1]])

        self.assignValueToDir(res, pointers[2], pointersVal[2])
        self.__nextPointer += 1

      elif operCode == 9: # greater than equals
        pointersVal = self.binaryOps(self.returnIsArray(currentQuad.getLeft()), self.returnIsArray(currentQuad.getRight()), self.returnIsArray(currentQuad.getRes()))
        pointers = self.binaryOps(self.getPointerType(pointersVal[0]), self.getPointerType(pointersVal[1]), self.getPointerType(pointersVal[2]))
        if pointers[0][pointersVal[0]] == None or pointers[1][pointersVal[1]] == None:
          raise Exception("ERROR! Comparing null values")
        res = self.gte(pointers[0][pointersVal[0]], pointers[1][pointersVal[1]])

        self.assignValueToDir(res, pointers[2], pointersVal[2])
        self.__nextPointer += 1

      elif operCode == 10: # equal
        pointersVal = self.binaryOps(self.returnIsArray(currentQuad.getLeft()), self.returnIsArray(currentQuad.getRight()), self.returnIsArray(currentQuad.getRes()))
        pointers = self.binaryOps(self.getPointerType(pointersVal[0]), self.getPointerType(pointersVal[1]), self.getPointerType(pointersVal[2]))
        if pointers[0][pointersVal[0]] == None or pointers[1][pointersVal[1]] == None:
          raise Exception("ERROR! Comparing null values")
        res = self.equal(pointers[0][pointersVal[0]], pointers[1][pointersVal[1]])

        self.assignValueToDir(res, pointers[2], pointersVal[2])
        self.__nextPointer += 1

      elif operCode == 11: # not equal
        pointersVal = self.binaryOps(self.returnIsArray(currentQuad.getLeft()), self.returnIsArray(currentQuad.getRight()), self.returnIsArray(currentQuad.getRes()))
        pointers = self.binaryOps(self.getPointerType(pointersVal[0]), self.getPointerType(pointersVal[1]), self.getPointerType(pointersVal[2]))
        if pointers[0][pointersVal[0]] == None or pointers[1][pointersVal[1]] == None:
          raise Exception("ERROR! Comparing null values")
        res = self.notEqual(pointers[0][pointersVal[0]], pointers[1][pointersVal[1]])

        self.assignValueToDir(res, pointers[2], pointersVal[2])
        self.__nextPointer += 1

      elif operCode == 12: # and
        pointersVal = self.binaryOps(self.returnIsArray(currentQuad.getLeft()), self.returnIsArray(currentQuad.getRight()), self.returnIsArray(currentQuad.getRes()))
        pointers = self.binaryOps(self.getPointerType(pointersVal[0]), self.getPointerType(pointersVal[1]), self.getPointerType(pointersVal[2]))
        if pointers[0][pointersVal[0]] == None or pointers[1][pointersVal[1]] == None:
          raise Exception("ERROR! Comparing null values")
        res = self.andFunc(pointers[0][pointersVal[0]], pointers[1][pointersVal[1]])

        self.assignValueToDir(res, pointers[2], pointersVal[2])
        self.__nextPointer += 1

      elif operCode == 13: # or
        pointersVal = self.binaryOps(self.returnIsArray(currentQuad.getLeft()), self.returnIsArray(currentQuad.getRight()), self.returnIsArray(currentQuad.getRes()))
        pointers = self.binaryOps(self.getPointerType(pointersVal[0]), self.getPointerType(pointersVal[1]), self.getPointerType(pointersVal[2]))
        if pointers[0][pointersVal[0]] == None or pointers[1][pointersVal[1]] == None:
          raise Exception("ERROR! Comparing null values")
        res = self.orFunc(pointers[0][pointersVal[0]], pointers[1][pointersVal[1]])

        self.assignValueToDir(res, pointers[2], pointersVal[2])
        self.__nextPointer += 1

      # TODO PRINT
      elif operCode == 14:
        pointerVal = self.returnIsArray(currentQuad.getRes())
        self.printLine(self.getPointerType(pointerVal)[pointerVal])
        self.__nextPointer += 1

      # TODO READ
      elif operCode == 15:
        pointerVal = self.returnIsArray(currentQuad.getRes())
        toReadPointer = self.getPointerType(pointerVal)

        res = self.readLine(helpers.getTypeV2(pointerVal))
        self.assignValueToDir(int(res), toReadPointer, currentQuad.getRes())

        self.__nextPointer += 1
      
      elif operCode == 16: # goto
        self.__nextPointer = currentQuad.getRes()
      
      elif operCode == 17: # gotoF
        tempAddress = currentQuad.getLeft()
        tempType = self.getPointerType(tempAddress)
        if tempType[tempAddress]:
          self.__nextPointer += 1
        else:
          self.__nextPointer = currentQuad.getRes()
      
      elif operCode == 18: # gotoV
        tempAddress = currentQuad.getLeft()
        tempType = self.getPointerType(tempAddress)
        if not tempType[tempAddress]:
          self.__nextPointer += 1
        else:
          self.__nextPointer = currentQuad.getRes()
        
      elif operCode == 19: # goSub
        global scopePointer
        funcName = list(scopePointer)[0]
        scopePointer[funcName]["arrParam"] = arrParam
        arrParam = []
        VM.get().pushCallStack(scopePointer, self.__nextPointer + 1)
        calledFunction = VM.get().topCallStack()
        self.__nextPointer = calledFunction[funcName]["start"]

      elif operCode == 20: # era
        arrParam = []
        if currentQuad.getRes() != -1:
          self.era(currentQuad.getLeft(), currentQuad.getRight(), currentQuad.getRes())
        else:
          self.era(currentQuad.getLeft(), currentQuad.getRight(), None)


      elif operCode == 21: # param
        paramDir = currentQuad.getLeft()
        paramPoint = self.getPointerType(paramDir)
        self.param(paramPoint, paramDir)
        self.__nextPointer += 1

      elif operCode == 22: # endfunc
        # regresa a migajita de pan
        mem = MainMemory.instantiate()
        funcName = list(self.topCallStack())[0]
        goHere = mem.getLocalTop()["goHere"]
        endingFunc = self.popCallStack()
        endingLocal = mem.popLocalTop()
        self.__nextPointer = goHere
        currentQuad = self.__quadList[self.__nextPointer]
        # it returns to main (or global)

      elif operCode == 23: # verify
        verDir = currentQuad.getLeft()
        verVal = self.getPointerType(verDir)
        valToSend = verVal[verDir]
        upperLim = currentQuad.getRes()
        self.ver(valToSend, upperLim)
        self.__nextPointer += 1

      elif operCode == 24: # return
        retAddress = currentQuad.getRes()
        retPointer = self.getPointerType(retAddress)
        returnVal = self.funcReturn(retPointer, retAddress)

        # regresa a migajita de pan
        mem = MainMemory.instantiate()
        funcName = list(self.topCallStack())[0]
        goHere = mem.getLocalTop()["goHere"]
        endingFunc = self.popCallStack()
        endingLocal = mem.popLocalTop()
        self.__nextPointer = goHere
        currentQuad = self.__quadList[self.__nextPointer]
        if not self.__callStack: # return to sleeping function
          # it returns to main (or global)
          globalDir = currentQuad.getRes()
          if getClassification(globalDir) == 'global':
            globalDirVal = mem.getGlobal()
          elif getClassification(globalDir) == 'local':
            globalDirVal = mem.getLocalTop()
          else:
            globalDirVal = mem.getConstants()
          self.assignValueToDir(returnVal, globalDirVal, globalDir)
          self.__nextPointer += 1
        else:
          currLocal = mem.getLocalTop()
          newDir = currentQuad.getRes()
          newVal = self.getPointerType(newDir)
          self.assignValueToDir(returnVal, newVal, newDir)
          self.__nextPointer += 1

      elif operCode == 25: # end
        # create a space in the stack with the local memory
        self.end()
        print("TERMINO CON EXITO")
        return True
        # self.__nextPointer += 1

      elif operCode == 26: # add from arrays
        # comes from array
        leftDir = currentQuad.getLeft()
        leftVal = self.getPointerType(leftDir)
        resDir = currentQuad.getRes()
        resVal = self.getPointerType(resDir)
        res = self.add(leftVal[leftDir], currentQuad.getRight())
        self.assignValueToDir(res, resVal, resDir)
        self.__nextPointer += 1

      elif operCode == 27: # add from arrays
        # comes from array
        leftDir = currentQuad.getLeft()
        leftVal = self.getPointerType(leftDir)
        resDir = currentQuad.getRes()
        resVal = self.getPointerType(resDir)
        res = self.multiply(leftVal[leftDir], currentQuad.getRight())
        self.assignValueToDir(res, resVal, resDir)
        self.__nextPointer += 1


      operCode = self.__quadList[self.__nextPointer].getOp()