-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.rb
More file actions
78 lines (61 loc) · 1.06 KB
/
text.rb
File metadata and controls
78 lines (61 loc) · 1.06 KB
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
require 'text_line'
require 'error'
module TinyBasic
class Text
attr_reader :lines, :current
def initialize
clear
end
def << line
case line
when String
self << TextLine.new(line)
when TextLine
@current = line
unless @current.direct?
if @current.has_statements?
lines[line.no] = @current unless @current.direct?
else
lines.delete(@current.no)
end
end
@current
else
raise WhatError.new(line)
end
end
def empty?
@lines.empty?
end
def clear
@lines = {}
end
def exec_print_list no
lines.keys.sort.each do |n|
next if n < no
l = lines[n]
puts "#{l.no} #{l.statements}"
end
end
# find line
def find_line no
@lines[no]
end
def find_next_line no
if no.is_a? TextLine
return find_next_line no.no
end
@lines.keys.each do |n|
return @lines[n] if n > no
end
nil
end
def first_line
find_next_line 0
end
def next_line line
find_next_line line
end
private
end
end