Exemplo n.º 1
0
    def __parse_tokens__(self, s):
        tokens = []
        token = ""
        single_tokens = (
            '(',
            ')',
            '[',
            ']',
            '+',
            '-',
            '*',
            '/',
            '^',
        )
        for ch in s:
            if ch in single_tokens or ch == ' ':
                if token:
                    tokens.append(Token(token))
                if ch != ' ':
                    token = Token(ch)
                    if token.val in ('+', '-'):
                        if not tokens or tokens[-1].typ not in (
                                TokenType.NUMBER, TokenType.X):
                            token.typ = TokenType.FUNCTION
                    tokens.append(token)
                token = ''
            else:
                if ch:
                    token += ch
        if token:
            token = Token(token)
            if token.val in ('+', '-'):
                if not tokens or tokens[-1].typ not in (TokenType.NUMBER,
                                                        TokenType.X):
                    token.typ = TokenType.FUNCTION
            tokens.append(token)

        return tokens