-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtested.rb
135 lines (114 loc) · 2.32 KB
/
tested.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
#まだ途中です...
#対応できていません
class Ed
def initialize
begin
@file = File.open(ARGV[0])
@size = 0
@currentline = 0
@maxline = 0
@buffer = []
rescue
end
if([email protected]?) then
@file.each do |line|
@buffer.push(line)
@size += line.bytesize
end
@currentline = @buffer.length
@maxline = @buffer.length
puts @size
puts @buffer.length
# puts @buffer
elsif(!ARGV[0].nil?) then
@filename = ARGV[0]
puts ARGV[0] + ": No such file or directory"
end
#modestate
#edit_mode: 0
#cmd_mode: 1
@mode = 1
#puts @buffer
#if @view is true and @mode:1 then print *
@view = false
@mark = "*"
loop do
read
eval
print
end
end
#コマンド受付
def read
#puts @mode
if(@mode==1 && @view) then printf @mark end
@input = STDIN.gets.chomp
#puts @cmd.size
end
#コマンド評価
# ed_ver.1
# add q,p(n),a,d,return
def eval
# regex_addr = '(?:\d+|[.$,;]|\/.*\/)'
# regex_cmnd = '(wq|[acdefgijkmnpqrsw]|\z)'
# regex_prmt = '(.*)'
@size = 0
@maxline = @buffer.length
@result = nil
@addr = @currentline
if(@input =~ /\A((?:\d+|[.$,;]|\/.*\/)(,(\d+|[.$,;]|\/.*\/))?)?(wq|[acdefgijkmnpqrswP]|\z)((.*))?\z/) then
if(!$~[1].nil?)
if($~[1].size > 1) then
@addr = $~[1].split(",")
@currentline = @addr[0].to_s
else
@addr = $~[1]
@currentline = @addr
end
end
@cmnd = $~[4]
# @prmt = $~[6]
# puts @addr
# puts @cmnd
# puts @prmt
self.send("#{@cmnd}")
@result = "ok"
# p $~.to_a
p @addr
# p @addr[1]
p @cmnd
else
@result = "?"
end
@maxline = @buffer.length
end
#command
private
def q
exit
end
def w
File.open(ARGV[0],"w") do |file|
@buffer.each do |line|
file.write(line)
@size += line.bytesize
end
end
end
def p
@result = "slected -> p"
end
def a
loop do
@message = STDIN.gets
if(@message =~ /\./) then break end
@buffer.insert(@currentline,@message)
@currentline += 1
end
end
#出力
def print
if([email protected]?) then puts @result end
end
end
Ed.new