-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.rb
487 lines (429 loc) · 9.45 KB
/
program.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
require 'text'
require 'error'
module TinyBasic
ForVariable = Struct.new(:var, :limit, :step, :line)
class Program
attr_reader :text, :current
attr_reader :variables, :array
attr_reader :stack, :for_var
Commands = {
list: :exec_print_list,
run: :exec_run,
new: :clear,
next: :exec_next,
let: :parse_let,
if: :exec_if,
goto: :exec_goto,
gosub: :exec_gosub,
return: :exec_return,
rem: :not_implemented,
for: :exec_for,
input: :exec_input,
print: :exec_print,
stop: :exec_stop,
}
def initialize
@text = Text.new
@variables = {}
@array = []
@stack = []
end
# @return true: executed direct command; false: append a line to the text area
def << line
@current = text << line
if @current.direct?
exec_line @current
true
else
false
end
end
def exec_line line
line.start
until line.end_line?
cmd = line.command?
case cmd
when :run, :new
# Check there is nothing after the command.
raise WhatError.new(line) unless line.end_line?
send Commands[cmd]
when :stop
# Check there is nothing after the command.
raise WhatError.new(line) unless line.end_line?
return send Commands[cmd], line
when :goto, :gosub
return send Commands[cmd], line
when :list
n = line.number?
raise WhatError.new(line) unless line.end_line?
send Commands[cmd], n || 0
when :print, :if, :for
send Commands[cmd], line
when :next, :return, :input
line = send Commands[cmd], line
when :rem
line.stop
else
# Let
send Commands[:let], line
end
end
line.direct? ? nil : text.next_line(line)
end
private
# execuet command
def not_implemented
end
def exec_run line = nil
line ||= text.first_line
@current = line if line
while @current
@current = exec_line(@current)
@current&.start
end
end
def exec_print_list no
text.exec_print_list no
end
def clear
text.clear
end
def exec_stop line
line.stop
nil
end
def exec_goto line
no = expression line
next_line = text.find_line no
unless next_line
raise HowError.new(line)
end
# Check there is nothing or separator after the command.
raise WhatError.new(line) unless line.separator? || line.end_line?
next_line
end
def exec_if line
if expression(line) == 0
line.stop
end
end
def exec_for line
stack.push @for_var if @for_var
var = line.variable?
idx = nil
if var == '@'
idx = parenthesis(line)
end
unless line.equal?
raise WhatError.new(line)
end
if var == '@'
array[idx] = expression(line)
else
variables[var] = expression(line)
end
unless line.other_key? == :to
raise WhatError.new(line)
end
limit = expression(line)
step = 1
if line.other_key? == :step
step = expression(line)
end
@for_var = ForVariable.new(idx || var, limit, step, TextLine.new(line))
# remove duplicated var's data
duplicated = stack.find{|e| e.is_a?(ForVariable) && e.var == @for_var.var}
stack.delete duplicated
line
end
def exec_next line
var = line.variable?
if @for_var.nil? || var.nil? || @for_var.var != var
raise WhatError.new(line)
end
var = @for_var.var
step = @for_var.step
val = nil
# add step
case var
when String
val = variables[var] += step
when Integer
val = array[var] += step
end
# check loop end
end_for = false
if step < 0
if val < @for_var.limit
end_for = true
end
else
if val > @for_var.limit
end_for = true
end
end
if end_for
@for_var = stack.pop
line
else
TextLine.new(@for_var.line)
end
end
def exec_gosub line
no = expression(line)
unless line.separator? || line.end_line?
raise WhatError.new(line)
end
next_line = text.find_line no
unless next_line
raise HowError.new(line)
end
gosub_var = ForVariable.new(nil, nil, nil, TextLine.new(line))
stack.push(gosub_var)
next_line
end
def exec_return line
unless line.separator? || line.end_line?
raise WhatError.new(line)
end
gosub_var = stack.pop
unless gosub_var && gosub_var&.var.nil?
raise WhatError.new(line)
end
TextLine.new(gosub_var.line)
end
def exec_print line
digits = 6
loop do
comma = line.comma?
if line.separator? || line.end_line?
puts unless comma
return
elsif line.sharp?
digits = expression(line)
elsif str = line.string?
print str
else
v = expression(line)
print v.to_s.rjust(digits, ' ')
end
end
end
def exec_input line
loop do
input_line = TextLine.new(line)
var = nil; idx = nil
if str = input_line.string?
# print prompte if there is a string
print "#{str}:"
# check variable
if var = input_line.variable?
if var == "@"
idx = expression(input_line)
unless idx
raise HowError.new(input_line)
end
end
end
else
# it must be a variable
unless var = input_line.variable?
raise WhatError.new(input_line)
end
if var == "@"
idx = expression(input_line)
unless idx
raise HowError.new(input_line)
end
print "#{var}(#{idx}):"
else
print "#{var}:"
end
end
# set a value to the variable
if var
begin
# put zero first to be a direct command
buf_line = TextLine.new("0 #{gets}")
val = expression(buf_line)
unless val
raise WhatError.new()
end
if var == "@"
array[idx] = val
else
variables[var] = val
end
rescue => e
case e
when TinyBasicError
puts e
else
puts HowError.new(TextLine.new(""))
end
next
end
end
# check end line
if input_line.separator? || input_line.end_line?
return input_line
end
# check required the next variable
unless input_line.comma?
raise WhatError.new(input_line)
end
line = input_line
end
end
def parse_let line
loop do
if line.separator? || line.end_line?
return
elsif var = line.variable?
idx = nil
if var == '@'
idx = parenthesis(line)
end
unless line.equal?
raise WhatError.new(line)
end
if var == '@'
array[idx] = expression(line)
else
variables[var] = expression(line)
end
unless line.comma?
if line.separator? || line.end_line?
return
else
raise WhatError.new(line)
end
end
else
raise WhatError.new(line)
end
end
end
def expression line
expression_1(line)
end
def expression_1 line
v1 = expression_2(line)
op = line.operator?
unless op
return v1
end
v2 = expression_2(line)
case op
when :">="
v1 >= v2 ? 1 : 0
when :"#"
v1 != v2 ? 1 : 0
when :">"
v1 > v2 ? 1 : 0
when :"="
v1 == v2 ? 1 : 0
when :"<="
v1 <= v2 ? 1 : 0
when :"<"
v1 < v2 ? 1 : 0
end
end
def expression_2 line
v1 = expression_3(line)
loop do
if line.adding?
v2 = expression_3(line)
v = v1 + v2
unless -32768 <= v && v <= 32767
raise HowError.new(line)
end
v1 = v
elsif line.subtracting?
v2 = expression_3(line)
v = v1 - v2
unless -32768 <= v && v <= 32767
raise HowError.new(line)
end
v1 = v
else
break
end
end
v1
end
def expression_3 line
v1 = expression_4(line)
loop do
if line.multiplying?
v2 = expression_4(line)
v = v1 * v2
unless -32768 <= v && v <= 32767
raise HowError.new(line)
end
v1 = v
elsif line.dividing?
v2 = expression_4(line)
v1 = v1 / v2
else
break
end
end
v1
end
def parenthesis line
unless line.left_parenthesis?
raise WhatError.new(line)
end
v = expression(line)
unless line.right_parenthesis?
raise WhatError.new(line)
end
v
end
def expression_4(line)
case line.function?
when :abs
v = parenthesis line
v.abs
when :rnd
v = parenthesis line
unless 1 <= v && v <= 32767 - 1
raise HowError.new(line)
end
rand(v) + 1
when :size
# DUMMY SIZE
1024 * 1024
else
var = line.variable?
case var
when nil
n = line.number?
if n
n
else
if line.current == '('
parenthesis line
else
nil
end
end
when '@'
# array a[0-32767]
n = parenthesis line
unless n
raise WhatError.new(line)
end
unless 0 <= n && n <= 32767
raise HowError.new(line)
end
array[n] || 0
else
# variable 'A'-'Z'
variables[var] || 0
end
end
end
end
end