Class: Brainfuck::Lexer
- Inherits:
-
Object
- Object
- Brainfuck::Lexer
- Defined in:
- lib/lang/lexer.rb
Constant Summary collapse
- OPERATORS =
['>', '<', '+', '-', '[', ']', ',', '.']
Class Method Summary collapse
Class Method Details
.tokenize(code) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/lang/lexer.rb', line 5 def tokenize(code) tokens = [] unless code && code.length > 0 tokens end i = 0 while i < code.length case code[i..-1] when /\A(\s+)/ i += $1.length # ignore whitespace when /\A(.)/ if OPERATORS.include? $1 tokens << [$1, $1] end # Comment is anything other than operators, so ignore i += 1 end end tokens end |