Esempio n. 1
0
    def exponent(self):
        """
        Exponent        ⇒  {Factor **} Factor
        """
        # Check for a factor expression and capture it to create an exponent expression. There must be at least one factor.
        aFactor = self.factor()

        # Initialize a variable to be used while creating an exponent expression with more than one factor expression
        # and '**' operator.
        aBinaryExpr = ''

        # Check for '**' operators.
        if self.currToken[0] == lexer.Lexer.POWER:
            # Capture the operator to create a BinaryExpr object.
            operator = self.currToken[0]

            # Move to the next token.
            self.currToken = next(self.tokens)

            # Create a BinaryExpr object.
            aBinaryExpr = ast.BinaryExpr(operator, aFactor, self.exponent())

        # Check whether right was given an expression.
        if aBinaryExpr == '':
            # Return the expression.
            return aFactor

        else:
            # Return the BinaryExpr object.
            return aBinaryExpr
Esempio n. 2
0
    def expression(self):
        left_tree = self.conjunction()

        while self.currtok[0] == 'OR':
            self.currtok = next(self.tokens)
            right_tree = self.conjunction()
            left_tree = ast.BinaryExpr(left_tree, right_tree, '||')

        return left_tree
Esempio n. 3
0
    def equality(self):

        left_tree = self.relation()
        if self.currtok[2] == '==' or self.currtok[2] == '!=':
            tmpID = self.currtok[2]
            self.currtok = next(self.tokens)
            right_tree = self.relation()
            left_tree = ast.BinaryExpr(left_tree, right_tree, tmpID)
        return left_tree
Esempio n. 4
0
    def addition(self):
        left_tree = self.term()

        while self.currtok[0] == 'PLUS' or self.currtok[0] == "MINUS":
            tmpID = self.currtok[2]
            self.currtok = next(self.tokens)
            right_tree = self.term()
            left_tree = ast.BinaryExpr(left_tree, right_tree, tmpID)

        return left_tree
Esempio n. 5
0
    def relation(self):

        left_tree = self.addition()
        if self.currtok[2] == '<' or self.currtok[2] == '<=' or \
                self.currtok[2] == '>' or self.currtok[2] == '>=':
            tmpID = self.currtok[2]
            self.currtok = next(self.tokens)
            right_tree = self.addition()
            left_tree = ast.BinaryExpr(left_tree, right_tree, tmpID)
        return left_tree
Esempio n. 6
0
    def conjunction(self):

        left_tree = self.equality()

        while self.currtok[0] == 'AND':
            self.currtok = next(self.tokens)
            right_tree = self.equality()
            left_tree = ast.BinaryExpr(left_tree, right_tree, '&&')

        return left_tree
Esempio n. 7
0
    def expon(self):

        left_tree = self.factor()

        while self.currtok[0] == 'EXPONENTIATION':
            tmpID = self.currtok[2]
            self.currtok = next(self.tokens)
            right_tree = self.factor()
            left_tree = ast.BinaryExpr(left_tree, right_tree, tmpID)

        return left_tree
Esempio n. 8
0
    def term(self):

        left_tree = self.expon()

        while self.currtok[0] == 'MULTIPLY' or self.currtok[
                0] == "DIVIDE" or self.currtok[0] == "MOD":
            tmpID = self.currtok[2]
            self.currtok = next(self.tokens)
            right_tree = self.expon()
            left_tree = ast.BinaryExpr(left_tree, right_tree, tmpID)

        return left_tree
Esempio n. 9
0
def p_expr_binop(p):
    '''expr : expr PLUS expr
            | expr MINUS expr
            | expr MULTIPLY expr
            | expr DIVIDE expr
            | expr EQ expr
            | expr NEQ expr
            | expr LT expr
            | expr LEQ expr
            | expr GT expr
            | expr GEQ expr
            | expr AND expr
            | expr OR expr
    '''
    p[0] = ast.BinaryExpr(binops[p[2]], p[1], p[3], p.lineno(2))
