Finished the language

This commit is contained in:
Mariano Uvalle 2019-04-30 09:54:10 -05:00
parent 7698c84dcc
commit 1234b8ddcd
10 changed files with 2377 additions and 2349 deletions

View file

@ -4,6 +4,7 @@ import sys
normalOperators = ['+', '-', '*', '/', '>', '<',
'<=', '>=', '==', '/=', '.and.', '.or.']
programStack = []
class VarTable:
def __init__(self):
@ -62,6 +63,9 @@ def splitQuad(string):
skip = False
tokens = string.split()
for token in tokens:
if skip:
skip = False
continue
if isStringOpen:
buffer += ' '
buffer += token
@ -70,6 +74,10 @@ def splitQuad(string):
isStringOpen = False
else:
if token[0] == '\'':
if token == "'":
result.append("' '")
skip = True
continue
if token[len(token) - 1] == '\'':
result.append(token)
else:
@ -165,6 +173,14 @@ def execute(quads):
elif tokens[0] == 'goto':
currentQuad = int(tokens[1])
continue
elif tokens[0] == 'call':
programStack.append(currentQuad + 1)
currentQuad = int(tokens[1])
continue
elif tokens[0] == 'goback':
newQuad = programStack.pop()
currentQuad = newQuad
continue
currentQuad += 1

View file

