2022-02-15 17:06:19 +01:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-02-16 16:23:37 +01:00
|
|
|
require 'English'
|
|
|
|
require 'optparse'
|
2022-10-11 13:06:26 +02:00
|
|
|
require 'reline'
|
2022-02-15 17:06:19 +01:00
|
|
|
|
|
|
|
require 'rpl'
|
|
|
|
|
|
|
|
class RplRepl
|
2022-10-04 16:49:24 +02:00
|
|
|
def initialize( interpreter: Rpl.new )
|
2022-02-16 16:22:52 +01:00
|
|
|
@interpreter = interpreter
|
2022-02-15 17:06:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2022-02-16 16:22:52 +01:00
|
|
|
print_stack unless @interpreter.stack.empty?
|
|
|
|
|
2022-10-11 13:06:26 +02:00
|
|
|
Reline.completion_proc = proc do |s|
|
2022-02-15 17:06:19 +01:00
|
|
|
( @interpreter.dictionary.words.keys + @interpreter.dictionary.vars.keys ).grep(/^#{Regexp.escape(s)}/)
|
|
|
|
end
|
2022-10-11 13:06:26 +02:00
|
|
|
Reline.completion_append_character = ' '
|
2022-02-15 17:06:19 +01:00
|
|
|
|
2022-10-05 15:22:15 +02:00
|
|
|
prefill = ''
|
2022-02-15 17:06:19 +01:00
|
|
|
loop do
|
2022-10-11 13:06:26 +02:00
|
|
|
unless prefill.empty?
|
|
|
|
Reline.pre_input_hook = lambda do
|
|
|
|
Reline.insert_text( prefill.to_s )
|
|
|
|
Reline.redisplay
|
2022-10-05 15:22:15 +02:00
|
|
|
|
2022-10-11 13:06:26 +02:00
|
|
|
Reline.pre_input_hook = nil
|
|
|
|
end
|
2022-10-05 15:22:15 +02:00
|
|
|
end
|
|
|
|
|
2022-10-11 13:06:26 +02:00
|
|
|
input = Reline.readline( ' ', true )
|
2022-02-15 17:06:19 +01:00
|
|
|
|
2022-10-05 09:56:12 +02:00
|
|
|
break if input.nil? || input.strip == 'quit'
|
|
|
|
|
2022-10-05 15:22:15 +02:00
|
|
|
prefill = ''
|
|
|
|
|
|
|
|
if input.strip == 'edit'
|
|
|
|
prefill = @interpreter.stack.pop.to_s
|
|
|
|
elsif input.strip == 'history'
|
2022-10-05 09:56:12 +02:00
|
|
|
Readline::HISTORY.each do |line|
|
|
|
|
pp line
|
|
|
|
end
|
|
|
|
elsif input.empty?
|
|
|
|
# Remove blank lines from history
|
|
|
|
Readline::HISTORY.pop
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
@interpreter.run( input )
|
|
|
|
rescue ArgumentError => e
|
|
|
|
pp e
|
|
|
|
end
|
2022-02-15 17:06:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
print_stack
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def print_stack
|
|
|
|
stack_size = @interpreter.stack.size
|
|
|
|
|
|
|
|
@interpreter.stack.each_with_index do |elt, i|
|
2022-02-26 18:53:39 +01:00
|
|
|
puts "#{stack_size - i}: #{elt}"
|
2022-02-15 17:06:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-16 16:28:33 +01:00
|
|
|
options = { run_REPL: ARGV.empty?,
|
2022-10-04 16:49:24 +02:00
|
|
|
persistence_filename: File.expand_path( '~/.local/state/rpl.rb/machine' ),
|
|
|
|
live_persistence: true,
|
2022-02-16 16:23:37 +01:00
|
|
|
files: [],
|
|
|
|
programs: [] }
|
|
|
|
|
2022-10-05 09:27:26 +02:00
|
|
|
Version = Rpl::VERSION
|
2022-10-04 17:10:00 +02:00
|
|
|
|
2022-02-16 16:23:37 +01:00
|
|
|
OptionParser.new do |opts|
|
2022-10-05 15:22:15 +02:00
|
|
|
opts.on('-s', '--state filename', "persist state in filename (default: #{options[:persistence_filename]}) (will be created if needed)") do |filename|
|
2022-10-04 16:49:24 +02:00
|
|
|
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|
|
2022-02-16 16:23:37 +01:00
|
|
|
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!
|
|
|
|
|
2022-02-24 15:28:40 +01:00
|
|
|
# Instantiate interpreter
|
2022-10-04 16:49:24 +02:00
|
|
|
interpreter = Rpl.new( persistence_filename: options[:persistence_filename],
|
|
|
|
live_persistence: options[:live_persistence] )
|
2022-02-24 15:28:40 +01:00
|
|
|
|
|
|
|
# first run provided files if any
|
2022-02-16 16:23:37 +01:00
|
|
|
options[:files].each do |filename|
|
2022-10-04 14:57:13 +02:00
|
|
|
interpreter.run "\"#{File.expand_path( filename )}\" feval"
|
2022-02-16 16:23:37 +01:00
|
|
|
end
|
|
|
|
|
2022-02-24 15:28:40 +01:00
|
|
|
# second run provided code if any
|
2022-02-16 16:23:37 +01:00
|
|
|
options[:programs].each do |program|
|
|
|
|
interpreter.run program
|
|
|
|
end
|
|
|
|
|
2022-02-24 15:28:40 +01:00
|
|
|
# third launch REPL if (explicitely or implicitely) asked
|
2022-10-04 16:49:24 +02:00
|
|
|
if options[:run_REPL]
|
|
|
|
RplRepl.new( interpreter: interpreter ).run
|
|
|
|
else
|
|
|
|
interpreter.persist_state
|
|
|
|
end
|