Preliminary code for execution

This commit is contained in:
Mariano Uvalle 2019-04-29 17:56:14 -05:00
parent 240f1c1255
commit 7698c84dcc
13 changed files with 2190 additions and 1693 deletions

185
final_lang/exec.py Normal file
View file

@ -0,0 +1,185 @@
import ast
import sys
normalOperators = ['+', '-', '*', '/', '>', '<',
'<=', '>=', '==', '/=', '.and.', '.or.']
class VarTable:
def __init__(self):
self.symbols = {}
def setVar(self, direction, value):
realDic = 0
if direction[0] == '$':
realDic = int(direction[1:])
elif direction[0] == '*':
realDic = int(self.getVar(direction.replace('*', '$')))
self.symbols[realDic] = value
def getVar(self, direction):
realDic = 0
if direction[0] == '$':
realDic = int(direction[1:])
elif direction[0] == '*':
indirectDic = int(direction[1:])
if indirectDic in self.symbols:
realDic = self.symbols[indirectDic]
else:
raise Exception(f'''
The value at direction {indirectDic} is accessed before initialization.
''')
if realDic in self.symbols:
return self.symbols[realDic]
else:
raise Exception(f'''
The value at direction {realDic} is accessed before initialization.
''')
# An instance of the [VarTable] class.
variables = VarTable()
def isDirection(operand):
if type(operand) is not str:
return False
if len(operand) > 1 and (operand[0] == '$' or operand[0] == '*'):
return True
return False
def isStringLiteral(string):
return string[0] == '\'' and string[len(string) - 1] == '\''
def splitQuad(string):
result = []
buffer = ''
isStringOpen = False
skip = False
tokens = string.split()
for token in tokens:
if isStringOpen:
buffer += ' '
buffer += token
if token[len(token) - 1] == '\'':
result.append(buffer)
isStringOpen = False
else:
if token[0] == '\'':
if token[len(token) - 1] == '\'':
result.append(token)
else:
buffer = token
isStringOpen = True
else:
result.append(token)
return result
def evalOperation(operator, operand1, operand2):
value1 = 0
value2 = 0
if isDirection(operand1):
value1 = variables.getVar(operand1)
else:
value1 = ast.literal_eval(operand1)
if isDirection(operand2):
value2 = variables.getVar(operand2)
else:
value2 = ast.literal_eval(operand2)
if operator == '+':
return value1 + value2
elif operator == '-':
return value1 - value2
elif operator == '*':
return value1 * value2
elif operator == '/':
return value1 / value2
elif operator == '>':
return value1 > value2
elif operator == '<':
return value1 < value2
elif operator == '>=':
return value1 >= value2
elif operator == '<=':
return value1 <= value2
elif operator == '==':
return value1 == value2
elif operator == '/=':
return value1 != value2
elif operator == '.and.':
return value1 and value2
elif operator == '.or.':
return value1 or value2
def execute(quads):
# Index of the quadruplet currently being executes.
currentQuad = 1
while currentQuad <= len(quads):
tokens = splitQuad(quads[currentQuad - 1])
# ---------- print operation
if tokens[0] == 'print':
if tokens[1] == 'endline':
# adding a new line
print('')
elif isStringLiteral(tokens[1]):
print(tokens[1].replace('\'', ''), end='')
else:
value = variables.getVar(tokens[1])
print(value, end='')
# ---------- read operation
elif tokens[0] == 'read':
value = input()
variables.setVar(tokens[1], ast.literal_eval(value))
# ---------- arithmetic and logical operations
elif tokens[0] in normalOperators:
result = evalOperation(tokens[0], tokens[1], tokens[2])
variables.setVar(tokens[3], result)
# ---------- assign operation
elif tokens[0] == '=':
newValue = 0
if isDirection(tokens[1]):
newValue = variables.getVar(tokens[1])
else:
newValue = ast.literal_eval(tokens[1])
variables.setVar(tokens[2], newValue)
# ---------- .not. operation
elif tokens[0] == '.not.':
value = variables.getVar(tokens[1])
variables.setVar(tokens[2], not value)
# ---------- gotoF operation
elif tokens[0] == 'gotoF':
value = variables.getVar(tokens[1])
if value == False:
currentQuad = int(tokens[2])
continue
# ---------- gotoF operation
elif tokens[0] == 'goto':
currentQuad = int(tokens[1])
continue
currentQuad += 1
if (len(sys.argv) > 1):
programName = sys.argv[1]
programFile = open(programName, "r")
quads = programFile.readlines()
quads = list(map(lambda quad: quad.replace('\n', ''), quads))
execute(quads)
programFile.close()
else:
raise Exception('''
No file name was provided.
Please add the file name as a command line argument
Example: exec.py test.fort.out
''')

