Skip to content

Commit

Permalink
Merge pull request #5 from luizmoitinho/If_statement
Browse files Browse the repository at this point in the history
If statement
  • Loading branch information
luizmoitinho authored Apr 27, 2020
2 parents ba690ab + 7f6f4a1 commit 90442bc
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 69 deletions.
42 changes: 24 additions & 18 deletions src/ExpressionLanguageParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def p_expr1(p):
p[0] = sa.Expr1_Scalar(p[1])
elif isinstance(p[1], sa.FunctionCall):
p[0] = sa.Expr1_FunctionCall(p[1])
elif isinstance(p[2], sa.ArrayDeclaration):
elif (p[1] == 'array' and isinstance(p[2], sa.ArrayDeclaration)):
p[0] = sa.Expr1_ArrayDeclaration(p[2])
elif isinstance(p[2], sa.Expr):
p[0] = sa.Expr1_ExprPar(p[2])
Expand Down Expand Up @@ -200,27 +200,36 @@ def p_statement(p):
p[0] = sa.Statement_Continue(p[1])
elif isinstance(p[1], sa.Return):
p[0] = sa.Statement_Return(p[1])
elif isinstance(p[1], sa.If):
p[0] = sa.Statement_If(p[1])
elif isinstance(p[1], sa.WhileStatement):
p[0] = sa.Statement_While(p[1])
elif isinstance(p[1], sa.DoWhileStatement):
p[0] = sa.Statement_Do_While(p[1])


def p_if_statement(p):
'''
if_statement : statement_if if_statement_complement
| statement_if
'''
if len(p)==3:
p[0] = sa.IfStatement_Complement(p[1],p[2])
else:
p[0] = sa.IfStatement_Single(p[1])


def p_if_statement_complement(p):
'''
if_statement_complement : statement_elseif
| statement_else
'''

def p_statement_if(p):
'''
statement_if : IF expr_parentheses statement_BLOCK_OPT
'''
if len(p) ==4:
p[0] = sa.StatementIf_ExprParen(p[2],p[3])

def p_statement_elseif(p):
'''
Expand All @@ -236,7 +245,8 @@ def p_while_statement(p):
'''
while_statement : WHILE expr_parentheses statement_BLOCK_OPT
'''
p[0] = sa.WhileStatementSingle(p[2], p[3])
if len(p)==4:
p[0] = sa.WhileStatementSingle(p[2], p[3])

def p_do_statement(p):
'''
Expand Down Expand Up @@ -314,7 +324,9 @@ def p_expr_parentheses(p):
'''
expr_parentheses : LPAREN expr RPAREN
'''
p[0] = sa.ExprParenthesesSingle(p[2])
if len(p)==4:
p[0] = sa.ExprParentheses_Expr(p[2])


def p_foreach_statement(p):
'''
Expand Down Expand Up @@ -620,7 +632,7 @@ def p_statement_MUL(p):
p[0] = sa.statementMulMul(p[1], p[2])
else:
p[0] = sa.statementMulSingle(p[1])

def p_for_expr_COLON_EXPR(p):
'''
for_expr_COLON_EXPR : COLON expr for_expr_COLON_EXPR
Expand All @@ -632,12 +644,12 @@ def p_statement_BLOCK_OPT(p):
statement_BLOCK_OPT : statement
| LKEY statement_MUL RKEY
| LKEY RKEY
'''
'''
if len(p) == 2:
p[0] = sa.StatementBlockOpt_Statement(p[1])
if len(p) == 4:
elif len(p) == 4:
p[0] = sa.StatementBlockOpt_StatementMul(p[2])
if len(p) == 3:
elif len(p) == 3:
p[0] = sa.StatementBlockOpt_Empty()

def p_parameter_list_COLON_PARAMETER(p):
Expand Down Expand Up @@ -690,16 +702,10 @@ def p_error(p):
lex.lex()
arquivo = '''
<?php
function add() {
while($i <= $j){
while($array[$i] < $x && $i < $fim){
$i = $i + 1;
}
while(($array[$j] > $x) && ($j > $inicio)){
$j = $j - 1;
}
}
}
if( $x == 10){
if( $valid)
$x++;
}
?>
'''

Expand Down
1 change: 0 additions & 1 deletion src/PrettyPrinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def incrementTab():
global space_buffer
space_buffer = space_buffer + spaces


