def parseFactor():
        current = Parser.tokens.actual
        # print(current.value)

        if current.type == "PLUS":
            Parser.tokens.selectNext()
            factor = Parser.parseFactor()
            node = n.UnOp("PLUS", [factor])

        elif current.type == "MINUS":
            Parser.tokens.selectNext()
            factor = Parser.parseFactor()
            node = n.UnOp("MINUS", [factor])

        elif current.type == "INT":
            node = n.IntVal(current.value)
            Parser.tokens.selectNext()

        elif current.type == "BOOL":
            if current.value == "true":
                node = n.BoolVal(True)
            else:
                node = n.BoolVal(False)
            Parser.tokens.selectNext()

        elif current.type == "STRING":
            node = n.StringVal(current.value)
            Parser.tokens.selectNext()

        elif current.type == "OPEN":
            Parser.tokens.selectNext()
            node = Parser.parseRelExpression()
            current = Parser.tokens.actual
            if current.type != "CLOSE":
                raise Exception("Nao fechou parenteses")
            Parser.tokens.selectNext()

        elif current.type == "VARIABLE":
            Parser.tokens.selectNext()
            node = n.Identifier(current.value)

        elif current.type == "NOT":
            Parser.tokens.selectNext()
            factor = Parser.parseFactor()
            node = n.UnOp("NOT", [factor])

        else:
            raise Exception("Queria FACTOR, recebeu {}".format(current.type))

        return node
Exemple #2
0
    def fator():

        new_token = Parser.tokens.actual

        # check if token is unary operator
        if (new_token.type == 'PLUS' or new_token.type == 'MINUS'
                or new_token.type == 'NOT'):
            if (new_token.type == 'PLUS'):

                Parser.tokens.selectNext()
                return nd.UnOp('+', [Parser.fator()])

            elif (new_token.type == 'MINUS'):
                Parser.tokens.selectNext()
                return nd.UnOp('-', [Parser.fator()])

            elif (new_token.type == 'NOT'):
                Parser.tokens.selectNext()
                return nd.UnOp('~', [Parser.fator()])

            else:
                raise TypeError("Invalid Token Error: ", new_token.type)

        # check if token is a number
        elif new_token.type == 'INT':
            left = nd.IntVal(new_token.value, [])
            Parser.tokens.selectNext()
            return left

        # check if token is a boolean
        elif new_token.type in ['TRUE', 'FALSE']:
            left = nd.BoolVal(new_token.type, [])
            Parser.tokens.selectNext()
            return left

        # check if token is a variable
        elif new_token.type == 'identifier':
            left = new_token.value
            Parser.tokens.selectNext()
            if (Parser.tokens.actual.type == '('):
                Parser.tokens.selectNext()
                right = []
                while (Parser.tokens.actual.type != ')'):
                    right.append(Parser.relExpression())
                    if (Parser.tokens.actual.type == ','):
                        Parser.tokens.selectNext()
                Parser.tokens.selectNext()
                return nd.FuncCall(left, right)
            else:
                return nd.Identifier(left, [])

        elif new_token.type == 'INPUT':
            left = nd.Input('input', [])
            Parser.tokens.selectNext()
            return left

        # check if token is parentesis
        elif new_token.type == '(':
            Parser.tokens.selectNext()
            left = Parser.relExpression()
            new_token = Parser.tokens.actual
            if new_token.type == ')':
                Parser.tokens.selectNext()
                return left
            else:
                raise TypeError(
                    "Invalid Token Error - Expecting Parentesis ')' ")
        else:
            raise SyntaxError(
                "Invalid Token - Sentence should start with number, parentesis or operator (+,-): ",
                new_token.type)