Class: Interpreter

Inherits:
Object
  • Object
show all
Includes:
Formatter
Defined in:
lib/interpreter.rb

Instance Method Summary collapse

Methods included from Formatter

#bold, #red

Constructor Details

#initializeInterpreter

Returns a new instance of Interpreter.



8
9
10
11
12
13
# File 'lib/interpreter.rb', line 8

def initialize
  Readline.completion_append_character = nil
  Readline.completion_proc = lambda do |input|
    commands.grep(/^#{Regexp.escape(input)}/)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *_args) ⇒ Object (private)



86
87
88
89
90
91
92
# File 'lib/interpreter.rb', line 86

def method_missing(name, *_args)
  index = (name.to_s[1..-1].to_i - 1).tap { |index| raise StandardError if index == -1 }
  expression = Readline::HISTORY[index]
  Readline::HISTORY.pop
  Readline::HISTORY << expression
  process(expression)
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/interpreter.rb', line 15

def call
  separator = ">"
  expression = ""
  begin
    expression += Readline.readline(prompt(separator), false)
    Readline::HISTORY << expression
    capture_stdout { eval(expression, TOPLEVEL_BINDING) }
  rescue SyntaxError => e
    if e.message =~ /syntax error, unexpected end-of-input/
      Readline::HISTORY.pop
      expression += "\n       "
      separator = "*"
      retry
    end
  rescue Exception => e
  end
  process(expression)
end