rpl.rb/repl.rb
2021-10-31 09:18:34 +01:00

53 lines
945 B
Ruby

# coding: utf-8
# frozen_string_literal: true
require "readline"
require "./lib/parser.rb"
def run_REPL( stack )
Readline.completion_proc = proc do |s|
directory_list = Dir.glob("#{s}*")
if directory_list.positive?
directory_list
else
Readline::HISTORY.grep(/^#{Regexp.escape(s)}/)
end
end
Readline.completion_append_character = " "
loop do
input = Readline.readline( "", true )
break if input == "exit"
# Remove blank lines from history
Readline::HISTORY.pop if input.empty?
stack = process_input( stack, input )
stack = display_stack( stack )
end
end
def process_input( stack, input )
parse_input( input ).each do |elt|
stack << elt
end
stack
end
def display_stack( stack )
stack_size = stack.size
stack.each_with_index { |elt, i| puts "#{stack_size - i}: #{elt["value"]}"}
stack
end
def stack_init
stack = []
stack
end
run_REPL( stack_init )