Files
rlox/rlox/prompt.rb
2018-07-27 17:50:56 +02:00

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