-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.rb
57 lines (47 loc) · 1.24 KB
/
reader.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require './symbol.rb'
require './parser.rb'
class Calc_Reader
def initialize(input)
@input = input
@get_last = false
end
def get_charsymbol
if @get_last then
@get_last = false
return @last_charsymbol
end
charsymbol = Charsymbol.new
@input.lstrip!
case @input
when /\A\(/ then
charsymbol.type = Charsymbol::LBracket
when /\A\)/ then
charsymbol.type = Charsymbol::RBracket
when /\A\^/ then
charsymbol.type = Charsymbol::Index
when /\A\*/ then
charsymbol.type = Charsymbol::Multiply
when /\A\// then
charsymbol.type = Charsymbol::Divide
when /\A\+/ then
charsymbol.type = Charsymbol::Plus
when /\A-/ then
charsymbol.type = Charsymbol::Minus
#when /d+?/ #Integers only
when /\A\d+(\.\d+)?/ #Floats
charsymbol.type = Charsymbol::Number
charsymbol.value = $&.to_f # Thanks google
when ''
charsymbol.type = Charsymbol::End
end
if charsymbol.unknown? then
raise ('Unknown Operand')
end
@input = $' # <== google - I was having a hard time with this
@last_charsymbol = charsymbol
charsymbol
end
def go_back
@get_last = true
end
end