Fix exit command; improve the interpreter

This commit is contained in:
Alex Kotov 2023-10-27 22:34:43 +04:00
parent eedb9ede3d
commit 15b24b8ebe
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 30 additions and 12 deletions

View File

@ -10,14 +10,6 @@ require 'referator'
$stdin.sync = true
$stdout.sync = true
def success(data = nil)
puts "OK #{data.to_json}"
end
def failure(data = nil)
puts "ERR #{data.to_json}"
end
def serialize_err(err)
{
class: err.class.name.freeze,
@ -33,12 +25,16 @@ workdir = File.expand_path(workdir).freeze
machine = Referator::Machine.new workdir
while (line = $stdin.gets)
while machine.running?
begin
line = $stdin.gets&.strip || 'exit'
line = 'null' if line.empty?
command = Referator::Command.parse line
result = machine.call command
success result
puts "OK #{result.to_json}"
rescue => e
failure serialize_err e
puts "ERR #{serialize_err(e).to_json}"
end
end

View File

@ -7,10 +7,14 @@ module Referator
def initialize(workdir)
self.workdir = workdir
@running = true
@config = Config.new self.workdir
@footnotes = Footnotes.new @config
end
def running? = @running
def call(command)
method_name = "do_#{command.name}"
raise 'Invalid command' unless respond_to? method_name, true
@ -33,7 +37,12 @@ module Referator
##################
def do_exit(_command)
exit
@running = false
nil
end
def do_null(_command)
nil
end
def do_ping(_command)

13
test.rb
View File

@ -89,6 +89,13 @@ def cmd(name, data = nil)
end
end
########
# NULL #
########
cmd('') { |result| raise unless result.nil? }
cmd(:NULL) { |result| raise unless result.nil? }
###################
# REGISTER_SCRIPT #
###################
@ -324,3 +331,9 @@ cmd(
raise unless result ==
"<sup><a href=\"#link-causa-arcana-com\">[4]</a></sup>\n"
end
########
# EXIT #
########
cmd(:EXIT) { |result| raise unless result.nil? }