Selaa lähdekoodia

Lex numbers

master
Dylan Baker 6 vuotta sitten
vanhempi
commit
84f9e00090
2 muutettua tiedostoa jossa 19 lisäystä ja 4 poistoa
  1. 11
    4
      lib/chervil/lexer.rb
  2. 8
    0
      spec/lexer_spec.rb

+ 11
- 4
lib/chervil/lexer.rb Näytä tiedosto

9
       @source[@position]
9
       @source[@position]
10
     end
10
     end
11
 
11
 
12
-    def advance
13
-      @position += 1
12
+    def advance(step = 1)
13
+      @position += step
14
     end
14
     end
15
 
15
 
16
     def get_next_token
16
     def get_next_token
17
       case current_char
17
       case current_char
18
+      when nil
19
+        Token.new(:eof, "eof")
18
       when '('
20
       when '('
19
         advance
21
         advance
20
         Token.new(:lparen, "(")
22
         Token.new(:lparen, "(")
21
       when ')'
23
       when ')'
22
         advance
24
         advance
23
         Token.new(:rparen, ")")
25
         Token.new(:rparen, ")")
24
-      when nil
25
-        Token.new(:eof, "eof")
26
+      else
27
+        source = @source.slice(@position..-1)
28
+        if match = source.match(/^[0-9]+(\.[0-9]+)?/)
29
+          advance(match[0].size)
30
+          Token.new(:number, match[0])
31
+        end
26
       end
32
       end
27
     end
33
     end
28
 
34
 
30
       tokens = Array.new
36
       tokens = Array.new
31
       loop do
37
       loop do
32
         token = get_next_token
38
         token = get_next_token
39
+        next if token.nil?
33
         tokens << token
40
         tokens << token
34
         break if token.type == :eof
41
         break if token.type == :eof
35
       end
42
       end

+ 8
- 0
spec/lexer_spec.rb Näytä tiedosto

9
         ]
9
         ]
10
       )
10
       )
11
     end
11
     end
12
+
13
+    it 'lexes integers' do
14
+      expect(Lexer.new('1').get_next_token).to eq(Token.new(:number, '1'))
15
+    end
16
+
17
+    it 'lexes floats' do
18
+      expect(Lexer.new('2.3').get_next_token).to eq(Token.new(:number, '2.3'))
19
+    end
12
   end
20
   end
13
 end
21
 end

Loading…
Peruuta
Tallenna