Added package for type checking, added type checking for Arithmetic operations

This commit is contained in:
Mariano Uvalle 2019-04-12 01:00:45 +00:00
parent a64bd11a78
commit e2a3920ef0
8 changed files with 520 additions and 227 deletions

View file

@ -1,7 +1,7 @@
import ply.lex as lex import ply.lex as lex
import ply.yacc as yacc import ply.yacc as yacc
import sys import sys
from stack import Stack from typeValidation import resultingType
resultQuadruplets = [] resultQuadruplets = []
quadrupletIndex = 1 quadrupletIndex = 1
@ -14,11 +14,12 @@ jumpsStack = []
exitsStack = [] exitsStack = []
avail = [] avail = []
for i in range(50): for i in range(50):
avail.append('T' + str(i)) avail.append('$' + str(i))
# Operations related to the table of symbols # Operations related to the table of symbols
symbols = {} symbols = {}
currentIndex = 0 # Our variables start at direction 50, the temps take the first 50 directions.
currentIndex = 50
# Implementation using a dictionary # Implementation using a dictionary
def addSymbol(name, symbolType): def addSymbol(name, symbolType):
@ -30,12 +31,18 @@ def addSymbol(name, symbolType):
'direction': currentIndex 'direction': currentIndex
} }
currentIndex += 1 currentIndex += 1
# 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.
def isTemp(operan):
return
tokens = [ tokens = [
'doubleColon', 'doubleColon',
'coma', 'coma',
@ -77,6 +84,7 @@ tokens = [
'real', 'real',
'subroutine', 'subroutine',
] ]
reserved = { reserved = {
'program' : 'program', 'program' : 'program',
'end' : 'end', 'end' : 'end',
@ -326,7 +334,7 @@ def p_EItem(p):
''' '''
EItem : Dimensional action_1 EItem : Dimensional action_1
| int action_2 | int action_2
| rea action_2 | rea action_2_rea
''' '''
def p_EQSymbols(p): def p_EQSymbols(p):
@ -342,12 +350,23 @@ def p_EQSymbols(p):
def p_action_1(p): def p_action_1(p):
"action_1 :" "action_1 :"
operandsStack.append(p[-1]) if p[-1] not in symbols:
raise Exception(f'The variable {p[-1]} was not declared')
direction = symbols[p[-1]]['direction']
sType = symbols[p[-1]]['type']
operandsStack.append(f'${direction}')
typesStack.append(sType)
def p_action_2(p): def p_action_2(p):
"action_2 :" "action_2 :"
operandsStack.append(p[-1]) operandsStack.append(p[-1])
typesStack.append('integer')
def p_action_2_rea(p):
"action_2_rea :"
operandsStack.append(p[-1])
typesStack.append('real')
def p_action_3(p): def p_action_3(p):
"action_3 :" "action_3 :"
@ -360,8 +379,12 @@ def p_action_4(p):
operator = operatorsStack.pop() operator = operatorsStack.pop()
operand2 = operandsStack.pop() operand2 = operandsStack.pop()
operand1 = operandsStack.pop() operand1 = operandsStack.pop()
type2 = typesStack.pop()
type1 = typesStack.pop()
resultType = resultingType(operator, type1, type2)
temp = avail.pop(0) temp = avail.pop(0)
operandsStack.append(temp) operandsStack.append(temp)
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
@ -378,8 +401,12 @@ def p_action_6(p):
operator = operatorsStack.pop() operator = operatorsStack.pop()
operand2 = operandsStack.pop() operand2 = operandsStack.pop()
operand1 = operandsStack.pop() operand1 = operandsStack.pop()
type2 = typesStack.pop()
type1 = typesStack.pop()
resultType = resultingType(operator, type1, type2)
temp = avail.pop(0) temp = avail.pop(0)
operandsStack.append(temp) operandsStack.append(temp)
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
@ -387,14 +414,25 @@ def p_action_6(p):
def p_action_7(p): def p_action_7(p):
"action_7 :" "action_7 :"
operandsStack.append(p[-1]) if p[-1] not in symbols:
raise Exception(f'The variable {p[-1]} was not declared')
direction = symbols[p[-1]]['direction']
sType = symbols[p[-1]]['type']
operandsStack.append(f'${direction}')
typesStack.append(sType)
def p_action_8(p): def p_action_8(p):
"action_8 :" "action_8 :"
operand2 = operandsStack.pop() operand2 = operandsStack.pop()
operand1 = operandsStack.pop() operand1 = operandsStack.pop()
type2 = typesStack.pop()
type1 = typesStack.pop()
# Result type only gets called to make sure the types are compatible.
resultingType('=', type1, type2)
resultQuadruplets.append('= ' + str(operand2) + ' ' + str(operand1) + '\n') resultQuadruplets.append('= ' + str(operand2) + ' ' + str(operand1) + '\n')
def p_error(p): def p_error(p):
print('XXX Invalid program') print('XXX Invalid program')
print(p) print(p)

View file

@ -62,7 +62,7 @@ Rule 56 Equality -> openParen EL closedParen
Rule 57 Equality -> not EL Rule 57 Equality -> not EL
Rule 58 EItem -> Dimensional action_1 Rule 58 EItem -> Dimensional action_1
Rule 59 EItem -> int action_2 Rule 59 EItem -> int action_2
Rule 60 EItem -> rea action_2 Rule 60 EItem -> rea action_2_rea
Rule 61 EQSymbols -> less Rule 61 EQSymbols -> less
Rule 62 EQSymbols -> more Rule 62 EQSymbols -> more
Rule 63 EQSymbols -> doubleEquals Rule 63 EQSymbols -> doubleEquals
@ -71,12 +71,13 @@ Rule 65 EQSymbols -> lessEquals
Rule 66 EQSymbols -> moreEquals Rule 66 EQSymbols -> moreEquals
Rule 67 action_1 -> <empty> Rule 67 action_1 -> <empty>
Rule 68 action_2 -> <empty> Rule 68 action_2 -> <empty>
Rule 69 action_3 -> <empty> Rule 69 action_2_rea -> <empty>
Rule 70 action_4 -> <empty> Rule 70 action_3 -> <empty>
Rule 71 action_5 -> <empty> Rule 71 action_4 -> <empty>
Rule 72 action_6 -> <empty> Rule 72 action_5 -> <empty>
Rule 73 action_7 -> <empty> Rule 73 action_6 -> <empty>
Rule 74 action_8 -> <empty> Rule 74 action_7 -> <empty>
Rule 75 action_8 -> <empty>
Terminals, with rules where they appear Terminals, with rules where they appear
@ -150,7 +151,8 @@ SumOrSub : 42
Tipo : 2 Tipo : 2
V : 1 2 V : 1 2
action_1 : 58 action_1 : 58
action_2 : 59 60 action_2 : 59
action_2_rea : 60
action_3 : 42 action_3 : 42
action_4 : 42 action_4 : 42
action_5 : 46 action_5 : 46
@ -362,9 +364,9 @@ state 15
state 16 state 16
(15) S -> Dimensional . action_7 equals EA action_8 (15) S -> Dimensional . action_7 equals EA action_8
(73) action_7 -> . (74) action_7 -> .
equals reduce using rule 73 (action_7 -> .) equals reduce using rule 74 (action_7 -> .)
action_7 shift and go to state 30 action_7 shift and go to state 30
@ -534,7 +536,7 @@ state 28
(50) EAParens -> . openParen EA closedParen (50) EAParens -> . openParen EA closedParen
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
openParen shift and go to state 47 openParen shift and go to state 47
@ -726,7 +728,7 @@ state 39
(57) Equality -> . not EL (57) Equality -> . not EL
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
openParen shift and go to state 61 openParen shift and go to state 61
@ -856,7 +858,7 @@ state 47
(50) EAParens -> . openParen EA closedParen (50) EAParens -> . openParen EA closedParen
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
openParen shift and go to state 47 openParen shift and go to state 47
@ -1028,36 +1030,36 @@ state 53
state 54 state 54
(60) EItem -> rea . action_2 (60) EItem -> rea . action_2_rea
(68) action_2 -> . (69) action_2_rea -> .
mul reduce using rule 68 (action_2 -> .) mul reduce using rule 69 (action_2_rea -> .)
div reduce using rule 68 (action_2 -> .) div reduce using rule 69 (action_2_rea -> .)
coma reduce using rule 68 (action_2 -> .) coma reduce using rule 69 (action_2_rea -> .)
plus reduce using rule 68 (action_2 -> .) plus reduce using rule 69 (action_2_rea -> .)
minus reduce using rule 68 (action_2 -> .) minus reduce using rule 69 (action_2_rea -> .)
closedParen reduce using rule 68 (action_2 -> .) closedParen reduce using rule 69 (action_2_rea -> .)
less reduce using rule 68 (action_2 -> .) less reduce using rule 69 (action_2_rea -> .)
more reduce using rule 68 (action_2 -> .) more reduce using rule 69 (action_2_rea -> .)
doubleEquals reduce using rule 68 (action_2 -> .) doubleEquals reduce using rule 69 (action_2_rea -> .)
notEquals reduce using rule 68 (action_2 -> .) notEquals reduce using rule 69 (action_2_rea -> .)
lessEquals reduce using rule 68 (action_2 -> .) lessEquals reduce using rule 69 (action_2_rea -> .)
moreEquals reduce using rule 68 (action_2 -> .) moreEquals reduce using rule 69 (action_2_rea -> .)
end reduce using rule 68 (action_2 -> .) end reduce using rule 69 (action_2_rea -> .)
id reduce using rule 68 (action_2 -> .) id reduce using rule 69 (action_2_rea -> .)
read reduce using rule 68 (action_2 -> .) read reduce using rule 69 (action_2_rea -> .)
print reduce using rule 68 (action_2 -> .) print reduce using rule 69 (action_2_rea -> .)
if reduce using rule 68 (action_2 -> .) if reduce using rule 69 (action_2_rea -> .)
do reduce using rule 68 (action_2 -> .) do reduce using rule 69 (action_2_rea -> .)
swap reduce using rule 68 (action_2 -> .) swap reduce using rule 69 (action_2_rea -> .)
exit reduce using rule 68 (action_2 -> .) exit reduce using rule 69 (action_2_rea -> .)
elif reduce using rule 68 (action_2 -> .) elif reduce using rule 69 (action_2_rea -> .)
else reduce using rule 68 (action_2 -> .) else reduce using rule 69 (action_2_rea -> .)
and reduce using rule 68 (action_2 -> .) and reduce using rule 69 (action_2_rea -> .)
or reduce using rule 68 (action_2 -> .) or reduce using rule 69 (action_2_rea -> .)
then reduce using rule 68 (action_2 -> .) then reduce using rule 69 (action_2_rea -> .)
action_2 shift and go to state 84 action_2_rea shift and go to state 84
state 55 state 55
@ -1070,7 +1072,7 @@ state 55
(50) EAParens -> . openParen EA closedParen (50) EAParens -> . openParen EA closedParen
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
openParen shift and go to state 47 openParen shift and go to state 47
@ -1149,7 +1151,7 @@ state 61
(57) Equality -> . not EL (57) Equality -> . not EL
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
openParen shift and go to state 61 openParen shift and go to state 61
@ -1226,7 +1228,7 @@ state 66
(57) Equality -> . not EL (57) Equality -> . not EL
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
openParen shift and go to state 61 openParen shift and go to state 61
@ -1252,7 +1254,7 @@ state 67
(50) EAParens -> . openParen EA closedParen (50) EAParens -> . openParen EA closedParen
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
openParen shift and go to state 47 openParen shift and go to state 47
@ -1346,12 +1348,12 @@ state 74
state 75 state 75
(42) EA -> EA SumOrSub . action_3 MultDiv action_4 (42) EA -> EA SumOrSub . action_3 MultDiv action_4
(69) action_3 -> . (70) action_3 -> .
openParen reduce using rule 69 (action_3 -> .) openParen reduce using rule 70 (action_3 -> .)
int reduce using rule 69 (action_3 -> .) int reduce using rule 70 (action_3 -> .)
rea reduce using rule 69 (action_3 -> .) rea reduce using rule 70 (action_3 -> .)
id reduce using rule 69 (action_3 -> .) id reduce using rule 70 (action_3 -> .)
action_3 shift and go to state 111 action_3 shift and go to state 111
@ -1366,7 +1368,7 @@ state 76
(50) EAParens -> . openParen EA closedParen (50) EAParens -> . openParen EA closedParen
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
openParen shift and go to state 47 openParen shift and go to state 47
@ -1403,12 +1405,12 @@ state 78
state 79 state 79
(46) MultDiv -> MultDiv MDSymbols . action_5 EAParens action_6 (46) MultDiv -> MultDiv MDSymbols . action_5 EAParens action_6
(71) action_5 -> . (72) action_5 -> .
openParen reduce using rule 71 (action_5 -> .) openParen reduce using rule 72 (action_5 -> .)
int reduce using rule 71 (action_5 -> .) int reduce using rule 72 (action_5 -> .)
rea reduce using rule 71 (action_5 -> .) rea reduce using rule 72 (action_5 -> .)
id reduce using rule 71 (action_5 -> .) id reduce using rule 72 (action_5 -> .)
action_5 shift and go to state 113 action_5 shift and go to state 113
@ -1496,53 +1498,53 @@ state 83
state 84 state 84
(60) EItem -> rea action_2 . (60) EItem -> rea action_2_rea .
mul reduce using rule 60 (EItem -> rea action_2 .) mul reduce using rule 60 (EItem -> rea action_2_rea .)
div reduce using rule 60 (EItem -> rea action_2 .) div reduce using rule 60 (EItem -> rea action_2_rea .)
coma reduce using rule 60 (EItem -> rea action_2 .) coma reduce using rule 60 (EItem -> rea action_2_rea .)
plus reduce using rule 60 (EItem -> rea action_2 .) plus reduce using rule 60 (EItem -> rea action_2_rea .)
minus reduce using rule 60 (EItem -> rea action_2 .) minus reduce using rule 60 (EItem -> rea action_2_rea .)
closedParen reduce using rule 60 (EItem -> rea action_2 .) closedParen reduce using rule 60 (EItem -> rea action_2_rea .)
less reduce using rule 60 (EItem -> rea action_2 .) less reduce using rule 60 (EItem -> rea action_2_rea .)
more reduce using rule 60 (EItem -> rea action_2 .) more reduce using rule 60 (EItem -> rea action_2_rea .)
doubleEquals reduce using rule 60 (EItem -> rea action_2 .) doubleEquals reduce using rule 60 (EItem -> rea action_2_rea .)
notEquals reduce using rule 60 (EItem -> rea action_2 .) notEquals reduce using rule 60 (EItem -> rea action_2_rea .)
lessEquals reduce using rule 60 (EItem -> rea action_2 .) lessEquals reduce using rule 60 (EItem -> rea action_2_rea .)
moreEquals reduce using rule 60 (EItem -> rea action_2 .) moreEquals reduce using rule 60 (EItem -> rea action_2_rea .)
end reduce using rule 60 (EItem -> rea action_2 .) end reduce using rule 60 (EItem -> rea action_2_rea .)
id reduce using rule 60 (EItem -> rea action_2 .) id reduce using rule 60 (EItem -> rea action_2_rea .)
read reduce using rule 60 (EItem -> rea action_2 .) read reduce using rule 60 (EItem -> rea action_2_rea .)
print reduce using rule 60 (EItem -> rea action_2 .) print reduce using rule 60 (EItem -> rea action_2_rea .)
if reduce using rule 60 (EItem -> rea action_2 .) if reduce using rule 60 (EItem -> rea action_2_rea .)
do reduce using rule 60 (EItem -> rea action_2 .) do reduce using rule 60 (EItem -> rea action_2_rea .)
swap reduce using rule 60 (EItem -> rea action_2 .) swap reduce using rule 60 (EItem -> rea action_2_rea .)
exit reduce using rule 60 (EItem -> rea action_2 .) exit reduce using rule 60 (EItem -> rea action_2_rea .)
elif reduce using rule 60 (EItem -> rea action_2 .) elif reduce using rule 60 (EItem -> rea action_2_rea .)
else reduce using rule 60 (EItem -> rea action_2 .) else reduce using rule 60 (EItem -> rea action_2_rea .)
and reduce using rule 60 (EItem -> rea action_2 .) and reduce using rule 60 (EItem -> rea action_2_rea .)
or reduce using rule 60 (EItem -> rea action_2 .) or reduce using rule 60 (EItem -> rea action_2_rea .)
then reduce using rule 60 (EItem -> rea action_2 .) then reduce using rule 60 (EItem -> rea action_2_rea .)
state 85 state 85
(15) S -> Dimensional action_7 equals EA . action_8 (15) S -> Dimensional action_7 equals EA . action_8
(42) EA -> EA . SumOrSub action_3 MultDiv action_4 (42) EA -> EA . SumOrSub action_3 MultDiv action_4
(74) action_8 -> . (75) action_8 -> .
(43) SumOrSub -> . plus (43) SumOrSub -> . plus
(44) SumOrSub -> . minus (44) SumOrSub -> . minus
end reduce using rule 74 (action_8 -> .) end reduce using rule 75 (action_8 -> .)
id reduce using rule 74 (action_8 -> .) id reduce using rule 75 (action_8 -> .)
read reduce using rule 74 (action_8 -> .) read reduce using rule 75 (action_8 -> .)
print reduce using rule 74 (action_8 -> .) print reduce using rule 75 (action_8 -> .)
if reduce using rule 74 (action_8 -> .) if reduce using rule 75 (action_8 -> .)
do reduce using rule 74 (action_8 -> .) do reduce using rule 75 (action_8 -> .)
swap reduce using rule 74 (action_8 -> .) swap reduce using rule 75 (action_8 -> .)
exit reduce using rule 74 (action_8 -> .) exit reduce using rule 75 (action_8 -> .)
elif reduce using rule 74 (action_8 -> .) elif reduce using rule 75 (action_8 -> .)
else reduce using rule 74 (action_8 -> .) else reduce using rule 75 (action_8 -> .)
plus shift and go to state 77 plus shift and go to state 77
minus shift and go to state 78 minus shift and go to state 78
@ -1602,7 +1604,7 @@ state 89
(57) Equality -> . not EL (57) Equality -> . not EL
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
openParen shift and go to state 61 openParen shift and go to state 61
@ -1670,7 +1672,7 @@ state 93
(57) Equality -> . not EL (57) Equality -> . not EL
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
openParen shift and go to state 61 openParen shift and go to state 61
@ -1692,7 +1694,7 @@ state 94
(57) Equality -> . not EL (57) Equality -> . not EL
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
openParen shift and go to state 61 openParen shift and go to state 61
@ -1710,7 +1712,7 @@ state 95
(55) Equality -> EItem EQSymbols . EItem (55) Equality -> EItem EQSymbols . EItem
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
int shift and go to state 53 int shift and go to state 53
@ -1927,7 +1929,7 @@ state 111
(50) EAParens -> . openParen EA closedParen (50) EAParens -> . openParen EA closedParen
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
openParen shift and go to state 47 openParen shift and go to state 47
@ -1960,7 +1962,7 @@ state 113
(50) EAParens -> . openParen EA closedParen (50) EAParens -> . openParen EA closedParen
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
openParen shift and go to state 47 openParen shift and go to state 47
@ -2083,7 +2085,7 @@ state 122
(50) EAParens -> . openParen EA closedParen (50) EAParens -> . openParen EA closedParen
(58) EItem -> . Dimensional action_1 (58) EItem -> . Dimensional action_1
(59) EItem -> . int action_2 (59) EItem -> . int action_2
(60) EItem -> . rea action_2 (60) EItem -> . rea action_2_rea
(24) Dimensional -> . id DimensionsOrEmpty (24) Dimensional -> . id DimensionsOrEmpty
openParen shift and go to state 47 openParen shift and go to state 47
@ -2124,25 +2126,25 @@ state 125
(42) EA -> EA SumOrSub action_3 MultDiv . action_4 (42) EA -> EA SumOrSub action_3 MultDiv . action_4
(46) MultDiv -> MultDiv . MDSymbols action_5 EAParens action_6 (46) MultDiv -> MultDiv . MDSymbols action_5 EAParens action_6
(70) action_4 -> . (71) action_4 -> .
(47) MDSymbols -> . mul (47) MDSymbols -> . mul
(48) MDSymbols -> . div (48) MDSymbols -> . div
coma reduce using rule 70 (action_4 -> .) coma reduce using rule 71 (action_4 -> .)
plus reduce using rule 70 (action_4 -> .) plus reduce using rule 71 (action_4 -> .)
minus reduce using rule 70 (action_4 -> .) minus reduce using rule 71 (action_4 -> .)
closedParen reduce using rule 70 (action_4 -> .) closedParen reduce using rule 71 (action_4 -> .)
end reduce using rule 70 (action_4 -> .) end reduce using rule 71 (action_4 -> .)
id reduce using rule 70 (action_4 -> .) id reduce using rule 71 (action_4 -> .)
read reduce using rule 70 (action_4 -> .) read reduce using rule 71 (action_4 -> .)
print reduce using rule 70 (action_4 -> .) print reduce using rule 71 (action_4 -> .)
if reduce using rule 70 (action_4 -> .) if reduce using rule 71 (action_4 -> .)
do reduce using rule 70 (action_4 -> .) do reduce using rule 71 (action_4 -> .)
swap reduce using rule 70 (action_4 -> .) swap reduce using rule 71 (action_4 -> .)
exit reduce using rule 70 (action_4 -> .) exit reduce using rule 71 (action_4 -> .)
elif reduce using rule 70 (action_4 -> .) elif reduce using rule 71 (action_4 -> .)
else reduce using rule 70 (action_4 -> .) else reduce using rule 71 (action_4 -> .)
then reduce using rule 70 (action_4 -> .) then reduce using rule 71 (action_4 -> .)
mul shift and go to state 80 mul shift and go to state 80
div shift and go to state 81 div shift and go to state 81
@ -2152,25 +2154,25 @@ state 125
state 126 state 126
(46) MultDiv -> MultDiv MDSymbols action_5 EAParens . action_6 (46) MultDiv -> MultDiv MDSymbols action_5 EAParens . action_6
(72) action_6 -> . (73) action_6 -> .
mul reduce using rule 72 (action_6 -> .) mul reduce using rule 73 (action_6 -> .)
div reduce using rule 72 (action_6 -> .) div reduce using rule 73 (action_6 -> .)
coma reduce using rule 72 (action_6 -> .) coma reduce using rule 73 (action_6 -> .)
plus reduce using rule 72 (action_6 -> .) plus reduce using rule 73 (action_6 -> .)
minus reduce using rule 72 (action_6 -> .) minus reduce using rule 73 (action_6 -> .)
closedParen reduce using rule 72 (action_6 -> .) closedParen reduce using rule 73 (action_6 -> .)
end reduce using rule 72 (action_6 -> .) end reduce using rule 73 (action_6 -> .)
id reduce using rule 72 (action_6 -> .) id reduce using rule 73 (action_6 -> .)
read reduce using rule 72 (action_6 -> .) read reduce using rule 73 (action_6 -> .)
print reduce using rule 72 (action_6 -> .) print reduce using rule 73 (action_6 -> .)
if reduce using rule 72 (action_6 -> .) if reduce using rule 73 (action_6 -> .)
do reduce using rule 72 (action_6 -> .) do reduce using rule 73 (action_6 -> .)
swap reduce using rule 72 (action_6 -> .) swap reduce using rule 73 (action_6 -> .)
exit reduce using rule 72 (action_6 -> .) exit reduce using rule 73 (action_6 -> .)
elif reduce using rule 72 (action_6 -> .) elif reduce using rule 73 (action_6 -> .)
else reduce using rule 72 (action_6 -> .) else reduce using rule 73 (action_6 -> .)
then reduce using rule 72 (action_6 -> .) then reduce using rule 73 (action_6 -> .)
action_6 shift and go to state 131 action_6 shift and go to state 131

File diff suppressed because one or more lines are too long

View file

@ -17,4 +17,6 @@ class Stack:
return self.items[len(self.items)-1] return self.items[len(self.items)-1]
def size(self): def size(self):
return len(self.items) return len(self.items)
hello = 'hello'

View file

@ -1,6 +1,6 @@
program test program test
integer :: a, b integer :: a, b
real :: c real :: c
a = (2 + 1) + 4 * 2 / 3 * b a = 2 + 4 * (2 + 1) / 3 * b
b = a b = a
end program end program

View file

@ -1,7 +1,7 @@
+ 2 1 T0 + 2 1 $0
* 4 2 T1 * 4 $0 $1
/ T1 3 T2 / $1 3 $2
* T2 b T3 * $2 $51 $3
+ T0 T3 T4 + 2 $3 $4
= T4 a = $4 $50
= a b = $50 $51

View file

@ -0,0 +1,125 @@
typeValidationMap = {
'*': {
'integer': {
'integer': 'integer',
'real': 'real',
},
'real': {
'integer': 'real',
'real': 'real'
},
},
'+': {
'integer': {
'integer': 'integer',
'real': 'real',
},
'real': {
'integer': 'real',
'real': 'real'
},
},
'-': {
'integer': {
'integer': 'integer',
'real': 'real',
},
'real': {
'integer': 'real',
'real': 'real'
},
},
'/': {
'integer': {
'integer': 'integer',
'real': 'real',
},
'real': {
'integer': 'real',
'real': 'real'
},
},
'>': {
'integer': {
'integer': 'bool',
'real': 'bool',
},
'real': {
'integer': 'bool',
'real': 'bool'
},
},
'<': {
'integer': {
'integer': 'bool',
'real': 'bool',
},
'real': {
'integer': 'bool',
'real': 'bool'
},
},
'>=': {
'integer': {
'integer': 'bool',
'real': 'bool',
},
'real': {
'integer': 'bool',
'real': 'bool'
},
},
'<=': {
'integer': {
'integer': 'bool',
'real': 'bool',
},
'real': {
'integer': 'bool',
'real': 'bool'
},
},
'==': {
'integer': {
'integer': 'bool',
'real': 'bool',
},
'real': {
'integer': 'bool',
'real': 'bool'
},
},
'/=': {
'integer': {
'integer': 'bool',
'real': 'bool',
},
'real': {
'integer': 'bool',
'real': 'bool'
},
},
'=': {
'integer': {
'integer': 'integer',
},
'real': {
'integer': 'real',
'real': 'real',
},
'bool': {
'bool': 'bool',
}
},
}
def resultingType(operator, operand1, operand2):
operatorMap = typeValidationMap[operator]
if operand1 in operatorMap:
operand1Map = operatorMap[operand1]
if operand2 in operand1Map:
return operand1Map[operand2]
else:
raise Exception('Operation not suppoerte')
else:
raise Exception('Operation not supported')

125
final_lang/types.py Normal file
View file

@ -0,0 +1,125 @@
typeValidationMap = {
'*': {
'integer': {
'integer': 'integer',
'real': 'real',
},
'real': {
'integer': 'real',
'real': 'real'
},
},
'+': {
'integer': {
'integer': 'integer',
'real': 'real',
},
'real': {
'integer': 'real',
'real': 'real'
},
},
'-': {
'integer': {
'integer': 'integer',
'real': 'real',
},
'real': {
'integer': 'real',
'real': 'real'
},
},
'/': {
'integer': {
'integer': 'integer',
'real': 'real',
},
'real': {
'integer': 'real',
'real': 'real'
},
},
'>': {
'integer': {
'integer': 'bool',
'real': 'bool',
},
'real': {
'integer': 'bool',
'real': 'bool'
},
},
'<': {
'integer': {
'integer': 'bool',
'real': 'bool',
},
'real': {
'integer': 'bool',
'real': 'bool'
},
},
'>=': {
'integer': {
'integer': 'bool',
'real': 'bool',
},
'real': {
'integer': 'bool',
'real': 'bool'
},
},
'<=': {
'integer': {
'integer': 'bool',
'real': 'bool',
},
'real': {
'integer': 'bool',
'real': 'bool'
},
},
'==': {
'integer': {
'integer': 'bool',
'real': 'bool',
},
'real': {
'integer': 'bool',
'real': 'bool'
},
},
'/=': {
'integer': {
'integer': 'bool',
'real': 'bool',
},
'real': {
'integer': 'bool',
'real': 'bool'
},
},
'=': {
'integer': {
'integer': 'integer',
},
'real': {
'integer': 'real',
'real': 'real',
},
'bool': {
'bool': 'bool',
}
},
}
def resultingType(operator, type1, type2):
operatorMap = typeValidationMap[operator]
if type1 in operatorMap:
type1Map = operatorMap[type1]
if type2 in type1Map:
return type1Map[type2]
else:
raise Exception('Operation not suppoerted')
else:
raise Exception('Operation not supported')