diff --git a/final_lang/fort.py b/final_lang/fort.py index 9a2be7d..9a68ed7 100644 --- a/final_lang/fort.py +++ b/final_lang/fort.py @@ -46,7 +46,7 @@ globalIndex2 = 0 # Adds a symbol to the symbols table. -def addSymbol(name, symbolType, value = 0): +def addSymbol(name, symbolType, value=0): global currentIndex symbols[name] = { 'type': symbolType, @@ -55,9 +55,7 @@ def addSymbol(name, symbolType, value = 0): 'dimension1': globalDimension1, 'dimension2': globalDimension2, } - if globalDimensionalSize != 1: - symbols[name]['indirectPointer'] = f'${currentIndex + globalDimensionalSize}' - currentIndex += 1 if globalDimensionalSize == 1 else globalDimensionalSize + 1 + currentIndex += globalDimensionalSize # Returns the last item of a list without deleting it. @@ -460,7 +458,7 @@ def p_action_1(p): dimension1 = symbols[p[-2]]['dimension1'] dimension2 = symbols[p[-2]]['dimension2'] if dimension2 is not 0: - indirectPointer = symbols[p[-2]]['indirectPointer'] + indirectPointer = avail.pop(0) if isDirection(globalIndex1) or isDirection(globalIndex2): resultQuadruplets.append( f'* {globalIndex1} {dimension1} {indirectPointer}\n') @@ -478,7 +476,7 @@ def p_action_1(p): globalIndex1 = 0 globalIndex2 = 0 elif dimension1 is not 0: - indirectPointer = symbols[p[-2]]['indirectPointer'] + indirectPointer = avail.pop(0) if isDirection(globalIndex1): resultQuadruplets.append( f'+ {direction} {globalIndex1} {indirectPointer}\n') @@ -530,9 +528,9 @@ def p_action_4(p): operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n') quadrupletIndex += 1 if (isTemp(operand2)): - avail = [operand2] + avail + avail = [operand2.replace('*', '$')] + avail if (isTemp(operand1)): - avail = [operand1] + avail + avail = [operand1.replace('*', '$')] + avail def p_action_5(p): @@ -558,9 +556,9 @@ def p_action_6(p): operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n') quadrupletIndex += 1 if (isTemp(operand2)): - avail = [operand2] + avail + avail = [operand2.replace('*', '$')] + avail if (isTemp(operand1)): - avail = [operand1] + avail + avail = [operand1.replace('*', '$')] + avail def p_action_8(p): @@ -578,7 +576,9 @@ def p_action_8(p): quadrupletIndex += 1 # Return the operand to the availbale if it is a temporal. if (isTemp(operand2)): - avail = [operand2] + avail + avail = [operand2.replace('*', '$')] + avail + if (isTemp(operand1)): + avail = [operand1.replace('*', '$')] + avail def p_action_9(p): @@ -599,9 +599,9 @@ def p_action_9(p): operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n') quadrupletIndex += 1 if (isTemp(operand2)): - avail = [operand2] + avail + avail = [operand2.replace('*', '$')] + avail if (isTemp(operand1)): - avail = [operand1] + avail + avail = [operand1.replace('*', '$')] + avail def p_action_10(p): @@ -627,9 +627,9 @@ def p_action_11(p): operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n') quadrupletIndex += 1 if (isTemp(operand2)): - avail = [operand2] + avail + avail = [operand2.replace('*', '$')] + avail if (isTemp(operand1)): - avail = [operand1] + avail + avail = [operand1.replace('*', '$')] + avail def p_action_12(p): @@ -660,9 +660,9 @@ def p_action_14(p): operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n') quadrupletIndex += 1 if (isTemp(operand2)): - avail = [operand2] + avail + avail = [operand2.replace('*', '$')] + avail if (isTemp(operand1)): - avail = [operand1] + avail + avail = [operand1.replace('*', '$')] + avail def p_action_15(p): @@ -698,7 +698,7 @@ def p_action_17(p): resultQuadruplets.append('gotoF ' + str(operand1) + ' _\n') quadrupletIndex += 1 if (isTemp(operand1)): - avail = [operand1] + avail + avail = [operand1.replace('*', '$')] + avail def p_action_18(p): @@ -786,7 +786,7 @@ def p_action_25(p): quadrupletIndex += 1 # Return the operand to the availbale if it is a temporal. if (isTemp(operand2)): - avail = [operand2] + avail + avail = [operand2.replace('*', '$')] + avail def p_action_26(p): @@ -810,9 +810,9 @@ def p_action_26(p): resultQuadruplets.append(f'gotoF {str(temp)} _\n') quadrupletIndex += 1 if (isTemp(operand2)): - avail = [operand2] + avail + avail = [operand2.replace('*', '$')] + avail if (isTemp(operand1)): - avail = [operand1] + avail + avail = [operand1.replace('*', '$')] + avail def p_action_27(p): @@ -880,10 +880,13 @@ def p_action_32(p): def p_action_33(p): "action_33 :" global quadrupletIndex + global avail operand1 = operandsStack.pop() typesStack.pop() resultQuadruplets.append(f'print {operand1}\n') quadrupletIndex += 1 + if (isTemp(operand1)): + avail = [operand1.replace('*', '$')] + avail def p_action_34(p): @@ -940,8 +943,6 @@ def p_action_41(p): resultQuadruplets.append(f'call {value}\n') quadrupletIndex += 1 - - def p_action_setDim1(p): "action_setDim1 :" diff --git a/final_lang/test2.fort b/final_lang/test2.fort index c0f0aa4..e98a178 100644 --- a/final_lang/test2.fort +++ b/final_lang/test2.fort @@ -1,23 +1,13 @@ program test integer [10][10] :: a integer :: i, j, counter -subroutine fillMatrix - counter = 1 - do i = 0, 9 then - do j = 0, 9 then - a(i,j) = counter - counter = counter + 1 - end do - end do -end subroutine -subroutine printMatrix - do i = 0, 9 then - do j = 0, 9 then - print a(i,j), ' ' - end do - print endline - end do -end subroutine -fillMatrix() -printMatrix() +i = 1 +j = 2 +a(i) = 1 +a(j) = 2 +print 'a(i) = ', a(i), endline +print 'a(j) = ', a(j), endline +a(i) = a(j) + a(i) +print 'a(i) = ', a(i), endline +i = j + 2 end program \ No newline at end of file diff --git a/final_lang/test2.fort.out b/final_lang/test2.fort.out index 45c0246..30983ef 100644 --- a/final_lang/test2.fort.out +++ b/final_lang/test2.fort.out @@ -1,42 +1,42 @@ -goto 41 -= 1 $153 -= 0 $151 -<= $151 9 $0 -gotoF $0 21 -= 0 $152 -<= $152 9 $1 -gotoF $1 18 -* $151 10 $150 -+ $152 $150 $150 -+ 50 $150 $150 -= $153 *150 -+ $153 1 $2 -= $2 $153 -+ $152 1 $2 -= $2 $152 -goto 7 -+ $151 1 $2 -= $2 $151 -goto 4 -goback -= 0 $151 -<= $151 9 $2 -gotoF $2 40 -= 0 $152 -<= $152 9 $3 -gotoF $3 36 -* $151 10 $150 -+ $152 $150 $150 -+ 50 $150 $150 -print *150 -print ' ' -+ $152 1 $4 -= $4 $152 -goto 26 +goto 2 += 1 $150 += 2 $151 +* $150 10 $0 ++ 0 $0 $0 ++ 50 $0 $0 += 1 *0 +* $151 10 $0 ++ 0 $0 $0 ++ 50 $0 $0 += 2 *0 +print 'a(i) = ' +* $150 10 $0 ++ 0 $0 $0 ++ 50 $0 $0 +print *0 print endline -+ $151 1 $4 -= $4 $151 -goto 23 -goback -call 2 -call 22 +print 'a(j) = ' +* $151 10 $0 ++ 0 $0 $0 ++ 50 $0 $0 +print *0 +print endline +* $150 10 $0 ++ 0 $0 $0 ++ 50 $0 $0 +* $151 10 $1 ++ 0 $1 $1 ++ 50 $1 $1 +* $150 10 $2 ++ 0 $2 $2 ++ 50 $2 $2 ++ *1 *2 $3 += $3 *0 +print 'a(i) = ' +* $150 10 $0 ++ 0 $0 $0 ++ 50 $0 $0 +print *0 +print endline ++ $151 2 $0 += $0 $150