Added support form arbitrary arithmetic expressions as indices for multi dimension access

This commit is contained in:
Mariano Uvalle 2019-04-25 00:42:17 -05:00
parent f9dfb4be5a
commit 94146e3185
3 changed files with 82 additions and 15 deletions

View file

@ -22,12 +22,14 @@ typesStack = []
jumpsStack = [] jumpsStack = []
exitsStack = [] exitsStack = []
avail = [] avail = []
for i in range(50): for i in range(49):
avail.append('$' + str(i)) avail.append('$' + str(i))
indirectPointer = '$49'
# Operations related to the table of symbols # Operations related to the table of symbols
symbols = {} symbols = {}
# Our variables start at direction 50, the temps take the first 50 directions. # Our variables start at direction 50, the temps take the first 49 directions.
currentIndex = 50 currentIndex = 50
# Multi dimensional variables dimensions just for declaration. # Multi dimensional variables dimensions just for declaration.
@ -78,6 +80,14 @@ def isTemp(operand):
return False return False
def isDirection(operand):
if type(operand) is not str:
return False
if "$" in operand:
return True
return False
def fillGoto(position, fillValue): def fillGoto(position, fillValue):
global resultQuadruplets global resultQuadruplets
# Using position - 1 because the quadruplets start at 1 and not 0. # Using position - 1 because the quadruplets start at 1 and not 0.
@ -434,22 +444,48 @@ def p_action_addSymbols(p):
for name in p[-1]: for name in p[-1]:
addSymbol(name, p[-4]) addSymbol(name, p[-4])
###############################################################################
###############################################################################
#WARNING DON'T USE MULTIDIM VARIABLES ELEMENTS ON AE TO CALCULATE ANOTHER INDEX
###############################################################################
###############################################################################
def p_action_1(p): def p_action_1(p):
"action_1 :" "action_1 :"
global globalIndex1 global globalIndex1
global globalIndex2 global globalIndex2
global quadrupletIndex
global resultQuadruplets
global avail
if p[-1] not in symbols: if p[-1] not in symbols:
raise Exception(f'The variable {p[-1]} was not declared') raise Exception(f'The variable {p[-1]} was not declared')
direction = symbols[p[-1]]['direction'] direction = symbols[p[-1]]['direction']
dimension1 = symbols[p[-1]]['dimension1'] dimension1 = symbols[p[-1]]['dimension1']
dimension2 = symbols[p[-1]]['dimension2'] dimension2 = symbols[p[-1]]['dimension2']
if dimension2 is not 0: if dimension2 is not 0:
if isDirection(globalIndex1) or isDirection(globalIndex2):
resultQuadruplets.append(f'* {globalIndex1} {dimension1} {indirectPointer}\n')
quadrupletIndex += 1
resultQuadruplets.append(f'+ {globalIndex2} {indirectPointer} {indirectPointer}\n')
quadrupletIndex += 1
resultQuadruplets.append(f'+ {direction} {indirectPointer} {indirectPointer}\n')
quadrupletIndex += 1
operandsStack.append(indirectPointer.replace('$', '*'))
else:
direction += globalIndex2 + (globalIndex1 * dimension1) direction += globalIndex2 + (globalIndex1 * dimension1)
elif dimension1 is not 0:
direction += globalIndex1
sType = symbols[p[-1]]['type']
operandsStack.append(f'${direction}') operandsStack.append(f'${direction}')
elif dimension1 is not 0:
if isDirection(globalIndex1):
resultQuadruplets.append(f'+ {direction} {globalIndex1} {indirectPointer}\n')
quadrupletIndex += 1
operandsStack.append(indirectPointer.replace('$', '*'))
else:
direction += globalIndex1
operandsStack.append(f'${direction}')
else:
operandsStack.append(f'${direction}')
sType = symbols[p[-1]]['type']
#operandsStack.append(f'${direction}')
typesStack.append(sType) typesStack.append(sType)
globalIndex1 = 0 globalIndex1 = 0
globalIndex2 = 0 globalIndex2 = 0
@ -527,17 +563,38 @@ def p_action_7(p):
"action_7 :" "action_7 :"
global globalIndex1 global globalIndex1
global globalIndex2 global globalIndex2
global quadrupletIndex
global resultQuadruplets
global avail
if p[-1] not in symbols: if p[-1] not in symbols:
raise Exception(f'The variable {p[-1]} was not declared') raise Exception(f'The variable {p[-1]} was not declared')
direction = symbols[p[-1]]['direction'] direction = symbols[p[-1]]['direction']
dimension1 = symbols[p[-1]]['dimension1'] dimension1 = symbols[p[-1]]['dimension1']
dimension2 = symbols[p[-1]]['dimension2'] dimension2 = symbols[p[-1]]['dimension2']
if dimension2 is not 0: if dimension2 is not 0:
direction += globalIndex2 + (globalIndex1 * dimension2) if isDirection(globalIndex1) or isDirection(globalIndex2):
elif dimension1 is not 0: resultQuadruplets.append(f'* {globalIndex1} {dimension1} {indirectPointer}\n')
direction += globalIndex1 quadrupletIndex += 1
sType = symbols[p[-1]]['type'] resultQuadruplets.append(f'+ {globalIndex2} {indirectPointer} {indirectPointer}\n')
quadrupletIndex += 1
resultQuadruplets.append(f'+ {direction} {indirectPointer} {indirectPointer}\n')
quadrupletIndex += 1
operandsStack.append(indirectPointer.replace('$', '*'))
else:
direction += globalIndex2 + (globalIndex1 * dimension1)
operandsStack.append(f'${direction}') operandsStack.append(f'${direction}')
elif dimension1 is not 0:
if isDirection(globalIndex1):
resultQuadruplets.append(f'+ {direction} {globalIndex1} {indirectPointer}\n')
quadrupletIndex += 1
operandsStack.append(indirectPointer.replace('$', '*'))
else:
direction += globalIndex1
operandsStack.append(f'${direction}')
else:
operandsStack.append(f'${direction}')
sType = symbols[p[-1]]['type']
#operandsStack.append(f'${direction}')
typesStack.append(sType) typesStack.append(sType)
globalIndex1 = 0 globalIndex1 = 0
globalIndex2 = 0 globalIndex2 = 0
@ -855,7 +912,8 @@ def p_action_setDim1(p):
global globalIndex1 global globalIndex1
operand1 = operandsStack.pop() operand1 = operandsStack.pop()
type1 = typesStack.pop() type1 = typesStack.pop()
if (type1 is 'integer'): print(f'Tipo del index {type1}')
if (type1 == 'integer'):
globalIndex1 = operand1 globalIndex1 = operand1
else: else:
raise Exception("Cannot use floating point number as index") raise Exception("Cannot use floating point number as index")
@ -866,7 +924,8 @@ def p_action_setDim2(p):
global globalIndex2 global globalIndex2
operand1 = operandsStack.pop() operand1 = operandsStack.pop()
type1 = typesStack.pop() type1 = typesStack.pop()
if (type1 is 'integer'): print(f'Tipo del index {type1}')
if (type1 == 'integer'):
globalIndex2 = operand1 globalIndex2 = operand1
else: else:
raise Exception("Cannot use floating point number as index") raise Exception("Cannot use floating point number as index")

View file

@ -1,6 +1,8 @@
program test program test
integer :: b integer :: b
integer [10][10] :: a integer [10] :: a
a(2,2) = 1 real :: c
b = a(2,4) do b = 0, 9 then
a(b) = b
end do
end program end program

View file

@ -1,2 +1,8 @@
= 1 $73 = 0 $50
= $75 $50 <= $50 9 $0
gotoF $0 9
+ 51 $50 $49
= $50 *49
+ $50 1 $1
= $1 $50
goto 2