-
Notifications
You must be signed in to change notification settings - Fork 105
/
interpreter.rb
73 lines (64 loc) · 1.64 KB
/
interpreter.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#This pattern provides an interpreter to deal with an abstract language. Using classes we can understand the inputs for parse them.
class Word
def initialize(value)
@value = value
end
def execute
@value
end
end
class Plus
def initialize(first, second)
@first = first
@second = second
end
def execute
@first.execute + @second.execute
end
end
class Minus
def initialize(first, second)
@first = first
@second = second
end
def execute
index = @first.execute =~ /#{@second.execute}/
second_index = index + @second.execute.length
@first.execute[0,index] + @first.execute[second_index..-1]
end
end
class Interpreter
def self.parse(input)
@waiting_second_word = false
words = []
operations = []
input.split.each_with_index do |value|
if value =~ /^[^+-].*/ && !@waiting_second_word
words << Word.new(value)
else
if symbol = operations.pop()
first = words.size > 1 ? Word.new(words.map(&:execute).join(" ")) :
words.pop
second = Word.new(value)
case symbol
when /\A\+/
words << Word.new(Plus.new(first, second).execute)
when /\A\-/
words << Word.new(Minus.new(first, second).execute)
end
@waiting_second_word = false
else
@waiting_second_word = true
operations << value
end
end
end
words.pop.execute
end
end
puts Interpreter.parse("NA + NA + NA + BATMAN")
#=> NANANABATMAN
puts Interpreter.parse("you know nothing Jon Snow - nothing")
#=> you know Jon Snow
puts Interpreter.parse("hello + world - llowo")
#=>herld