-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheso.rb
255 lines (232 loc) · 4.89 KB
/
eso.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# coding: utf-8
class Tree
def initialize(value)
@value = value
@left
@right
end
attr_accessor :value, :right, :left
def set_children(left, right)
@left = left
@right = right
end
def show
@left.show unless @left.nil?
print @value
@right.show unless @right.nil?
end
def text
text = ""
text += @left.text unless @left.nil?
text += @value
text += @right.text unless @right.nil?
text
end
def calc
if @value == "+"
@left.calc + @right.calc
elsif @value == "-"
@left.calc - @right.calc
elsif @value == "*"
@left.calc * @right.calc
elsif @value == "/"
@left.calc / @right.calc
else
@value.to_i
end
end
def check?
if value == "="
l = @left.calc
r = @right.calc
l == r
else
@value == "/" and @right.value == "0"
end
end
end
class Memory
def initialize(trees, input)
@trees = trees
@input = input
@memory = []
256.times {@memory << 0}
@counter = 0
end
attr_accessor :memory, :counter
def both_operator(left, right)
ll = left.left.calc
lr = left.right.calc
rl = right.left.calc
rr = right.right.calc
if @memory[ll] == @memory[lr]
@counter += @memory[rl]
else
@counter += @memory[rr]
end
end
def left_operator(left, right)
l = left.left.calc
r = left.right.calc
target = right.calc
@memory[target] = eval("#{@memory[l]} #{left.value} #{@memory[r]}")
end
def right_operator(left, right)
l = right.left.calc
r = right.right.calc
target = left.calc
case right.value
when "+"
@memory[l] = @memory[target]
@memory[r] = @memory[target]
when "-"
if @memory[target] < @memory[l]
@counter -= @memory[r]
end
when "*"
if @memory[l] < @memory[r]
print @memory[target].chr
else
print @memory[target]
end
when "/"
i = @input.shift
if i.nil?
@memory[target] = 0
elsif @memory[l] < @memory[r]
@memory[target] = i.ord
else
@memory[target] = i.to_i
end
end
end
def operator?(char)
char == "+" || char == "-" || char == "*" || char == "/"
end
def exec_one_step
tree = @trees[@counter]
if tree.value == "/"
value = tree.left.calc
@counter += @memory[value]
elsif tree.value == "="
if operator? tree.left.value and operator? tree.right.value # a + b = c + d
both_operator tree.left, tree.right
elsif operator? tree.left.value # a + b = c
left_operator tree.left, tree.right
elsif operator? tree.right.value # a = b + c
right_operator tree.left, tree.right
else # a = a
l = tree.left.calc
r = tree.right.calc
@memory[l] = r
end
end
@counter += 1
#p @memory #メモリの中身を出力
end
def finish?
@counter >= @trees.length
end
end
def to_rpn(chars)
output = []
stack = []
a = nil
chars.each do |token|
token = token.first
case token
when "(" then stack << token
when ")"
output << a until (a = stack.pop) == "("
when "*", "/"
loop do
a = stack.last
break unless %w(* /).include? a
output << stack.pop
end
stack << token
when "+", "-"
loop do
a = stack.last
break unless %w(+ - * /).include? a
output << stack.pop
end
stack << token
else
output << token
end
end
output << a while a = stack.pop
output
end
def calculate(parsed)
stack = []
while x = parsed.shift
if parsed.length == 0
stack << x
break
elsif %w(+ - * /).include? x
a, b = stack.pop, stack.pop
stack << eval("#{b} #{x} #{a}")
else
stack << x
end
end
stack
end
def make_tree(chars)
rpn = to_rpn chars
calc = calculate rpn
root = Tree.new calc.last
if calc.length == 3
left = Tree.new calc[0].to_s
right = Tree.new calc[1].to_s
root.set_children left, right
end
root
end
def parse(chars)
if chars.include? ["="]
eq = chars.index ["="]
left = make_tree chars[0..eq - 1]
right = make_tree chars[eq + 1..-1]
ret = Tree.new("=")
ret.set_children left, right
ret
else
make_tree(chars)
end
end
def execute(trees, input)
counter = 0
memory = Memory.new trees, input
until memory.finish?
memory.exec_one_step
end
end
def main
trees = []
File.open(ARGV[0], mode="rt"){|f|
f.each_line {|l|
ch = l.scan(/([0-9\.]+|\+|\-|\*|\/|\(|\)|=)/)
unless ch.empty?
pa = parse ch
trees << pa
end
}
}
if trees.all? {|tree| tree.check?}
input = STDIN.read
input = input.chars unless input.nil?
execute trees, input
else
trees.each_with_index do |tree, i|
unless tree.check?
puts "line#{i + 1}:#{tree.text} is wrong!"
end
end
end
end
if __FILE__ == $0
main
end