-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.rb
executable file
·38 lines (34 loc) · 976 Bytes
/
lexer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require_relative 'token.rb'
class Lexer
def self.call(string)
new().send(:tokenise, string)
end
def self.to_proc
method(:call).to_proc
end
private
def tokenise(string)
tokens = []
string.each_char.with_index { |char, i|
case char
when /[λ\\]/
tokens << Token.new(:tklambda, char)
when /\./
tokens << Token.new(:tkdot, char)
when /\(/
tokens << Token.new(:tklparen, char)
when /\)/
tokens << Token.new(:tkrparen, char)
when /[a-z]/
tokens << Token.new(:tkid, char)
else
raise "Invalid character: \"#{char}\" at pos: #{i}"
end
}
return tokens
end
end
if $0 == __FILE__
input = ARGV[0]
output = Lexer.call(input)
end