def printTab():
global space_buffer
print(space_buffer, end='')
Expand Down
124 changes: 84 additions & 40 deletions src/SintaxeAbstrata.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ class InnerStatementMul(metaclass=ABCMeta):
@abstractmethod
def accept(self, Visitor):
pass

class InnerStatementMul_Mul(InnerStatementMul):
def __init__(self, innerStatement, innerStatementMul):
self.innerStatement = innerStatement
self.innerStatementMul = innerStatementMul
def accept(self, Visitor):
Visitor.visitInnerStatementMul_Mul(self)

class InnerStatementMul_Single(InnerStatementMul):
def __init__(self, innerStatement):
Expand All @@ -78,7 +71,40 @@ class Statement(metaclass = ABCMeta):
@abstractmethod
def accept(self, Visitor):
pass


class StatementBlockOpt(metaclass = ABCMeta):
@abstractmethod
def accept(self, Visitor):
pass

class StatementBlockOpt_Statement(StatementBlockOpt):
def __init__(self, statement):
self.statement = statement
def accept(self, Visitor):
Visitor.visitStatementBlockOpt_Statement(self)

class StatementBlockOpt_ParenEmpty(StatementBlockOpt):
def accept(self, Visitor):
Visitor.visitStatementBlockOpt_ParenEmpty(self)

class StatementBlockOpt_StatementMul(StatementBlockOpt):
def __init__(self, statementmul):
self.statementmul = statementmul
def accept(self, Visitor):
Visitor.visitStatementBlockOpt_StatementMul(self)

class StatementMul(metaclass = ABCMeta):
@abstractmethod
def accept(self, Visitor):
pass

class StatementMul_Mul(StatementMul):
def __init__(self, statement,statementMul):
self.statement = statement
self.statementMul = statementMul
def accept(self, Visitor):
Visitor.visitStatementMul_Mul(self)

class Statement_Expr(Statement):
def __init__(self, expr, semiColon):
self.expr = expr
Expand Down Expand Up @@ -110,6 +136,13 @@ def __init__(self, exit):
def accept(self, Visitor):
Visitor.visitStatement_Exit(self)

class Statement_If(Statement):
def __init__(self,_if):
self._if = _if
def accept(self, Visitor):
Visitor.visitStatement_If(self)


class Statement_While(Statement):
def __init__(self, whilee):
self.whilee = whilee
Expand All @@ -133,6 +166,49 @@ class FuncDecStatement(metaclass = ABCMeta):
def accept(self, Visitor):
pass

class ExprParentheses(metaclass = ABCMeta):
@abstractmethod
def accept(self,Visitor):
pass

class ExprParentheses_Expr(ExprParentheses):
def __init__(self, expr):
self.expr = expr
def accept(self, Visitor):
Visitor.visitExprParentheses_Expr(self)

class If(metaclass = ABCMeta):
@abstractmethod
def accept(self, Visitor):
pass

class IfStatement_Complement(If):
def __init__(self,statement_if,if_statement_complement):
self.statement_if = statement_if
self.if_statement_complement = if_statement_complement
def accept(self,Visitor):
Visitor.visitIfStatement_Complement(self)

class IfStatement_Single(If):
def __init__(self, statementIf):
self.statementIf = statementIf
def accept(self, Visitor):
Visitor.visitIfStatement_Single(self)


class StatementIf(metaclass = ABCMeta):
@abstractmethod
def accept(self, Visitor):
pass

class StatementIf_ExprParen(StatementIf):
def __init__(self, expr_parentheses,statement_BLOCK_OPT):
self.expr_parentheses = expr_parentheses
self.statement_BLOCK_OPT=statement_BLOCK_OPT
def accept(self,Visitor):
Visitor.visitStatementIf_ExprParen(self)


class funcDecStatement_Function(FuncDecStatement):
def __init__(self, fds_id, fds_parameter, fds_statements):
self.fds_id = fds_id
Expand Down Expand Up @@ -896,43 +972,11 @@ def __init__(self, exprparentheses, statement):
def accept(self, Visitor):
Visitor.visitWhileStatementSingle(self)

class ExprParentheses(metaclass = ABCMeta):
@abstractmethod
def accept(self, Visitor):
pass

class ExprParenthesesSingle(ExprParentheses):
def __init__(self, expr):
self.expr = expr
def accept(self, Visitor):
Visitor.visitExprParenthesesSingle(self)

