Class: PyruLexer

Inherits:
Object
  • Object
show all
Defined in:
lib/pyru_lexer.rb

Overview

pyru_lexer.rb

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ PyruLexer

Returns a new instance of PyruLexer.



4
5
6
7
8
# File 'lib/pyru_lexer.rb', line 4

def initialize(code)
  @code = code
  @position = 0
  @tokens = []
end

Instance Method Details

#tokenizeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pyru_lexer.rb', line 10

def tokenize
  while @position < @code.length
    case @code[@position]
    when /\s/
      # Skip whitespace
      @position += 1
    when /[a-zA-Z]/
      # Identify identifiers
      identifier = ''
      while @code[@position] =~ /[a-zA-Z0-9_]/
        identifier += @code[@position]
        @position += 1
      end
      @tokens << { type: :IDENTIFIER, value: identifier }
    # Add other token types and their respective cases as needed
    else
      # Skip unknown characters
      @position += 1
    end
  end
  @tokens
end