Exemplo n.º 1
0
    def factor(self):
        unary_exp = self.unary()
        while self.cur_token.type in (TokenType.MUL, TokenType.DIV):
            tok = self.cur_token
            self.eat(tok.type)  # '*' ó '/'
            unary_exp = ast.BinaryOp(
                left=unary_exp,
                operator=tok.value,
                right=self.unary(),
            )

        return unary_exp
Exemplo n.º 2
0
    def term(self):
        factor_exp = self.factor()
        while self.cur_token.type in (TokenType.PLUS, TokenType.MINUS):
            tok = self.cur_token
            self.eat(tok.type)  # '+' ó '-'
            factor_exp = ast.BinaryOp(
                left=factor_exp,
                operator=tok.value,
                right=self.factor(),
            )

        return factor_exp
Exemplo n.º 3
0
    def equality(self):
        comp_exp = self.comparison()
        while self.cur_token.type in (TokenType.EQUAL, TokenType.NOT_EQUAL):
            tok = self.cur_token
            self.eat(tok.type)  # '==' ó '!='
            comp_exp = ast.BinaryOp(
                left=comp_exp,
                operator=tok.value,
                right=self.comparison(),
            )

        return comp_exp
Exemplo n.º 4
0
    def logic_and(self):
        and_exp = self.equality()
        while self.cur_token.type == TokenType.AND:
            tok = self.cur_token
            self.eat(TokenType.AND)
            and_exp = ast.BinaryOp(
                left=and_exp,
                operator=tok.value,
                right=self.equality(),
            )

        return and_exp
Exemplo n.º 5
0
    def logic_or(self):
        or_exp = self.logic_and()
        while self.cur_token.type == TokenType.OR:
            tok = self.cur_token
            self.eat(TokenType.OR)
            or_exp = ast.BinaryOp(
                left=or_exp,
                operator=tok.value,
                right=self.logic_and(),
            )

        return or_exp
Exemplo n.º 6
0
    def comparison(self):
        term_exp = self.term()
        while self.cur_token.type in (TokenType.LESS, TokenType.LESS_EQ,
                                      TokenType.GREATER, TokenType.GREATER_EQ):
            tok = self.cur_token
            self.eat(tok.type)  # '<' ó '>' ó '<=' ó '>='
            term_exp = ast.BinaryOp(
                left=term_exp,
                operator=tok.value,
                right=self.term(),
            )

        return term_exp