Fixed gitignore adn formatting

This commit is contained in:
Mariano Uvalle 2019-04-22 14:47:49 -05:00
parent 1be80bcbd3
commit 5755fd5582
2 changed files with 105 additions and 50 deletions

8
.gitignore vendored
View file

@ -102,3 +102,11 @@ venv.bak/
# mypy # mypy
.mypy_cache/ .mypy_cache/
# zip files
*.zip
# vscode configuration
.vscode/
*.DS_Store

View file

@ -1,13 +1,14 @@
import ply.lex as lex import ply.lex as lex
import ply.yacc as yacc import ply.yacc as yacc
import sys import sys
# Ignore unresolved import error if present.
from typeValidation import resultingType from typeValidation import resultingType
#---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
#--------------------------------Globals------------------------------------- # --------------------------------Globals-------------------------------------
#---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
kEqualityOperators = ['>', '<', '>=', '<=', '==', '/=',] kEqualityOperators = ['>', '<', '>=', '<=', '==', '/=', ]
resultQuadruplets = [] resultQuadruplets = []
# The first quadruplet will be line 1, that way it will match the lines in # The first quadruplet will be line 1, that way it will match the lines in
@ -29,11 +30,13 @@ 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 50 directions.
currentIndex = 50 currentIndex = 50
#---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
#------------------------------Util methods---------------------------------- # ------------------------------Util methods----------------------------------
#---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# Adds a symbol to the symbols table. # Adds a symbol to the symbols table.
def addSymbol(name, symbolType): def addSymbol(name, symbolType):
global currentIndex global currentIndex
initialValue = 0 if symbolType == 'integer' else 0.0 initialValue = 0 if symbolType == 'integer' else 0.0
@ -45,12 +48,16 @@ def addSymbol(name, symbolType):
currentIndex += 1 currentIndex += 1
# Returns the last item of a list without deleting it. # Returns the last item of a list without deleting it.
def peek(list): def peek(list):
if (len(list) == 0): if (len(list) == 0):
return None return None
return list[len(list) - 1] return list[len(list) - 1]
# Checks whether or not a an operand is a temp variable. # Checks whether or not a an operand is a temp variable.
def isTemp(operand): def isTemp(operand):
if type(operand) is not str: if type(operand) is not str:
return False return False
@ -59,15 +66,17 @@ def isTemp(operand):
return True return True
return False 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.
resultQuadruplets[position-1] = resultQuadruplets[position-1].replace('_', str(fillValue)) resultQuadruplets[position-1] = resultQuadruplets[position -
1].replace('_', str(fillValue))
return return
#---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
#---------------------------------LEXER-------------------------------------- # ---------------------------------LEXER--------------------------------------
#---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
tokens = [ tokens = [
@ -113,20 +122,20 @@ tokens = [
] ]
reserved = { reserved = {
'program' : 'program', 'program': 'program',
'end' : 'end', 'end': 'end',
'read' : 'read', 'read': 'read',
'print' : 'print', 'print': 'print',
'if' : 'if', 'if': 'if',
'then' : 'then', 'then': 'then',
'else' : 'else', 'else': 'else',
'elif' : 'elif', 'elif': 'elif',
'do' : 'do', 'do': 'do',
'swap' : 'swap', 'swap': 'swap',
'exit' : 'exit', 'exit': 'exit',
'integer' : 'integer', 'integer': 'integer',
'real' : 'real', 'real': 'real',
'subroutine' : 'subroutine', 'subroutine': 'subroutine',
} }
t_doubleColon = r'::' t_doubleColon = r'::'
@ -153,11 +162,13 @@ t_moreEquals = r'\>\='
t_more = r'\>' t_more = r'\>'
t_ignore = ' \t\r\n\f\v' t_ignore = ' \t\r\n\f\v'
def t_rea(t): def t_rea(t):
r'\d+\.\d+' r'\d+\.\d+'
t.value = float(t.value) t.value = float(t.value)
return t return t
def t_int(t): def t_int(t):
r'\d+' r'\d+'
t.value = int(t.value) t.value = int(t.value)
@ -167,22 +178,24 @@ def t_int(t):
def t_id(t): def t_id(t):
r'[a-zA-Z_][a-zA-Z_0-9]*' r'[a-zA-Z_][a-zA-Z_0-9]*'
if t.value in reserved: if t.value in reserved:
t.type = reserved[ t.value ] t.type = reserved[t.value]
else: else:
t.type = 'id' t.type = 'id'
return t return t
def t_error(t): def t_error(t):
print("Illegal character!") print("Illegal character!")
print(t) print(t)
t.lexer.skip(1) t.lexer.skip(1)
lexer = lex.lex() lexer = lex.lex()
#---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
#---------------------------------PARSER------------------------------------- # ---------------------------------PARSER-------------------------------------
#---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
def p_programa(p): def p_programa(p):
@ -191,6 +204,7 @@ def p_programa(p):
''' '''
print('+++ Valid program') print('+++ Valid program')
def p_V(p): def p_V(p):
''' '''
V : V Tipo Dim doubleColon Rid V : V Tipo Dim doubleColon Rid
@ -215,6 +229,7 @@ def p_Rid(p):
else: else:
p[0] = p[1] + [p[3]] p[0] = p[1] + [p[3]]
def p_Tipo(p): def p_Tipo(p):
''' '''
Tipo : integer Tipo : integer
@ -223,6 +238,7 @@ def p_Tipo(p):
# p[0] will contain the type in string format # p[0] will contain the type in string format
p[0] = p[1] p[0] = p[1]
def p_Dim(p): def p_Dim(p):
''' '''
Dim : openBra int closedBra Dim : openBra int closedBra
@ -230,18 +246,21 @@ def p_Dim(p):
| |
''' '''
def p_F(p): def p_F(p):
''' '''
F : F subroutine id B end subroutine F : F subroutine id B end subroutine
| |
''' '''
def p_B(p): def p_B(p):
''' '''
B : B S B : B S
| |
''' '''
def p_S(p): def p_S(p):
''' '''
S : Dimensional action_7 equals EA action_8 S : Dimensional action_7 equals EA action_8
@ -256,6 +275,8 @@ def p_S(p):
''' '''
# Adjust the action to support matrices # Adjust the action to support matrices
def p_Dimensional(p): def p_Dimensional(p):
''' '''
Dimensional : id DimensionsOrEmpty Dimensional : id DimensionsOrEmpty
@ -269,48 +290,56 @@ def p_DimensionsOrEmpty(p):
| |
''' '''
def p_ComaEAOrEmpty(p): def p_ComaEAOrEmpty(p):
''' '''
ComaEAOrEmpty : coma EA ComaEAOrEmpty : coma EA
| |
''' '''
def p_RDimensional(p): def p_RDimensional(p):
''' '''
RDimensional : Dimensional RDimensional : Dimensional
| RDimensional coma Dimensional | RDimensional coma Dimensional
''' '''
def p_RDimOrString(p): def p_RDimOrString(p):
''' '''
RDimOrString : DimOrString RDimOrString : DimOrString
| RDimOrString coma DimOrString | RDimOrString coma DimOrString
''' '''
def p_DimOrString(p): def p_DimOrString(p):
''' '''
DimOrString : Dimensional DimOrString : Dimensional
| string | string
''' '''
def p_Relif(p): def p_Relif(p):
''' '''
Relif : openParen EL closedParen action_17 then B Relif : openParen EL closedParen action_17 then B
| Relif elif action_18 openParen EL closedParen action_17 then B | Relif elif action_18 openParen EL closedParen action_17 then B
''' '''
def p_ElseOrEmpty(p): def p_ElseOrEmpty(p):
''' '''
ElseOrEmpty : else action_19 B ElseOrEmpty : else action_19 B
| |
''' '''
def p_IntOrEmpty(p): def p_IntOrEmpty(p):
''' '''
IntOrEmpty : coma int IntOrEmpty : coma int
| |
''' '''
def p_EA(p): def p_EA(p):
''' '''
EA : MultDiv EA : MultDiv
@ -325,12 +354,14 @@ def p_SumOrSub(p):
''' '''
p[0] = p[1] p[0] = p[1]
def p_MultDiv(p): def p_MultDiv(p):
''' '''
MultDiv : EAParens MultDiv : EAParens
| MultDiv MDSymbols action_5 EAParens action_6 | MultDiv MDSymbols action_5 EAParens action_6
''' '''
def p_MDSymbols(p): def p_MDSymbols(p):
''' '''
MDSymbols : mul MDSymbols : mul
@ -338,24 +369,28 @@ def p_MDSymbols(p):
''' '''
p[0] = p[1] p[0] = p[1]
def p_EAParens(p): def p_EAParens(p):
''' '''
EAParens : EItem EAParens : EItem
| openParen EA closedParen | openParen EA closedParen
''' '''
def p_EL(p): def p_EL(p):
''' '''
EL : AND EL : AND
| EL or action_10 AND action_9 | EL or action_10 AND action_9
''' '''
def p_AND(p): def p_AND(p):
''' '''
AND : Equality AND : Equality
| AND and action_12 Equality action_11 | AND and action_12 Equality action_11
''' '''
def p_Equality(p): def p_Equality(p):
''' '''
Equality : EItem EQSymbols action_13 EItem action_14 Equality : EItem EQSymbols action_13 EItem action_14
@ -363,6 +398,7 @@ def p_Equality(p):
| not EL action_15 | not EL action_15
''' '''
def p_EItem(p): def p_EItem(p):
''' '''
EItem : Dimensional action_1 EItem : Dimensional action_1
@ -370,6 +406,7 @@ def p_EItem(p):
| rea action_2_rea | rea action_2_rea
''' '''
def p_EQSymbols(p): def p_EQSymbols(p):
''' '''
EQSymbols : less EQSymbols : less
@ -381,9 +418,10 @@ def p_EQSymbols(p):
''' '''
p[0] = p[1] p[0] = p[1]
#---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
#-----------------------------PARSER ACTIONS--------------------------------- # -----------------------------PARSER ACTIONS---------------------------------
#---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
def p_action_1(p): def p_action_1(p):
"action_1 :" "action_1 :"
@ -400,15 +438,18 @@ def p_action_2(p):
operandsStack.append(p[-1]) operandsStack.append(p[-1])
typesStack.append('integer') typesStack.append('integer')
def p_action_2_rea(p): def p_action_2_rea(p):
"action_2_rea :" "action_2_rea :"
operandsStack.append(p[-1]) operandsStack.append(p[-1])
typesStack.append('real') typesStack.append('real')
def p_action_3(p): def p_action_3(p):
"action_3 :" "action_3 :"
operatorsStack.append(p[-1]) operatorsStack.append(p[-1])
def p_action_4(p): def p_action_4(p):
"action_4 :" "action_4 :"
if (peek(operatorsStack) == '+' or peek(operatorsStack) == '-'): if (peek(operatorsStack) == '+' or peek(operatorsStack) == '-'):
@ -423,7 +464,8 @@ def p_action_4(p):
temp = avail.pop(0) temp = avail.pop(0)
operandsStack.append(temp) operandsStack.append(temp)
typesStack.append(resultType) typesStack.append(resultType)
resultQuadruplets.append(str(operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n') resultQuadruplets.append(str(
operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n')
quadrupletIndex += 1 quadrupletIndex += 1
if (isTemp(operand2)): if (isTemp(operand2)):
avail = [operand2] + avail avail = [operand2] + avail
@ -450,7 +492,8 @@ def p_action_6(p):
temp = avail.pop(0) temp = avail.pop(0)
operandsStack.append(temp) operandsStack.append(temp)
typesStack.append(resultType) typesStack.append(resultType)
resultQuadruplets.append(str(operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n') resultQuadruplets.append(str(
operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n')
quadrupletIndex += 1 quadrupletIndex += 1
if (isTemp(operand2)): if (isTemp(operand2)):
avail = [operand2] + avail avail = [operand2] + avail
@ -458,7 +501,6 @@ def p_action_6(p):
avail = [operand1] + avail avail = [operand1] + avail
def p_action_7(p): def p_action_7(p):
"action_7 :" "action_7 :"
if p[-1] not in symbols: if p[-1] not in symbols:
@ -479,12 +521,14 @@ def p_action_8(p):
type1 = typesStack.pop() type1 = typesStack.pop()
# Result type only gets called to make sure the types are compatible. # Result type only gets called to make sure the types are compatible.
resultingType('=', type1, type2) resultingType('=', type1, type2)
resultQuadruplets.append('= ' + str(operand2) + ' ' + str(operand1) + '\n') resultQuadruplets.append('= ' + str(operand2) +
' ' + str(operand1) + '\n')
quadrupletIndex += 1 quadrupletIndex += 1
# Return the operand to the availbale if it is a temporal. # Return the operand to the availbale if it is a temporal.
if (isTemp(operand2)): if (isTemp(operand2)):
avail = [operand2] + avail avail = [operand2] + avail
def p_action_9(p): def p_action_9(p):
"action_9 :" "action_9 :"
if (peek(operatorsStack) == '.or.'): if (peek(operatorsStack) == '.or.'):
@ -499,7 +543,8 @@ def p_action_9(p):
temp = avail.pop(0) temp = avail.pop(0)
operandsStack.append(temp) operandsStack.append(temp)
typesStack.append(resultType) typesStack.append(resultType)
resultQuadruplets.append(str(operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n') resultQuadruplets.append(str(
operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n')
quadrupletIndex += 1 quadrupletIndex += 1
if (isTemp(operand2)): if (isTemp(operand2)):
avail = [operand2] + avail avail = [operand2] + avail
@ -507,7 +552,6 @@ def p_action_9(p):
avail = [operand1] + avail avail = [operand1] + avail
def p_action_10(p): def p_action_10(p):
"action_10 :" "action_10 :"
operatorsStack.append(p[-1]) operatorsStack.append(p[-1])
@ -527,7 +571,8 @@ def p_action_11(p):
temp = avail.pop(0) temp = avail.pop(0)
operandsStack.append(temp) operandsStack.append(temp)
typesStack.append(resultType) typesStack.append(resultType)
resultQuadruplets.append(str(operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n') resultQuadruplets.append(str(
operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n')
quadrupletIndex += 1 quadrupletIndex += 1
if (isTemp(operand2)): if (isTemp(operand2)):
avail = [operand2] + avail avail = [operand2] + avail
@ -559,7 +604,8 @@ def p_action_14(p):
temp = avail.pop(0) temp = avail.pop(0)
operandsStack.append(temp) operandsStack.append(temp)
typesStack.append(resultType) typesStack.append(resultType)
resultQuadruplets.append(str(operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n') resultQuadruplets.append(str(
operator) + ' ' + str(operand1) + ' ' + str(operand2) + ' ' + str(temp) + '\n')
quadrupletIndex += 1 quadrupletIndex += 1
if (isTemp(operand2)): if (isTemp(operand2)):
avail = [operand2] + avail avail = [operand2] + avail
@ -573,13 +619,15 @@ def p_action_15(p):
raise Exception("Cannot use .not. with non boolean") raise Exception("Cannot use .not. with non boolean")
global quadrupletIndex global quadrupletIndex
operand1 = peek(operandsStack) operand1 = peek(operandsStack)
resultQuadruplets.append('.not. ' + str(operand1) + ' ' + str(operand1) + '\n') resultQuadruplets.append('.not. ' + str(operand1) +
' ' + str(operand1) + '\n')
def p_action_16(p): def p_action_16(p):
"action_16 :" "action_16 :"
jumpsStack.append('$') jumpsStack.append('$')
def p_action_17(p): def p_action_17(p):
"action_17 :" "action_17 :"
global quadrupletIndex global quadrupletIndex
@ -587,7 +635,8 @@ def p_action_17(p):
operand1 = operandsStack.pop() operand1 = operandsStack.pop()
type1 = typesStack.pop() type1 = typesStack.pop()
if (type1 is not 'bool'): if (type1 is not 'bool'):
raise Exception('Can\'t use non logical expression as conditional for if') raise Exception(
'Can\'t use non logical expression as conditional for if')
jumpsStack.append(quadrupletIndex) jumpsStack.append(quadrupletIndex)
resultQuadruplets.append('gotoF ' + str(operand1) + ' _\n') resultQuadruplets.append('gotoF ' + str(operand1) + ' _\n')
quadrupletIndex += 1 quadrupletIndex += 1
@ -651,17 +700,17 @@ def p_action_23(p):
quadrupletIndex += 1 quadrupletIndex += 1
def p_error(p): def p_error(p):
print('XXX Invalid program') print('XXX Invalid program')
print(p) print(p)
parser = yacc.yacc() parser = yacc.yacc()
#---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
#--------------------INTERMEDIATE CODE FILE GENERATION----------------------- # --------------------INTERMEDIATE CODE FILE GENERATION-----------------------
#---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
if (len(sys.argv) > 1): if (len(sys.argv) > 1):
programName = sys.argv[1] programName = sys.argv[1]
@ -684,5 +733,3 @@ else:
Example: fort.py test.fort Example: fort.py test.fort
''') ''')