From 15b24b8ebe8fb524e4f60a7d3c6efcd5158d2802 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 27 Oct 2023 22:34:43 +0400 Subject: [PATCH] Fix exit command; improve the interpreter --- exe/referator | 18 +++++++----------- lib/referator/machine.rb | 11 ++++++++++- test.rb | 13 +++++++++++++ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/exe/referator b/exe/referator index 3553108..230b09d 100755 --- a/exe/referator +++ b/exe/referator @@ -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 diff --git a/lib/referator/machine.rb b/lib/referator/machine.rb index 9fa51df..de27e9e 100644 --- a/lib/referator/machine.rb +++ b/lib/referator/machine.rb @@ -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) diff --git a/test.rb b/test.rb index 1cbae33..df8050b 100755 --- a/test.rb +++ b/test.rb @@ -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 == "[4]\n" end + +######## +# EXIT # +######## + +cmd(:EXIT) { |result| raise unless result.nil? }