Class: Heist::REPL

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ REPL

Returns a new instance of REPL.

[View source]

6
7
8
9
10
11
12
13
14
# File 'lib/repl.rb', line 6

def initialize(options = {})
  @runtime = Runtime.new(options)
  @scope = Runtime::FileScope.new(@runtime.top_level, File.expand_path('.'))
  @results = []
  
  @runtime.define('~') { |x| @results[-x] }
  
  reset!
end

Instance Method Details

#runObject

[View source]

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
# File 'lib/repl.rb', line 16

def run
  puts @runtime.info
  
  Readline.completion_append_character = nil
  Readline.completion_proc = @runtime.top_level.method(:longest_prefix)
  
  loop do
    begin
      input  = Readline.readline(prompt)
      exit if input.nil?
      
      push(input)
      tree = Heist.parse(@buffer * "\n")
      next if tree.nil?
      
      reset!
      result = @scope.eval(tree)
      unless result.nil?
        puts "; => #{ Heist.stringify(result) }\n\n"
        @results << result
        @scope['^'] = result
      end
      
    rescue Exception => ex
      return if SystemExit === ex
      if Interrupt === ex
        reset! and puts "\n\n"
      else
        puts "; [error] #{ ex.message }\n\n"
      end
      
    # debugging
    #  puts "; backtrace: " + ex.backtrace.join("\n;            ") +
    #    "\n\n" unless Heist::RuntimeError === ex
    end
  end
end