Esempio n. 10
0
    def expression(self):
        """
        Expression      ⇒  Conjunction { || Conjunction }
        :return:
        """
        left_tree = self.conjuction()

        # Run this while the current token is
        # equal to "||" aka Or
        while self.currtok[0] == lexer.Lexer.OR:
            tempor = self.currtok[1]
            self.currtok = next(self.tokens)
            right_tree = self.conjuction()
            left_tree = ast.BinaryExpr(left_tree, right_tree, tempor)

        return left_tree
Esempio n. 11
0
    def conjuction(self):
        """
        Conjunction     ⇒  Equality { && Equality }
        :return:
        """
        left_tree = self.equality()

        # Run this while the current token is equal
        # To "&&" aka and
        while self.currtok[0] == lexer.Lexer.AND:
            tempand = self.currtok[1]
            self.currtok = next(self.tokens)
            right_tree = self.equality()
            left_tree = ast.BinaryExpr(left_tree, right_tree, tempand)

        return left_tree
Esempio n. 12
0
    def equality(self):
        """
        Equality  ⇒  Relation [ EquOp Relation ]
        :return:
        """
        left_tree = self.relation()

        # Check if the current token is either
        # "==" or "!="
        if self.currtok[0] in [lexer.Lexer.EQEQ, lexer.Lexer.NOTEQ]:
            tmpexpr = self.currtok[1]
            self.currtok = next(self.tokens)
            right_tree = self.relation()
            left_tree = ast.BinaryExpr(left_tree, right_tree, tmpexpr)

        return left_tree
Esempio n. 13
0
    def term(self):
        """
        Term → Factor {MulOp Factor}
        :return:
        """
        left_tree = self.factor()

        # Run this while the current token is a
        # times, division, mod, or exponential
        while self.currtok[0] in [lexer.Lexer.TIMES, lexer.Lexer.DIV, lexer.Lexer.MOD,
                                  lexer.Lexer.EXPONENTIAL]:
            tmp = self.currtok[1]
            self.currtok = next(self.tokens)
            right_tree = self.factor()
            left_tree = ast.BinaryExpr(left_tree, right_tree, tmp)

        return left_tree
Esempio n. 14
0
def get_random_binary_expr(expr_type, lhs, rhs):
    # FIXME lhs/rhs could be a constant, (expr) etc. Ideally we need an assert
    # here that makes sure that lhs and rhs type conform with the expr_type. We
    # might need a get_type() for all asts that can have type (including expr)
    if expr_type == ast.ExprType.Arith:
        op_type = get_random_arith_op()
        return ast.BinaryExpr(lhs, op_type, rhs)

    if expr_type == ast.ExprType.Logical:
        op_type = get_random_logical_op()
    return ast.LogicalExpr(lhs, op_type, rhs)

    if expr_type == ast.ExprType.Relational:
        op_type = get_random_relational_op()
    return ast.RelationalExpr(lhs, op_type, rhs)

    assert False, "unknown expr type"
Esempio n. 15
0
    def relation(self):
        """
        Relation  ⇒  Addition [ RelOp Addition ]
        :return:
        """
        left_tree = self.addition()

        # Check if the current token is either a
        # less than, less than or equal to, greater than, or
        # greater than or equal to
        if self.currtok[0] in [lexer.Lexer.LESS, lexer.Lexer.LESSEQ, lexer.Lexer.GREAT, lexer.Lexer.GREATEQ]:
            tmp = self.currtok[1]
            self.currtok = next(self.tokens)
            right_tree = self.addition()
            left_tree = ast.BinaryExpr(left_tree, right_tree, tmp)

        return left_tree
Esempio n. 16
0
    def addition(self):
        """
        Addition → Term  {AddOp Term}
        :return: BinaryPlusExpr
        """

        left_tree = self.term()

        # Run this while the current token is either
        # a plus or a minus
        while self.currtok[0] in [lexer.Lexer.PLUS, lexer.Lexer.MINUS]:
            tmp = self.currtok[1]
            self.currtok = next(self.tokens)
            right_tree = self.term()
            left_tree = ast.BinaryExpr(left_tree, right_tree, tmp)

        return left_tree
