Exemplo n.º 1
0
 def next_token(self):
     token = Token()
     self.skip_whitespaces()
     skip_read_char = False
     if self.ch == '+':
         if self.peek_char() == '+':
             self.read_char()
             token.type = TokenType.PLUSPLUS
             token.literal = '++'
         else:
             token.type = TokenType.PLUS
             token.literal = self.ch
     elif self.ch == '-':
         token.type = TokenType.MINUS
         token.literal = self.ch
     elif self.ch == '/':
         if self.peek_char() == '*':
             self.read_char()
             token.type = TokenType.START_COMMENT
             token.literal = '/*'
         else:
             token.type = TokenType.SLASH
             token.literal = self.ch
     elif self.ch == '*':
         if self.peek_char() == '/':
             self.read_char()
             token.type = TokenType.END_COMMENT
             token.literal = '*/'
         else:
             token.type = TokenType.ASTERISK
             token.literal = self.ch
     elif self.ch == '>':
         if self.peek_char() == '=':
             self.read_char()
             token.type = TokenType.GTE
             token.literal = ">="
         else:
             token.type = TokenType.GT
             token.literal = self.ch
     elif self.ch == '<':
         if self.peek_char() == '=':
             self.read_char()
             token.type = TokenType.LTE
             token.literal = "<="
         else:
             token.type = TokenType.LT
             token.literal = self.ch
     elif self.ch == ';':
         token.type = TokenType.SEMICOLON
         token.literal = self.ch
     elif self.ch == '(':
         token.type = TokenType.LPAREN
         token.literal = self.ch
     elif self.ch == ')':
         token.type = TokenType.RPAREN
         token.literal = self.ch
     elif self.ch == '{':
         token.type = TokenType.LBRACE
         token.literal = self.ch
     elif self.ch == '}':
         token.type = TokenType.RBRACE
         token.literal = self.ch
     elif self.ch == ',':
         token.type = TokenType.COMMA
         token.literal = self.ch
     elif self.ch == 0:
         token.type = TokenType.EOF
         token.literal = self.ch
     elif self.ch == '"':
         token.type = TokenType.STRING
         token.literal = self.read_string()
     elif self.ch == '[':
         token.type = TokenType.LBRACKET
         token.literal = self.ch
     elif self.ch == ']':
         token.type = TokenType.RBRACKET
         token.literal = self.ch
     elif self.ch == ':':
         token.type = TokenType.COLON
         token.literal = self.ch
     elif self.ch == '.':
         token.type = TokenType.DOT
         token.literal = self.ch
     elif self.ch == '=':
         if self.peek_char() == '=':
             self.read_char()
             token.type = TokenType.EQ
             token.literal = '=='
         else:
             token.type = TokenType.ASSIGN
             token.literal = self.ch
     elif self.ch == '!':
         if self.peek_char() == '=':
             self.read_char()
             token.type = TokenType.NOT_EQ
             token.literal = '!='
         else:
             token.type = TokenType.BANG
             token.literal = '!'
     else:
         if self.is_letter():
             token.literal = self.read_ident()
             token.type = self.lookup_ident(token.literal)
             skip_read_char = True
         elif self.is_digit():
             token.literal = self.read_number()
             if '.' in token.literal:
                 token.type = TokenType.FLOAT
             else:
                 token.type = TokenType.INT
             skip_read_char = True
         else:
             token.type = TokenType.ILLEGAL
             token.literal = ''
     if not skip_read_char:
         self.read_char()
     return token