40 lines
694 B
Ruby
40 lines
694 B
Ruby
require "readline"
|
|
|
|
require './rlox/executor'
|
|
|
|
def prompt()
|
|
exec = Executor.new
|
|
|
|
f = File.new("#{Dir.home}/.rlox", "a+")
|
|
File.readlines("#{Dir.home}/.rlox").each { | line |
|
|
Readline::HISTORY.push line.rstrip
|
|
}
|
|
|
|
begin
|
|
while buf = Readline.readline("> ", true)
|
|
Readline::HISTORY.pop if /^\s*$/ =~ buf
|
|
|
|
if buf[-1] != ';'
|
|
buf << ';'
|
|
end
|
|
|
|
begin
|
|
res = exec.run(buf)
|
|
if res
|
|
res.each { | res |
|
|
puts res.to_s
|
|
}
|
|
end
|
|
rescue LoxError => err
|
|
STDERR.puts err
|
|
ensure
|
|
f.write("#{buf}\n")
|
|
end
|
|
exec.inc_line
|
|
end
|
|
rescue Interrupt
|
|
ensure
|
|
f.close
|
|
end
|
|
end
|