Esempio n. 17
0
    def expression(self):
        """
        Expression      ⇒  Conjunction { || Conjunction }
        """
        # Check for a conjunction expression and capture it to create a BinaryExpr object.
        left = self.conjunction()

        # Check for '||' until there are no more.
        while self.currToken[0] == lexer.Lexer.BOOLOR:
            # Capture the operator to create a BinaryExpr object.
            operator = self.currToken[0]

            # Move to the next token.
            self.currToken = next(self.tokens)

            # Check for another conjunction expression and capture it to create a BinaryExpr object.
            right = self.conjunction()

            # Create a BinaryExpr object.
            left = ast.BinaryExpr(operator, left, right)

        # Return the BinaryExpr object.
        return left
Esempio n. 18
0
    def conjunction(self):
        """
        Conjunction     ⇒  Equality { && Equality }
        """
        # Check for an equality expression and capture it to create a BinaryExpr object.
        left = self.equality()

        # Check for '&&' until there are no more.
        while self.currToken[0] == lexer.Lexer.BOOLAND:
            # Capture the operator to create a BinaryExpr object.
            operator = self.currToken[0]

            # Move to the next token.
            self.currToken = next(self.tokens)

            # Check for another equality expression and capture it to create a BinaryExpr object.
            right = self.equality()

            # Create a BinaryExpr object.
            left = ast.BinaryExpr(operator, left, right)

        # Return the BinaryExpr object.
        return left
Esempio n. 19
0
    def equality(self):
        """
        Equality        ⇒  Relation [ EquOp Relation ]
        """
        # Check for a relation expression and capture it to create a BinaryExpr object.
        left = self.relation()

        # Check for optional EqOp operators.
        if self.currToken[0] in (lexer.Lexer.EQ, lexer.Lexer.NOTEQ):
            # Capture the operator to create a BinaryExpr object.
            operator = self.currToken[0]

            # Move to the next token.
            self.currToken = next(self.tokens)

            # Check for another relation expression and capture it to create a BinaryExpr object.
            right = self.relation()

            # Create a BinaryExpr object.
            left = ast.BinaryExpr(operator, left, right)

        # Return the BinaryExpr object.
        return left
Esempio n. 20
0
    def addition(self):
        """
        Addition        ⇒  Term { AddOp Term }
        """
        # Check for a term expression and capture it to create a BinaryExpr object.
        left = self.term()

        # Check for addOps operators.
        while self.currToken[0] in (lexer.Lexer.ADD, lexer.Lexer.SUBTRACT):
            # Capture the operator to create a BinaryExpr object.
            operator = self.currToken[0]

            # Move to the next token.
            self.currToken = next(self.tokens)

            # Check for another term expression and capture it to create a BinaryExpr object.
            right = self.term()

            # Create a BinaryExpr object.
            left = ast.BinaryExpr(operator, left, right)

        # Return the BinaryExpr object.
        return left
Esempio n. 21
0
    def relation(self):
        """
        Relation        ⇒  Addition [ RelOp Addition ]
        """
        # Check for an addition expression and capture it to create a BinaryExpr object.
        left = self.addition()

        # Check for optional RelOp operators.
        if self.currToken[0] in (lexer.Lexer.LESS, lexer.Lexer.LESSEQ,
                                 lexer.Lexer.GREAT, lexer.Lexer.GREATEQ):
            # Capture the operator to create a BinaryExpr object.
            operator = self.currToken[0]

            # Move to the next token.
            self.currToken = next(self.tokens)

            # Check for another addition expression and capture it to create a BinaryExpr object.
            right = self.addition()

            # Create a BinaryExpr object.
            left = ast.BinaryExpr(operator, left, right)

        # Return the BinaryExpr object.
        return left
Esempio n. 22
0
    def term(self):
        """
        Term            ⇒  Exponent { MulOp Exponent }
        """
        # Check for an exponent expression and capture it to create a BinaryExpr object.
        left = self.exponent()

        # Check for mulOps operators.
        while self.currToken[0] in (lexer.Lexer.MULTIPLY, lexer.Lexer.DIVIDE,
                                    lexer.Lexer.REMAINDER):
            # Capture the operator to create a BinaryExpr object.
            operator = self.currToken[0]

            # Move to the next token.
            self.currToken = next(self.tokens)

            # Check for another exponent expression and capture it to create a BinaryExpr object.
            right = self.exponent()

            # Create a BinaryExpr object.
            left = ast.BinaryExpr(operator, left, right)

        # Return the BinaryExpr object.
        return left