lib/monkey/lexer.rb
# Obtain the next token from the original input.
#
# This should be called repeatedly until it returns
# a token with type equal to {Monkey::Token::EOF}, as ignoring
# that special token will result in an infinite loop.
Obtain the next token from the original input.
This should be called repeatedly until it returns
a token with type equal to {Monkey::Token::EOF}, as ignoring
that special token will result in an infinite loop.
Obtain the next token from the original input . This should be called repeatedly until it returns a token with type equal to { Monkey :: Token :: EOF } as ignoring that special token will result in an infinite loop .
def next_token
      skip_whitespace
      tok = case @ch
            when "="
              if peek_char == "="
                last_ch = @ch
                read_char
                Token.new(Token::EQ, last_ch + @ch)
              else
                Token.new(Token::ASSIGN, @ch)
              end
            when "!"
              if peek_char == "="
                last_ch = @ch
                read_char
                Token.new(Token::NOT_EQ, last_ch + @ch)
              else
                Token.new(Token::BANG, @ch)
              end
            when ";"
              Token.new(Token::SEMICOLON, @ch)
            when "("
              Token.new(Token::LPAREN, @ch)
            when ")"
              Token.new(Token::RPAREN, @ch)
            when "{"
              Token.new(Token::LBRACE, @ch)
            when "}"
              Token.new(Token::RBRACE, @ch)
            when ","
              Token.new(Token::COMMA, @ch)
            when "+"
              Token.new(Token::PLUS, @ch)
            when "-"
              Token.new(Token::MINUS, @ch)
            when "*"
              Token.new(Token::ASTERISK, @ch)
            when "/"
              Token.new(Token::SLASH, @ch)
            when "<"
              Token.new(Token::LT, @ch)
            when ">"
              Token.new(Token::GT, @ch)
            when "\x0"
              Token.new(Token::EOF, "")
            when proc(&->(ch) { ch =~ /[a-zA-Z_]/ })
              literal = read_identifier
              type = Token.lookup_ident(literal)
              return Token.new(type, literal)
            when proc(&->(ch) { ch =~ /[0-9]/ })
              literal = read_number
              return Token.new(Token::INT, literal)
            else
              Token.new(Token::ILLEGAL, @ch)
            end
      read_char
      tok
    end
def next_token skip_whitespace tok = case @ch when "=" if peek_char == "=" last_ch = @ch read_char Token . new ( Token :: EQ , last_ch + @ch ) else Token . new ( Token :: ASSIGN , @ch ) end when "!" if peek_char == "=" last_ch = @ch read_char Token . new ( Token :: NOT_EQ , last_ch + @ch ) else Token . new ( Token :: BANG , @ch ) end when ";" Token . new ( Token :: SEMICOLON , @ch ) when "(" Token . new ( Token :: LPAREN , @ch ) when ")" Token . new ( Token :: RPAREN , @ch ) when "{" Token . new ( Token :: LBRACE , @ch ) when "}" Token . new ( Token :: RBRACE , @ch ) when "," Token . new ( Token :: COMMA , @ch ) when "+" Token . new ( Token :: PLUS , @ch ) when "-" Token . new ( Token :: MINUS , @ch ) when "*" Token . new ( Token :: ASTERISK , @ch ) when "/" Token . new ( Token :: SLASH , @ch ) when "<" Token . new ( Token :: LT , @ch ) when ">" Token . new ( Token :: GT , @ch ) when "\x0" Token . new ( Token :: EOF , "" ) when proc ( & -> ( ch ) { ch =~ / [a-zA-Z_] / } ) literal = read_identifier type = Token . lookup_ident ( literal ) return Token . new ( type , literal ) when proc ( & -> ( ch ) { ch =~ / [0-9] / } ) literal = read_number return Token . new ( Token :: INT , literal ) else Token . new ( Token :: ILLEGAL , @ch ) end read_char tok end
Obtain the next token from the original input.
Obtain the next token from the original input .