@ -46,12 +46,11 @@ globalIndex2 = 0
# Adds a symbol to the symbols table.
def addSymbol(name, symbolType):
def addSymbol(name, symbolType, value = 0):
global currentIndex
initialValue = 0 if symbolType == 'integer' else 0.0
symbols[name] = {
'type': symbolType,
'value': initialValue,
'value': value,
'direction': currentIndex,
'dimension1': globalDimension1,
'dimension2': globalDimension2,
@ -223,7 +222,7 @@ lexer = lex.lex()
def p_programa(p):
'''
programa : program id V F B end program
programa : program action_37 id V F action_38 B end program
'''
print('+++ Valid program')
@ -267,7 +266,7 @@ def p_Dim(p):
def p_F(p):
'''
F : F subroutine id B end subroutine
F : F subroutine id action_39 B end subroutine action_40
|
'''
@ -281,8 +280,8 @@ def p_B(p):
def p_S(p):
'''
S : Dimensional action_7 equals EA action_8
| id parens
S : Dimensional equals EA action_8
| id parens action_41
| read RDimensional
| print RDimOrString
| if action_16 Relif ElseOrEmpty end if action_20
@ -290,7 +289,6 @@ def p_S(p):
| do then action_21 B action_22 end do
| swap Dimensional coma Dimensional
| exit action_23
| id openParen closedParen
'''
# Adjust the action to support matrices
@ -298,7 +296,7 @@ def p_S(p):
def p_Dimensional(p):
'''
Dimensional : id DimensionsOrEmpty
Dimensional : id DimensionsOrEmpty action_1
'''
p[0] = p[1]
@ -319,8 +317,8 @@ def p_ComaEAOrEmpty(p):
def p_RDimensional(p):
'''
RDimensional : Dimensional action_1 action_36
| RDimensional coma Dimensional action_1 action_36
RDimensional : Dimensional action_36
| RDimensional coma Dimensional action_36
'''
@ -333,7 +331,7 @@ def p_RDimOrString(p):
def p_DimOrString(p):
'''
DimOrString : Dimensional action_1 action_33
DimOrString : Dimensional action_33
| string action_34
| endline action_34
'''
@ -421,7 +419,7 @@ def p_Equality(p):
def p_EItem(p):
'''
EItem : Dimensional action_1
EItem : Dimensional
| int action_2
| rea action_2_rea
'''
@ -456,13 +454,13 @@ def p_action_1(p):
global quadrupletIndex
global resultQuadruplets
global avail
if p[-1] not in symbols:
raise Exception(f'The variable {p[-1]} was not declared')
direction = symbols[p[-1]]['direction']
dimension1 = symbols[p[-1]]['dimension1']
dimension2 = symbols[p[-1]]['dimension2']
if p[-2] not in symbols:
raise Exception(f'The variable {p[-2]} was not declared')
direction = symbols[p[-2]]['direction']
dimension1 = symbols[p[-2]]['dimension1']
dimension2 = symbols[p[-2]]['dimension2']
if dimension2 is not 0:
indirectPointer = symbols[p[-1]]['indirectPointer']
indirectPointer = symbols[p[-2]]['indirectPointer']
if isDirection(globalIndex1) or isDirection(globalIndex2):
resultQuadruplets.append(
f'* {globalIndex1} {dimension1} {indirectPointer}\n')
@ -477,8 +475,10 @@ def p_action_1(p):
else:
direction += globalIndex2 + (globalIndex1 * dimension1)
operandsStack.append(f'${direction}')
globalIndex1 = 0
globalIndex2 = 0
elif dimension1 is not 0:
indirectPointer = symbols[p[-1]]['indirectPointer']
indirectPointer = symbols[p[-2]]['indirectPointer']
if isDirection(globalIndex1):
resultQuadruplets.append(
f'+ {direction} {globalIndex1} {indirectPointer}\n')
@ -487,13 +487,12 @@ def p_action_1(p):
else:
direction += globalIndex1
operandsStack.append(f'${direction}')
globalIndex1 = 0
else:
operandsStack.append(f'${direction}')
sType = symbols[p[-1]]['type']
sType = symbols[p[-2]]['type']
# operandsStack.append(f'${direction}')
typesStack.append(sType)
globalIndex1 = 0
globalIndex2 = 0
def p_action_2(p):
@ -564,53 +563,6 @@ def p_action_6(p):
avail = [operand1] + avail
def p_action_7(p):
"action_7 :"
global globalIndex1
global globalIndex2
global quadrupletIndex
global resultQuadruplets
global avail
if p[-1] not in symbols:
raise Exception(f'The variable {p[-1]} was not declared')
direction = symbols[p[-1]]['direction']
dimension1 = symbols[p[-1]]['dimension1']
dimension2 = symbols[p[-1]]['dimension2']
if dimension2 is not 0:
indirectPointer = symbols[p[-1]]['indirectPointer']
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)
operandsStack.append(f'${direction}')
elif dimension1 is not 0:
indirectPointer = symbols[p[-1]]['indirectPointer']
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)
globalIndex1 = 0
globalIndex2 = 0
def p_action_8(p):
"action_8 :"
global quadrupletIndex
@ -941,28 +893,61 @@ def p_action_34(p):
quadrupletIndex += 1
# 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
operand1 = operandsStack.pop()
type1 = typesStack.pop()
typesStack.pop()
resultQuadruplets.append(f'read {operand1}\n')
quadrupletIndex += 1
def p_action_37(p):
"action_37 :"
global quadrupletIndex
resultQuadruplets.append('goto _\n')
quadrupletIndex += 1
def p_action_38(p):
"action_38 :"
global quadrupletIndex
fillGoto(1, quadrupletIndex)
def p_action_39(p):
"action_39 :"
global quadrupletIndex
subroutineId = p[-1]
addSymbol(subroutineId, 'method', quadrupletIndex)
def p_action_40(p):
"action_40 :"
global quadrupletIndex
resultQuadruplets.append('goback\n')
quadrupletIndex += 1
def p_action_41(p):
"action_41 :"
global quadrupletIndex
if p[-2] not in symbols:
raise Exception(f'The variable {p[-2]} was not declared')
if symbols[p[-2]]['type'] is not 'method':
raise Exception(f'{p[-2]} is not a function')
value = symbols[p[-2]]['value']
resultQuadruplets.append(f'call {value}\n')
quadrupletIndex += 1
def p_action_setDim1(p):
"action_setDim1 :"
global globalIndex1
operand1 = operandsStack.pop()
type1 = typesStack.pop()
print(f'Tipo del index {type1}')
if (type1 == 'integer'):
globalIndex1 = operand1
else:
@ -974,7 +959,6 @@ def p_action_setDim2(p):
global globalIndex2
operand1 = operandsStack.pop()
type1 = typesStack.pop()
print(f'Tipo del index {type1}')
if (type1 == 'integer'):
globalIndex2 = operand1
else:

