Skip to content

Commit

Permalink
Adicionar array como tipo booleano, adicionar suporte a expressão ari…
Browse files Browse the repository at this point in the history
…tmética com variáveis
  • Loading branch information
erickolisveira committed Jun 9, 2020
1 parent 7ff85e1 commit 93f6753
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
8 changes: 3 additions & 5 deletions src/Parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,11 +687,9 @@ def p_error(p):
lex.lex()
arquivo = '''
<?php
while(10 == $x){
$valor = 1;
$valor2 = $valor;
}
$valor = 1.5;
$v = $valor + $valor - $valor / $valor * $valor;
?>
'''
Expand Down
34 changes: 22 additions & 12 deletions src/SemanticVisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def isValidNumber(number):
return False

def isTypePrimitive(type):
if(type in st.Number or type == st.BOOL):
if(type in st.Number or type == st.BOOL or type == st.ARRAY):
return True
return False

Expand Down Expand Up @@ -90,7 +90,6 @@ def visitWhileStatementSingle(self, whileStatement):
st.beginScope('while')
exprBool = whileStatement.exprparentheses.accept(self)
whileStatement.statement.accept(self)
print(exprBool)
st.endScope()

def visitFuncDecStatement_Function(self, funcDecStatement):
Expand Down Expand Up @@ -153,6 +152,10 @@ def visitStatement_Expr(self, statement):
def visitExpr_Plus(self, exprPlus):
type1 = exprPlus.expr1.accept(self)
type2 = exprPlus.expr2.accept(self)

type1 = isVariable(self, type1)
type2 = isVariable(self, type2)

c = coercion(type1, type2)
if (c == None):
el.ExpressionTypeError(self, exprPlus, type1, type2)
Expand All @@ -161,6 +164,10 @@ def visitExpr_Plus(self, exprPlus):
def visitExpr_Minus(self, exprMinus):
type1 = exprMinus.expr1.accept(self)
type2 = exprMinus.expr2.accept(self)

type1 = isVariable(self, type1)
type2 = isVariable(self, type2)

c = coercion(type1, type2)
if (c == None):
el.ExpressionTypeError(self, exprMinus, type1, type2)
Expand All @@ -169,6 +176,10 @@ def visitExpr_Minus(self, exprMinus):
def visitExpr_Times(self, exprTimes):
type1 = exprTimes.expr1.accept(self)
type2 = exprTimes.expr2.accept(self)

type1 = isVariable(self, type1)
type2 = isVariable(self, type2)

c = coercion(type1, type2)
if (c == None):
el.ExpressionTypeError(self, exprTimes, type1, type2)
Expand All @@ -177,6 +188,10 @@ def visitExpr_Times(self, exprTimes):
def visitExpr_Divide(self, exprDivide):
type1 = exprDivide.expr1.accept(self)
type2 = exprDivide.expr2.accept(self)

type1 = isVariable(self, type1)
type2 = isVariable(self, type2)

c = coercion(type1, type2)
if (c == None):
el.ExpressionTypeError(self, exprDivide, type1, type2)
Expand All @@ -197,17 +212,17 @@ def visitExpr_Equals(self, exprEqual):
type1 = exprEqual.expr1.accept(self)
type2 = exprEqual.expr2.accept(self)

type1 = isVariable(self,type1)
type2 = isVariable(self,type2)
type1 = isVariable(self, type1)
type2 = isVariable(self, type2)

if isTypePrimitive(type1) and isTypePrimitive(type2):
return st.BOOL
else:
print('ERROR: Invalid boolean expression:', end='')
print('ERROR: Invalid boolean expression: ', end='')
exprEqual.expr1.accept(self.printer)
print(' == ',end='')
exprEqual.expr2.accept(self.printer)

print('')

def visitExpr_AssignExpr(self, assignExpr):
bindable = assignExpr.variable.accept(self)
Expand All @@ -230,10 +245,7 @@ def visitExpr_AddAssignExpr(self, assignExpr):
el.AttributionTypeError(self, assignExpr, exprType)

if bindable[st.TYPE] not in st.Number:
print('ERROR: Invalid atribution of type ', exprType, end='')
print('on variable ', end='')
assignExpr.variable.accept(self.printer)
print(' that has type', bindable[st.TYPE])
el.AttributionInvalidTypeError(self, exprType, assignExpr, bindable)

st.updateBindableType(bindable[st.NAME], exprType)

Expand All @@ -245,7 +257,6 @@ def visitExpr_NumberFloat(self, exprNumber):

def visitExpr_EncapsedString(self, exprEncapsed):
return st.STRING


def visitExpr_Boolean(self, exprBoolean):
return st.BOOL
Expand Down Expand Up @@ -387,7 +398,6 @@ def visitArrayPairList_Mul(self, arrayPairList):
arrayPairList.arrayPairListArr.accept(self)

def visitArrayPairListArr_Single(self, arrayPairList):
print('single')
arrayPairList.arrayPair.accept(self)

def visitArrayPair_Expr(self, arrayPair):
Expand Down

0 comments on commit 93f6753

Please sign in to comment.