Class: Ichabod::Repl

Inherits:
Object
  • Object
show all
Defined in:
lib/ichabod/repl.rb

Constant Summary collapse

Prompt =
'js> '
AwaitingInput =
'?> '
Result =
'=> '
HistoryFile =
File.join(File.expand_path('~'), '.ichabod_history')

Class Method Summary collapse

Class Method Details

.load_historyObject



29
30
31
32
33
34
# File 'lib/ichabod/repl.rb', line 29

def self.load_history
  return unless File.exists? HistoryFile
  File.readlines(HistoryFile).each do |line|
    Readline::HISTORY.push line.chomp
  end
end

.quitObject



42
43
44
45
# File 'lib/ichabod/repl.rb', line 42

def self.quit
  save_history
  exit
end

.save_historyObject



36
37
38
39
40
# File 'lib/ichabod/repl.rb', line 36

def self.save_history
  File.open(HistoryFile, 'w') do |f|
    f.puts Readline::HISTORY.to_a.join("\n")
  end
end

.start(dom = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ichabod/repl.rb', line 10

def self.start(dom = nil)
  load_history
  @parser = Ichabod::Runtime.new(:dom => dom)

  loop do
    input = Readline.readline(Prompt)
    quit if input.nil?

    begin
      puts Result + @parser.eval(input).inspect.to_s
    rescue => e
      save_history
      raise e
    else
      Readline::HISTORY.push(input)
    end
  end
end