class StatementBlockOpt(metaclass = ABCMeta):
@abstractmethod
def accept(self, Visitor):
pass

class StatementBlockOpt_Statement(StatementBlockOpt):
def __init__(self, statement):
self.statement = statement
def accept(self, Visitor):
Visitor.visitStatementBlockOpt_Statement(self)

class StatementBlockOpt_StatementMul(StatementBlockOpt):
def __init__(self, statementmul):
self.statementmul = statementmul
def accept(self, Visitor):
Visitor.visitStatementBlockOpt_StatementMul(self)

class StatementBlockOpt_Empty(StatementBlockOpt):
def accept(self, Visitor):
Visitor.visitStatementBlockOpt_Empty(self)

class StatementMul(metaclass = ABCMeta):
@abstractmethod
def accept(self, Visitor):
pass

class statementMulMul(StatementMul):
def __init__(self, statement, statementmul):
self.statement = statement
Expand Down
49 changes: 39 additions & 10 deletions src/Visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,21 @@ def visitFds_statements_noStatements(self, fds_statements):
print('{')
pp.printTab()
print('}')


def visitStatementBlockOpt_ParenEmpty(self, statementBlockOpt):
statementBlockOpt.statement.accept(self)

def StatementBlockOpt_ParenEmpty(self, StatementBlockOpt):
print('(',end='')
print(')',end='')

def visitStatementMul_Mul(self, StatementMul):
StatementMul.statement.accept(self)
StatementMul.statementMul.accept(self)

def visitStatementMul_Single(self, StatementMul):
StatementMul.statement.accept(self)

def visitParameterList_Parameter_Mul(self, parameterList):
parameterList.parameter.accept(self)
parameterList.parameter_list_colon_parameter.accept(self)
Expand Down Expand Up @@ -148,12 +162,16 @@ def visitStatement_Continue(self, statement):
def visitStatement_Return(self, statement):
pp.printTab()
statement._return.accept(self)


def visitStatement_If(self, statement):
statement._if.accept(self)

def visitStatement_Exit(self, statement):
pp.printTab()
statement.exit.accept(self)
print(';')


def visitStatement_While(self, statement):
pp.printTab()
statement.whilee.accept(self)
Expand All @@ -163,11 +181,27 @@ def visitStatement_Do_While(self, statement):
statement.dowhilee.accept(self)
print(';')


def visitStatement_Die(self, statement):
pp.printTab()
statement.die.accept(self)
print(';')

def visitIfStatement_Complement(self, ifStatement):
ifStatement.statement_if.accept(self)
ifStatement.if_statement_complement(self)

def visitIfStatement_Single(self, ifStatement):
ifStatement.statementIf.accept(self)

def visitStatementIf_ExprParen(self, statement_if):
print('if',end='')
statement_if.expr_parentheses.accept(self)
statement_if.statement_BLOCK_OPT.accept(self)

def visitExprParentheses_Expr(self, exprParentheses_Expr):
print('(',end='')
exprParentheses_Expr.expr.accept(self)
print(')',end='')

def visitExpr_Minus_Expr1(self, expr):
print('-', end='')
Expand Down Expand Up @@ -445,19 +479,14 @@ def visitWhileStatementSingle(self, whilestatement):
print('while', end='')
whilestatement.exprparentheses.accept(self)
whilestatement.statement.accept(self)

def visitExprParenthesesSingle(self, exprparentheses):
print('(', end='')
exprparentheses.expr.accept(self)
print(')', end='')

def visitStatementBlockOpt_Statement(self, statementblockopt):
statementblockopt.statement.accept(self)

def visitStatementBlockOpt_StatementMul(self, statementblockopt):
def visitStatementBlockOpt_StatementMul(self, statementBlockOpt):
print('{')
pp.incrementTab()
statementblockopt.statementmul.accept(self)
statementBlockOpt.statementmul.accept(self)
pp.decrementTab()
pp.printTab()
print('}')
Expand Down
Binary file modified src/__pycache__/ExpressionLanguageLex.cpython-38.pyc
Binary file not shown.
Binary file modified src/__pycache__/SintaxeAbstrata.cpython-38.pyc
Binary file not shown.
Binary file modified src/__pycache__/Visitor.cpython-38.pyc
Binary file not shown.
Binary file modified src/__pycache__/parsetab.cpython-38.pyc
Binary file not shown.

0 comments on commit 90442bc

Please sign in to comment.