View file

@ -127,6 +127,7 @@ tokens = [
'moreEquals',
'id',
'program',
'endline',
'end',
'read',
'print',
@ -144,6 +145,7 @@ tokens = [
reserved = {
'program': 'program',
'endline': 'endline',
'end': 'end',
'read': 'read',
'print': 'print',
@ -169,7 +171,7 @@ t_closedParen = r'\)'
t_plus = r'\+'
t_minus = r'-'
t_mul = r'\*'
t_string = r'\'[a-zA-Z0-9 \t\r\n\f()\[\]\&\!\@\#\$\%\^\-\=\+\/\,]*\''
t_string = r'\'[a-zA-Z0-9 \?\:\t\r\n\f()\[\]\&\!\@\#\$\%\^\-\=\+\/\,]*\''
t_or = r'\.or\.'
t_and = r'\.and\.'
t_not = r'\.not\.'
@ -276,12 +278,13 @@ def p_B(p):
|
'''
def p_S(p):
'''
S : Dimensional action_7 equals EA action_8
| id parens
| read RDimensional
| print RDimOrString action_35
| print RDimOrString
| if action_16 Relif ElseOrEmpty end if action_20
| do id action_24 equals EA action_25 coma EA action_26 IntOrEmpty then B action_29 end do
| do then action_21 B action_22 end do
@ -316,8 +319,8 @@ def p_ComaEAOrEmpty(p):
def p_RDimensional(p):
'''
RDimensional : Dimensional
| RDimensional coma Dimensional
RDimensional : Dimensional action_1 action_36
| RDimensional coma Dimensional action_1 action_36
'''
@ -332,6 +335,7 @@ def p_DimOrString(p):
'''
DimOrString : Dimensional action_1 action_33
| string action_34
| endline action_34
'''
@ -438,16 +442,12 @@ def p_EQSymbols(p):
# -----------------------------PARSER ACTIONS---------------------------------
# ----------------------------------------------------------------------------
def p_action_addSymbols(p):
"action_addSymbols :"
for name in p[-1]:
addSymbol(name, p[-4])
###############################################################################
###############################################################################
#WARNING DON'T USE MULTIDIM VARIABLES ELEMENTS ON AE TO CALCULATE ANOTHER INDEX
###############################################################################
###############################################################################
def p_action_1(p):
"action_1 :"
@ -464,11 +464,14 @@ def p_action_1(p):
if dimension2 is not 0:
indirectPointer = symbols[p[-1]]['indirectPointer']
if isDirection(globalIndex1) or isDirection(globalIndex2):
resultQuadruplets.append(f'* {globalIndex1} {dimension1} {indirectPointer}\n')
resultQuadruplets.append(
f'* {globalIndex1} {dimension1} {indirectPointer}\n')
quadrupletIndex += 1
resultQuadruplets.append(f'+ {globalIndex2} {indirectPointer} {indirectPointer}\n')
resultQuadruplets.append(
f'+ {globalIndex2} {indirectPointer} {indirectPointer}\n')
quadrupletIndex += 1
resultQuadruplets.append(f'+ {direction} {indirectPointer} {indirectPointer}\n')
resultQuadruplets.append(
f'+ {direction} {indirectPointer} {indirectPointer}\n')
quadrupletIndex += 1
operandsStack.append(indirectPointer.replace('$', '*'))
else:
@ -477,7 +480,8 @@ def p_action_1(p):
elif dimension1 is not 0:
indirectPointer = symbols[p[-1]]['indirectPointer']
if isDirection(globalIndex1):
resultQuadruplets.append(f'+ {direction} {globalIndex1} {indirectPointer}\n')
resultQuadruplets.append(
f'+ {direction} {globalIndex1} {indirectPointer}\n')
quadrupletIndex += 1
operandsStack.append(indirectPointer.replace('$', '*'))
else:
@ -486,7 +490,7 @@ def p_action_1(p):
else:
operandsStack.append(f'${direction}')
sType = symbols[p[-1]]['type']
#operandsStack.append(f'${direction}')
# operandsStack.append(f'${direction}')
typesStack.append(sType)
globalIndex1 = 0
globalIndex2 = 0
@ -575,11 +579,14 @@ def p_action_7(p):
if dimension2 is not 0:
indirectPointer = symbols[p[-1]]['indirectPointer']
if isDirection(globalIndex1) or isDirection(globalIndex2):
resultQuadruplets.append(f'* {globalIndex1} {dimension1} {indirectPointer}\n')
resultQuadruplets.append(
f'* {globalIndex1} {dimension1} {indirectPointer}\n')
quadrupletIndex += 1
resultQuadruplets.append(f'+ {globalIndex2} {indirectPointer} {indirectPointer}\n')
resultQuadruplets.append(
f'+ {globalIndex2} {indirectPointer} {indirectPointer}\n')
quadrupletIndex += 1
resultQuadruplets.append(f'+ {direction} {indirectPointer} {indirectPointer}\n')
resultQuadruplets.append(
f'+ {direction} {indirectPointer} {indirectPointer}\n')
quadrupletIndex += 1
operandsStack.append(indirectPointer.replace('$', '*'))
else:
@ -588,7 +595,8 @@ def p_action_7(p):
elif dimension1 is not 0:
indirectPointer = symbols[p[-1]]['indirectPointer']
if isDirection(globalIndex1):
resultQuadruplets.append(f'+ {direction} {globalIndex1} {indirectPointer}\n')
resultQuadruplets.append(
f'+ {direction} {globalIndex1} {indirectPointer}\n')
quadrupletIndex += 1
operandsStack.append(indirectPointer.replace('$', '*'))
else:
@ -597,7 +605,7 @@ def p_action_7(p):
else:
operandsStack.append(f'${direction}')
sType = symbols[p[-1]]['type']
#operandsStack.append(f'${direction}')
# operandsStack.append(f'${direction}')
typesStack.append(sType)
globalIndex1 = 0
globalIndex2 = 0
@ -890,6 +898,7 @@ def p_action_29(p):
quadrupletIndex += 1
fillGoto(gotoFPosition, quadrupletIndex)
def p_action_30(p):
"action_30 :"
global globalDimension1
@ -905,6 +914,7 @@ def p_action_31(p):
globalDimension2 = p[-1]
globalDimensionalSize *= p[-1]
def p_action_32(p):
"action_32 :"
global globalDimension1
@ -914,6 +924,7 @@ def p_action_32(p):
globalDimension2 = 0
globalDimensionalSize = 1
def p_action_33(p):
"action_33 :"
global quadrupletIndex
@ -930,10 +941,19 @@ def p_action_34(p):
quadrupletIndex += 1
def p_action_35(p):
"action_35 :"
# def p_action_35(p):
# "action_35 :"
# global quadrupletIndex
# resultQuadruplets.append('print endline\n')
# quadrupletIndex += 1
def p_action_36(p):
"action_36 :"
global quadrupletIndex
resultQuadruplets.append('print "\\n"\n')
operand1 = operandsStack.pop()
type1 = typesStack.pop()
resultQuadruplets.append(f'read {operand1}\n')
quadrupletIndex += 1

2
final_lang/fort.sh Executable file
View file

@ -0,0 +1,2 @@
python3 ./fort.py $1
python3 ./exec.py "${1}.out"

0
final_lang/fort.sh.out Normal file
View file

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

53
final_lang/prueba.fort Normal file
View file

@ -0,0 +1,53 @@
program test
integer :: option, result, x, y, counter, counter2,factorial, exponential
do then
print 'Choose and option'
print '1 - Calculate the factorial of a number'
print '2 - Calculate a number to the power of another number'
print '3 - Calculate e to the power of a number'
read option
if (option == 1) then
print 'Enter the number'
read x
result = 1
do counter = 1, x then
result = result * counter
end do
print 'The result is : ', result
elif (option == 2) then
print 'Enter the base'
read x
print 'Enter the exponent'
read y
result = 1
do counter = 1, y then
result = result * x
end do
print 'The result is : ', result
elif (option == 3) then
print 'Enter the exponent'
read x
result = 0
do counter = 0, 10 then
factorial = 1
do counter2 = 1, counter then
factorial = factorial * counter2
end do
exponential = 1
do counter2 = 1, counter then
exponential = exponential * x
end do
result = result + exponential / factorial
end do
print 'The result is : ', result
else
print 'Not a valid option'
end if
print 'Would you like to make another calculation? (type 1 for yes or 0 for no)'
read option
if (option == 0) then
exit
end if
end do
end program

View file

@ -0,0 +1,94 @@
print 'Choose and option'
print endline
print '1 - Calculate the factorial of a number'
print endline
print '2 - Calculate a number to the power of another number'
print endline
print '3 - Calculate e to the power of a number'
print endline
read $50
== $50 1 $0
gotoF $0 28
print 'Enter the number'
print endline
read $52
= 1 $51
= 1 $54
<= $54 $52 $0
gotoF $0 24
* $51 $54 $1
= $1 $51
+ $54 1 $1
= $1 $54
goto 17
print 'The result is : '
print $51
print endline
goto 88
== $50 2 $1
gotoF $1 49
print 'Enter the base'
print endline
read $52
print 'Enter the exponent'
print endline
read $53
= 1 $51
= 1 $54
<= $54 $53 $1
gotoF $1 45
* $51 $52 $2
= $2 $51
+ $54 1 $2
= $2 $54
goto 38
print 'The result is : '
print $51
print endline
goto 88
== $50 3 $2
gotoF $2 86
print 'Enter the exponent'
print endline
read $52
= 0 $51
= 0 $54
<= $54 10 $2
gotoF $2 82
= 1 $56
= 1 $55
<= $55 $54 $3
gotoF $3 67
* $56 $55 $4
= $4 $56
+ $55 1 $4
= $4 $55
goto 60
= 1 $57
= 1 $55
<= $55 $54 $4
gotoF $4 76
* $57 $52 $5
= $5 $57
+ $55 1 $5
= $5 $55
goto 69
/ $57 $56 $5
+ $51 $5 $6
= $6 $51
+ $54 1 $6
= $6 $54
goto 56
print 'The result is : '
print $51
print endline
goto 88
print 'Not a valid option'
print endline
print 'Would you like to make another calculation? (type 1 for yes or 0 for no)'
print endline
read $50
== $50 0 $6
gotoF $6 94
goto 95
goto 1

View file

@ -1,4 +1,107 @@
+ $58 $51 $0
= $0 $57
= 2 $52
== $53 $55 $0
= 1 $30057
<= $30057 $30053 $0
gotoF $0 25
= 1 $30058
<= $30058 $30054 $1
gotoF $1 22
* 0 100 $30052
+ $30058 $30052 $30052
+ 20052 $30052 $30052
* 0 100 $10050
+ $30058 $10050 $10050
+ 50 $10050 $10050
* 0 100 $20051
+ $30058 $20051 $20051
+ 10051 $20051 $20051
+ *10050 *20051 $2
= $2 *30052
= 2 $20153
+ $30058 1 $2
= $2 $30058
goto 5
+ $30057 1 $2
= $2 $30057
goto 2
= 1 $30057
<= $30057 $30053 $2
gotoF $2 45
= 1 $30058
<= $30058 $30054 $3
gotoF $3 40
* 0 100 $30052
+ $30058 $30052 $30052
+ 20052 $30052 $30052
print *30052
print ' '
print endline
+ $30058 1 $4
= $4 $30058
goto 29
print '
'
print endline
+ $30057 1 $4
= $4 $30057
goto 26
= 1 $30057
<= $30057 $30053 $4
gotoF $4 67
= 1 $30058
<= $30058 $30054 $5
gotoF $5 64
print 'Enter value ('
print $30057
print ','
print $30058
print ') For matrix1
'
print endline
* 0 100 $10050
+ $30058 $10050 $10050
+ 50 $10050 $10050
read *10050
+ $30058 1 $6
= $6 $30058
goto 49
+ $30057 1 $6
= $6 $30057
goto 46
= 1 $30057
<= $30057 $30053 $6
gotoF $6 89
= 1 $30058
<= $30058 $30054 $7
gotoF $7 86
print 'Enter value ('
print $30057
print ','
print $30058
print ') For matrix2
'
print endline
* 0 100 $20051
+ $30058 $20051 $20051
+ 10051 $20051 $20051
read *20051
+ $30058 1 $8
= $8 $30058
goto 71
+ $30057 1 $8
= $8 $30057
goto 68
print 'Enter the rows of the first matrix'
print endline
read $30053
print 'Enter the columns for the first matrix'
print endline
read $30054
print 'Enter the rows of the second matrix'
print endline
read $30055
print 'Enter the columns for the second matrix'
print endline
read $30056
== $30053 $30055 $8
gotoF $8 104
goto 105
goto 101

View file

@ -1,25 +1,8 @@
program test
integer :: a, b, c, d, x, y, z, w
real :: e, f
a = 2 + 4 * (2 + 1) / 3 * b
b = a
if (a > b) then
a = x
elif (a > c) then
a = y
elif (a > d) then
a = z
else
a = w
end if
do then
if (a > b .or. c > b) then
exit
end if
a = a + 1
end do
b = 0
do a = b + 1, 10, 2 then
b = a
end do
integer [2][2] :: a
integer :: i, j
a(0,0) = 1
i = 0
j = 1
print a(j,i)
end program

View file

@ -1,37 +1,7 @@
+ 2 1 $0
* 4 $0 $1
/ $1 3 $0
* $0 $51 $1
+ 2 $1 $0
= $0 $50
= $50 $51
> $50 $51 $0
gotoF $0 12
= $54 $50
goto 21
> $50 $52 $0
gotoF $0 16
= $55 $50
goto 21
> $50 $53 $0
gotoF $0 20
= $56 $50
goto 21
= $57 $50
> $50 $51 $0
> $52 $51 $1
.or. $0 $1 $2
gotoF $2 26
goto 29
+ $50 1 $2
= $2 $50
goto 21
= 0 $51
+ $51 1 $2
= $2 $50
<= $50 10 $2
gotoF $2 38
= $50 $51
+ $50 2 $0
= $0 $50
goto 32
= 1 $50
= 0 $55
= 1 $56
* 0 2 $54
+ $55 $54 $54
+ 50 $54 $54
print *54

View file

@ -1,6 +0,0 @@
program test
integer [10] :: a, b
integer :: c
print a(3), ' ', b(0), ' ', c, ' hello'
c = 2
end program

View file

@ -1,8 +0,0 @@
print $53
print ' '
print $61
print ' '
print $72
print ' hello'
print "\n"
= 2 $72