rpl.rb/bin/rpl
2023-07-20 16:27:38 +02:00

168 lines
4.3 KiB
Ruby
Executable file

#!/usr/bin/env ruby
# frozen_string_literal: true
require 'English'
require 'optparse'
require 'reline'
require 'rpl'
class RplRepl
def initialize( interpreter: Rpl.new )
begin
@stty_save = `stty -g`.chomp
rescue Error => e
puts "Couldn't get terminal characteristics"
pp e
end
@interpreter = interpreter
end
def run!
print_stack unless @interpreter.stack.empty?
Reline.completion_proc = proc do |s|
( @interpreter.dictionary.words.keys + @interpreter.dictionary.vars.keys ).grep(/^#{Regexp.escape(s)}/)
end
Reline.completion_append_character = ' '
prefill = ''
loop do
unless prefill.empty?
Reline.pre_input_hook = lambda do
Reline.insert_text( prefill.to_s )
Reline.redisplay
Reline.pre_input_hook = nil
end
end
input = Reline.readline( ' ', true )
break if input.nil? || input.strip == 'quit'
prefill = ''
if input.strip == 'edit'
prefill = @interpreter.stack.pop.to_s
elsif input.strip == 'history'
history = Reline::HISTORY.map { |line| "\"#{line}\"" }.join(' ')
@interpreter.run!( "{ #{history} }" )
elsif input.empty?
# Remove blank lines from history
Reline::HISTORY.pop
else
begin
@interpreter.run!( input )
rescue ArgumentError => e
pp e
end
end
print_lcd if @interpreter.show_lcd
print_stack
rescue Interrupt
puts '^C'
`stty #{@stty_save}` if @stty_save
exit 0
end
end
def print_lcd
puts @interpreter.lcd_grob.to_braille
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
def persistence_filename
persistence_dir = ENV.fetch('XDG_DATA_HOME', nil)
persistence_dir ||= '~/.local/share'
persistence_dir += '/rpl.rb'
File.expand_path( "#{persistence_dir}/env.rpl" )
end
options = { run_REPL: false,
persistence: true,
live_persistence: true,
persistence_filename:,
files: [],
programs: [],
verbosity: :critical }
Version = Rpl::VERSION
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 nor save persisted state') do
warn "Not loading #{options[:persistence_filename]}." if options[:verbosity] == :debug
options[:persistence_filename] = nil
options[:live_persistence] = false
options[:persistence] = false
end
opts.on('-d', '--no-persist', 'Do not persist state') do
warn 'Not persisting this session.' if options[:verbosity] == :debug
options[:live_persistence] = false
options[: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
warn 'Will load REPL.' if options[:verbosity] == :debug
options[:run_REPL] = true
end
opts.on('-V', '--verbose "level"', 'set verbosity level') do |level|
options[:verbosity] = level.to_sym
warn "Setting verbosity to #{level.to_sym}" if options[:verbosity] == :debug
end
end.parse!
options[:run_REPL] = options[:files].empty? && options[:programs].empty?
warn "Loading state #{options[:persistence_filename]}." if options[:verbosity] == :debug && !options[:persistence_filename].nil?
# 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
RplRepl.new( interpreter: ).run! if options[:run_REPL]
interpreter.persist_state if options[:persistence]
puts interpreter.export_stack