merge REPL in main file rpl.rb
This commit is contained in:
parent
80fbf082b2
commit
c2043ba663
1 changed files with 47 additions and 0 deletions
47
rpl.rb
47
rpl.rb
|
@ -1,5 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'readline'
|
||||
|
||||
require_relative './lib/interpreter'
|
||||
|
||||
require_relative './lib/core/branch'
|
||||
|
@ -551,3 +553,48 @@ class Rpl < Interpreter
|
|||
# Graphics
|
||||
end
|
||||
end
|
||||
|
||||
class RplRepl
|
||||
def initialize
|
||||
@interpreter = Rpl.new
|
||||
end
|
||||
|
||||
def run
|
||||
Readline.completion_proc = proc do |s|
|
||||
Readline::HISTORY.grep(/^#{Regexp.escape(s)}/)
|
||||
end
|
||||
Readline.completion_append_character = ' '
|
||||
|
||||
loop do
|
||||
input = Readline.readline( ' ', true )
|
||||
break if input.nil? || input == 'quit'
|
||||
|
||||
pp Readline::HISTORY if input == 'history'
|
||||
|
||||
# Remove blank lines from history
|
||||
Readline::HISTORY.pop if input.empty?
|
||||
|
||||
begin
|
||||
@interpreter.run( input )
|
||||
rescue ArgumentError => e
|
||||
p e
|
||||
end
|
||||
|
||||
print_stack
|
||||
end
|
||||
end
|
||||
|
||||
def format_element( elt )
|
||||
@interpreter.stringify( elt )
|
||||
end
|
||||
|
||||
def print_stack
|
||||
stack_size = @interpreter.stack.size
|
||||
|
||||
@interpreter.stack.each_with_index do |elt, i|
|
||||
puts "#{stack_size - i}: #{format_element( elt )}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
RplRepl.new.run if __FILE__ == $PROGRAM_NAME
|
||||
|
|
Loading…
Reference in a new issue