#!/usr/bin/env ruby # frozen_string_literal: true require 'English' require 'optparse' require 'readline' require 'rpl' class RplRepl def initialize( interpreter: Rpl.new ) @interpreter = interpreter end def run print_stack unless @interpreter.stack.empty? Readline.completion_proc = proc do |s| ( @interpreter.dictionary.words.keys + @interpreter.dictionary.vars.keys ).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 pp e end print_stack end end def print_stack stack_size = @interpreter.stack.size @interpreter.stack.each_with_index do |elt, i| puts "#{stack_size - i}: #{elt}" end end end options = { run_REPL: ARGV.empty?, persistence_filename: File.expand_path( '~/.local/state/rpl.rb/machine' ), live_persistence: true, files: [], programs: [] } Version = '0.10.1' OptionParser.new do |opts| opts.on('-s', "--state filename", "persist state in filename (default: #{options[:persistence_filename]}) (will be created if needed)") do |filename| options[:persistence_filename] = File.expand_path( filename ) end opts.on('-q', '--no-state', 'Do not load persisted state') do options[:persistence_filename] = nil end opts.on('-d', '--no-persist', 'Do not persist state') do options[:live_persistence] = false end opts.on('-c', '--code "program"', 'run provided "program"') do |program| options[:programs] << program end opts.on('-f', '--file program.rpl', 'load program.rpl') do |filename| options[:files] << filename end opts.on('-i', '--interactive', 'launch interactive REPL') do options[:run_REPL] = true end end.parse! # Instantiate interpreter interpreter = Rpl.new( persistence_filename: options[:persistence_filename], live_persistence: options[:live_persistence] ) # first run provided files if any options[:files].each do |filename| interpreter.run "\"#{File.expand_path( filename )}\" feval" end # second run provided code if any options[:programs].each do |program| interpreter.run program end # third launch REPL if (explicitely or implicitely) asked if options[:run_REPL] RplRepl.new( interpreter: interpreter ).run else interpreter.persist_state end