-
Notifications
You must be signed in to change notification settings - Fork 0
/
flat3.rb
331 lines (290 loc) · 6.91 KB
/
flat3.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# 文列 = 文
# 文 = 代入文 | if文 | print文 | while文 | '{' 文列 '}'
# 代入文 = 変数 '=' 式
# if文 = 'if' 式 'then' 文 'else' 文 'end'
# input文 = 'in' 変数
# output文 = 'out' 式
# while文 = 'while'式 '{' 文 '}'
# 式 = 項 (('+'|'-')項)*
# 項 = 因子(('*'|'/')因子)*
# 因子 = '-'?(リテラル|'('式')')
# input文とif文は実装できませんでした。
class Flat
@@keywords = {
'+' => :add,
'-' => :sub,
'*' => :mul,
'/' => :div,
'%' => :mod,
'(' => :lpar,
')' => :rpar,
'if' => :if,
'then' => :then,
'else' => :else,
'end' => :end,
'in' => :input,
'out' => :output,
'while' => :while,
'"' => :doublequot,
}
attr_accessor :code
#初期化
def initialize()
@code = ''
@space = {}
end
#構文解析
def get_token()
if @code =~ /\A\s*(#{@@keywords.keys.map { |t| Regexp.escape(t) } .join('|')})/
@code = $'
return @@keywords[$1]
elsif @code =~ /\A\s*([0-9.]+)/
@code = $'
return $1.to_f
elsif @code =~ /\A\s*([a-zA-z]\w*)/
@code = $'
return $1.to_s
elsif @code =~ /\A\s*(\w+)/
@code = $'
return $1.to_s
elsif @code =~ /\A\s*\z/
return nil
end
return :bad_token
end
def unget_token(token)
return if token == :bad_token || token == nil
if token.is_a? Numeric
@code = token.to_s + @code
else
@code = @@keywords.key(token) ? @@keywords.key(token) + @code : token + @code
end
end
def expression()
result = term()
while true
token = get_token()
unless token == :add || token == :sub
unget_token(token)
break
end
result = [token, result, term()]
end
return result
end
def term()
result = factor()
while true
token = get_token()
unless token == :mul || token == :sub
unget_token(token)
break
end
result = [token, result, factor()]
end
return result
end
def check()
fail Exception, "'('が見つからないよ" unless get_token() == :lpar
token1 = get_token()
token2 = get_token
unget_token(token2)
if token1 == :doublequot
if @code =~ /\A\s*(.+)(")/
@code = $2 + $'
result = $1.to_s
end
fail Exception, '`"`が見つからないよ' unless get_token() == :doublequot
elsif token1.is_a?(Numeric) || token2 == :add || token2 == :sub || token2 == :mul || token2 == :div
unget_token(token1)
result = sentence()
else
result = [:variable, token1]
end
fail Exception, "')'が見つからないよ" unless get_token() == :rpar
return result
end
def factor()
token = get_token()
minusflg = 1
if token == :sub
minusflg = -1
token = get_token()
end
if token.is_a? Numeric
return token * minusflg
elsif token == :lpar
result = expression()
fail Exception, 'unexpected token' unless get_token() == :rpar
return [:mul, minusflg, result]
elsif token == :doublequot # 文字列の計算
if @code =~ /\A\s*(.+)(")/
@code = $2 + $'
result = $1.to_s
end
fail Exception, '`"`が見つからないよ' unless get_token == :doublequot
return result
else # 変数の計算
return [:variable, token]
end
end
def assign()
var = get_token()
if @code =~ /\A\s*(=)/
@code = $'
else
fail Exception, "'='が見つからないよ"
end
token1 = get_token()
if token1 == :doublequot
if @code =~ /\A\s*(.+)(")/
@code = $2 + $'
result = $1.to_s
end
fail Exception, '`"`が見つからないよ' unless get_token() == :doublequot
return [:assignment, [var, result]]
end
if token1.to_s =~ /\A\s*(\d+|\d+\.\d+)\z/
unget_token(token1)
return [:assignment, [var, sentence()]]
else
token2 = get_token()
unget_token(token2)
if token2 == %i(add sub mul div)
unget_token(token1)
return [:assignment, [var, sentence()]]
end
return [:assignment, [var, [:variable, token1]]]
end
end
# blockの中身
def in_block()
block = ''
if @code =~ /(?<paren>{(?:[^{}]|\g<paren>)*})/
block = $1
@code = $'
if block =~ /({)/
block = $'
else
fail Exception, "'{'が見つからないよ"
end
if block =~ /\A\s*(.+)(})/m
block = $1
else
fail Exception, "'}'が見つからないよ"
end
else
fail Exception, "'{}'が見つからないよ"
end
code, result = @code, @result
@code = block
sentences()
block = @result
@code, @result = code, result
block.unshift(:block)
end
def par_while()
num = get_token()
if num.is_a? Numeric
num.to_i
block = in_block()
return [:while,num,block]
else
raise Exception,"ループ回数を指定してください。"
end
end
def sentences()
@result = []
while true
break if @code =~ /\A\s+\z/ || @code == ''
@result << sentence()
end
end
def sentence()
token1 = get_token()
token2 = get_token()
unget_token(token2)
# if token1.is_a?(Numeric) || token2 == %i(add sub mul div)
if token1.is_a?(Numeric) || token2 == :add || token2 == :sub || token2 == :mul || token2 == :div
unget_token(token1)
return expression()
end
# 変数
if token1 =~ /\A\s*([a-zA-z]\w*)/ && !token1.instance_of?(Symbol)
unget_token(token1)
return assign()
end
case token1
when :output
return [:output, check()]
when :if
return par_if()
when :while
return par_while()
when :read
return pre_read()
end
end
#解釈実行
def evaluate()
p @result
@result.each { |e|
eval(e)
}
# p @space
end
def eval(exp)
if exp.instance_of?(Array)
case exp[0]
when :add
return eval(exp[1]) + eval(exp[2])
when :sub
return eval(exp[1]) - eval(exp[2])
when :mul
return eval(exp[1]) * eval(exp[2])
when :div
return eval(exp[1]) / eval(exp[2])
when :input
print "->"
when :output
puts eval(exp[1])
when :while
#回数とblock
flt_while(exp[1],exp[2])
when :assignment
@space[exp[1][0]] = eval(exp[1][1])
when :variable
return @space[exp[1]] ? eval(@space[exp[1]]) : '0'
when :block
flt_block(exp)
end
else
return exp
end
end
def flt_block(block)
block.each { |b|
eval(b)
}
end
def flt_while(num,block)
start = 1
fin = num
while true
eval(block)
break if start == fin
start += 1
end
end
end
flt = Flat.new
begin
File.open(ARGV[0]) { |file|
flt.code = file.read
}
rescue
puts 'file not open'
exit
end
flt.sentences()
flt.evaluate()