File diff suppressed because it is too large Load diff

File diff suppressed because one or more lines are too long

View file

@ -2,31 +2,31 @@ 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'
print 'Choose and option', endline
print '1 - Calculate the factorial of a number', endline
print '2 - Calculate a number to the power of another number', endline
print '3 - Calculate e to the power of a number', endline
read option
if (option == 1) then
print 'Enter the number'
print 'Enter the number', endline
read x
result = 1
do counter = 1, x then
result = result * counter
end do
print 'The result is : ', result
print 'The result is : ', result, endline
elif (option == 2) then
print 'Enter the base'
print 'Enter the base', endline
read x
print 'Enter the exponent'
print 'Enter the exponent', endline
read y
result = 1
do counter = 1, y then
result = result * x
end do
print 'The result is : ', result
print 'The result is : ', result, endline
elif (option == 3) then
print 'Enter the exponent'
print 'Enter the exponent', endline
read x
result = 0
do counter = 0, 10 then
@ -40,11 +40,11 @@ do then
end do
result = result + exponential / factorial
end do
print 'The result is : ', result
print 'The result is : ', result, endline
else
print 'Not a valid option'
print 'Not a valid option', endline
end if
print 'Would you like to make another calculation? (type 1 for yes or 0 for no)'
print 'Would you like to make another calculation? (type 1 for yes or 0 for no)', endline
read option
if (option == 0) then
exit

View file

@ -1,3 +1,4 @@
goto 2
print 'Choose and option'
print endline
print '1 - Calculate the factorial of a number'
@ -8,25 +9,25 @@ print '3 - Calculate e to the power of a number'
print endline
read $50
== $50 1 $0
gotoF $0 28
gotoF $0 29
print 'Enter the number'
print endline
read $52
= 1 $51
= 1 $54
<= $54 $52 $0
gotoF $0 24
gotoF $0 25
* $51 $54 $1
= $1 $51
+ $54 1 $1
= $1 $54
goto 17
goto 18
print 'The result is : '
print $51
print endline
goto 88
goto 89
== $50 2 $1
gotoF $1 49
gotoF $1 50
print 'Enter the base'
print endline
read $52
@ -36,59 +37,59 @@ read $53
= 1 $51
= 1 $54
<= $54 $53 $1
gotoF $1 45
gotoF $1 46
* $51 $52 $2
= $2 $51
+ $54 1 $2
= $2 $54
goto 38
goto 39
print 'The result is : '
print $51
print endline
goto 88
goto 89
== $50 3 $2
gotoF $2 86
gotoF $2 87
print 'Enter the exponent'
print endline
read $52
= 0 $51
= 0 $54
<= $54 10 $2
gotoF $2 82
gotoF $2 83
= 1 $56
= 1 $55
<= $55 $54 $3
gotoF $3 67
gotoF $3 68
* $56 $55 $4
= $4 $56
+ $55 1 $4
= $4 $55
goto 60
goto 61
= 1 $57
= 1 $55
<= $55 $54 $4
gotoF $4 76
gotoF $4 77
* $57 $52 $5
= $5 $57
+ $55 1 $5
= $5 $55
goto 69
goto 70
/ $57 $56 $5
+ $51 $5 $6
= $6 $51
+ $54 1 $6
= $6 $54
goto 56
goto 57
print 'The result is : '
print $51
print endline
goto 88
goto 89
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
gotoF $6 95
goto 96
goto 2

3
final_lang/spaces.fort Normal file
View file

@ -0,0 +1,3 @@
program test
print 'hello', ' ', 'world', endline
end program

View file

@ -0,0 +1,4 @@
print 'hello'
print ' '
print 'world'
print endline

View file

@ -1,8 +1,23 @@
program test
integer [2][2] :: a
integer :: i, j
a(0,0) = 1
i = 0
j = 1
print a(j,i)
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()
end program

View file

@ -1,7 +1,42 @@
= 1 $50
= 0 $55
= 1 $56
* 0 2 $54
+ $55 $54 $54
+ 50 $54 $54
print *54
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
print endline
+ $151 1 $4
= $4 $151
goto 23
goback
call 2
call 22