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

View file

@ -46,12 +46,11 @@ globalIndex2 = 0
# Adds a symbol to the symbols table. # Adds a symbol to the symbols table.
def addSymbol(name, symbolType): def addSymbol(name, symbolType, value = 0):
global currentIndex global currentIndex
initialValue = 0 if symbolType == 'integer' else 0.0
symbols[name] = { symbols[name] = {
'type': symbolType, 'type': symbolType,
'value': initialValue, 'value': value,
'direction': currentIndex, 'direction': currentIndex,
'dimension1': globalDimension1, 'dimension1': globalDimension1,
'dimension2': globalDimension2, 'dimension2': globalDimension2,
@ -223,7 +222,7 @@ lexer = lex.lex()
def p_programa(p): 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') print('+++ Valid program')
@ -267,7 +266,7 @@ def p_Dim(p):
def p_F(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): def p_S(p):
''' '''
S : Dimensional action_7 equals EA action_8 S : Dimensional equals EA action_8
| id parens | id parens action_41
| read RDimensional | read RDimensional
| print RDimOrString | print RDimOrString
| if action_16 Relif ElseOrEmpty end if action_20 | 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 | do then action_21 B action_22 end do
| swap Dimensional coma Dimensional | swap Dimensional coma Dimensional
| exit action_23 | exit action_23
| id openParen closedParen
''' '''
# Adjust the action to support matrices # Adjust the action to support matrices
@ -298,7 +296,7 @@ def p_S(p):
def p_Dimensional(p): def p_Dimensional(p):
''' '''
Dimensional : id DimensionsOrEmpty Dimensional : id DimensionsOrEmpty action_1
''' '''
p[0] = p[1] p[0] = p[1]
@ -319,8 +317,8 @@ def p_ComaEAOrEmpty(p):
def p_RDimensional(p): def p_RDimensional(p):
''' '''
RDimensional : Dimensional action_1 action_36 RDimensional : Dimensional action_36
| RDimensional coma Dimensional action_1 action_36 | RDimensional coma Dimensional action_36
''' '''
@ -333,7 +331,7 @@ def p_RDimOrString(p):
def p_DimOrString(p): def p_DimOrString(p):
''' '''
DimOrString : Dimensional action_1 action_33 DimOrString : Dimensional action_33
| string action_34 | string action_34
| endline action_34 | endline action_34
''' '''
@ -421,7 +419,7 @@ def p_Equality(p):
def p_EItem(p): def p_EItem(p):
''' '''
EItem : Dimensional action_1 EItem : Dimensional
| int action_2 | int action_2
| rea action_2_rea | rea action_2_rea
''' '''
@ -456,13 +454,13 @@ def p_action_1(p):
global quadrupletIndex global quadrupletIndex
global resultQuadruplets global resultQuadruplets
global avail global avail
if p[-1] not in symbols: if p[-2] not in symbols:
raise Exception(f'The variable {p[-1]} was not declared') raise Exception(f'The variable {p[-2]} was not declared')
direction = symbols[p[-1]]['direction'] direction = symbols[p[-2]]['direction']
dimension1 = symbols[p[-1]]['dimension1'] dimension1 = symbols[p[-2]]['dimension1']
dimension2 = symbols[p[-1]]['dimension2'] dimension2 = symbols[p[-2]]['dimension2']
if dimension2 is not 0: if dimension2 is not 0:
indirectPointer = symbols[p[-1]]['indirectPointer'] indirectPointer = symbols[p[-2]]['indirectPointer']
if isDirection(globalIndex1) or isDirection(globalIndex2): if isDirection(globalIndex1) or isDirection(globalIndex2):
resultQuadruplets.append( resultQuadruplets.append(
f'* {globalIndex1} {dimension1} {indirectPointer}\n') f'* {globalIndex1} {dimension1} {indirectPointer}\n')
@ -477,8 +475,10 @@ def p_action_1(p):
else: else:
direction += globalIndex2 + (globalIndex1 * dimension1) direction += globalIndex2 + (globalIndex1 * dimension1)
operandsStack.append(f'${direction}') operandsStack.append(f'${direction}')
globalIndex1 = 0
globalIndex2 = 0
elif dimension1 is not 0: elif dimension1 is not 0:
indirectPointer = symbols[p[-1]]['indirectPointer'] indirectPointer = symbols[p[-2]]['indirectPointer']
if isDirection(globalIndex1): if isDirection(globalIndex1):
resultQuadruplets.append( resultQuadruplets.append(
f'+ {direction} {globalIndex1} {indirectPointer}\n') f'+ {direction} {globalIndex1} {indirectPointer}\n')
@ -487,13 +487,12 @@ def p_action_1(p):
else: else:
direction += globalIndex1 direction += globalIndex1
operandsStack.append(f'${direction}') operandsStack.append(f'${direction}')
globalIndex1 = 0
else: else:
operandsStack.append(f'${direction}') operandsStack.append(f'${direction}')
sType = symbols[p[-1]]['type'] sType = symbols[p[-2]]['type']
# operandsStack.append(f'${direction}') # operandsStack.append(f'${direction}')
typesStack.append(sType) typesStack.append(sType)
globalIndex1 = 0
globalIndex2 = 0
def p_action_2(p): def p_action_2(p):
@ -564,53 +563,6 @@ def p_action_6(p):
avail = [operand1] + avail 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): def p_action_8(p):
"action_8 :" "action_8 :"
global quadrupletIndex global quadrupletIndex
@ -941,28 +893,61 @@ def p_action_34(p):
quadrupletIndex += 1 quadrupletIndex += 1
# def p_action_35(p):
# "action_35 :"
# global quadrupletIndex
# resultQuadruplets.append('print endline\n')
# quadrupletIndex += 1
def p_action_36(p): def p_action_36(p):
"action_36 :" "action_36 :"
global quadrupletIndex global quadrupletIndex
operand1 = operandsStack.pop() operand1 = operandsStack.pop()
type1 = typesStack.pop() typesStack.pop()
resultQuadruplets.append(f'read {operand1}\n') resultQuadruplets.append(f'read {operand1}\n')
quadrupletIndex += 1 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): def p_action_setDim1(p):
"action_setDim1 :" "action_setDim1 :"
global globalIndex1 global globalIndex1
operand1 = operandsStack.pop() operand1 = operandsStack.pop()
type1 = typesStack.pop() type1 = typesStack.pop()
print(f'Tipo del index {type1}')
if (type1 == 'integer'): if (type1 == 'integer'):
globalIndex1 = operand1 globalIndex1 = operand1
else: else:
@ -974,7 +959,6 @@ def p_action_setDim2(p):
global globalIndex2 global globalIndex2
operand1 = operandsStack.pop() operand1 = operandsStack.pop()
type1 = typesStack.pop() type1 = typesStack.pop()
print(f'Tipo del index {type1}')
if (type1 == 'integer'): if (type1 == 'integer'):
globalIndex2 = operand1 globalIndex2 = operand1
else: 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 integer :: option, result, x, y, counter, counter2,factorial, exponential
do then do then
print 'Choose and option' print 'Choose and option', endline
print '1 - Calculate the factorial of a number' print '1 - Calculate the factorial of a number', endline
print '2 - Calculate a number to the power of another number' print '2 - Calculate a number to the power of another number', endline
print '3 - Calculate e to the power of a number' print '3 - Calculate e to the power of a number', endline
read option read option
if (option == 1) then if (option == 1) then
print 'Enter the number' print 'Enter the number', endline
read x read x
result = 1 result = 1
do counter = 1, x then do counter = 1, x then
result = result * counter result = result * counter
end do end do
print 'The result is : ', result print 'The result is : ', result, endline
elif (option == 2) then elif (option == 2) then
print 'Enter the base' print 'Enter the base', endline
read x read x
print 'Enter the exponent' print 'Enter the exponent', endline
read y read y
result = 1 result = 1
do counter = 1, y then do counter = 1, y then
result = result * x result = result * x
end do end do
print 'The result is : ', result print 'The result is : ', result, endline
elif (option == 3) then elif (option == 3) then
print 'Enter the exponent' print 'Enter the exponent', endline
read x read x
result = 0 result = 0
do counter = 0, 10 then do counter = 0, 10 then
@ -40,11 +40,11 @@ do then
end do end do
result = result + exponential / factorial result = result + exponential / factorial
end do end do
print 'The result is : ', result print 'The result is : ', result, endline
else else
print 'Not a valid option' print 'Not a valid option', endline
end if 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 read option
if (option == 0) then if (option == 0) then
exit exit

View file

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

View file

@ -1,7 +1,42 @@
= 1 $50 goto 41
= 0 $55 = 1 $153
= 1 $56 = 0 $151
* 0 2 $54 <= $151 9 $0
+ $55 $54 $54 gotoF $0 21
+ 50 $54 $54 = 0 $152
print *54 <= $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