Fix exit command; improve the interpreter
This commit is contained in:
parent
eedb9ede3d
commit
15b24b8ebe
3 changed files with 30 additions and 12 deletions
|
@ -10,14 +10,6 @@ require 'referator'
|
||||||
$stdin.sync = true
|
$stdin.sync = true
|
||||||
$stdout.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)
|
def serialize_err(err)
|
||||||
{
|
{
|
||||||
class: err.class.name.freeze,
|
class: err.class.name.freeze,
|
||||||
|
@ -33,12 +25,16 @@ workdir = File.expand_path(workdir).freeze
|
||||||
|
|
||||||
machine = Referator::Machine.new workdir
|
machine = Referator::Machine.new workdir
|
||||||
|
|
||||||
while (line = $stdin.gets)
|
while machine.running?
|
||||||
begin
|
begin
|
||||||
|
line = $stdin.gets&.strip || 'exit'
|
||||||
|
line = 'null' if line.empty?
|
||||||
|
|
||||||
command = Referator::Command.parse line
|
command = Referator::Command.parse line
|
||||||
result = machine.call command
|
result = machine.call command
|
||||||
success result
|
|
||||||
|
puts "OK #{result.to_json}"
|
||||||
rescue => e
|
rescue => e
|
||||||
failure serialize_err e
|
puts "ERR #{serialize_err(e).to_json}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,10 +7,14 @@ module Referator
|
||||||
def initialize(workdir)
|
def initialize(workdir)
|
||||||
self.workdir = workdir
|
self.workdir = workdir
|
||||||
|
|
||||||
|
@running = true
|
||||||
|
|
||||||
@config = Config.new self.workdir
|
@config = Config.new self.workdir
|
||||||
@footnotes = Footnotes.new @config
|
@footnotes = Footnotes.new @config
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def running? = @running
|
||||||
|
|
||||||
def call(command)
|
def call(command)
|
||||||
method_name = "do_#{command.name}"
|
method_name = "do_#{command.name}"
|
||||||
raise 'Invalid command' unless respond_to? method_name, true
|
raise 'Invalid command' unless respond_to? method_name, true
|
||||||
|
@ -33,7 +37,12 @@ module Referator
|
||||||
##################
|
##################
|
||||||
|
|
||||||
def do_exit(_command)
|
def do_exit(_command)
|
||||||
exit
|
@running = false
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def do_null(_command)
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def do_ping(_command)
|
def do_ping(_command)
|
||||||
|
|
13
test.rb
13
test.rb
|
@ -89,6 +89,13 @@ def cmd(name, data = nil)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
########
|
||||||
|
# NULL #
|
||||||
|
########
|
||||||
|
|
||||||
|
cmd('') { |result| raise unless result.nil? }
|
||||||
|
cmd(:NULL) { |result| raise unless result.nil? }
|
||||||
|
|
||||||
###################
|
###################
|
||||||
# REGISTER_SCRIPT #
|
# REGISTER_SCRIPT #
|
||||||
###################
|
###################
|
||||||
|
@ -324,3 +331,9 @@ cmd(
|
||||||
raise unless result ==
|
raise unless result ==
|
||||||
"<sup><a href=\"#link-causa-arcana-com\">[4]</a></sup>\n"
|
"<sup><a href=\"#link-causa-arcana-com\">[4]</a></sup>\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
########
|
||||||
|
# EXIT #
|
||||||
|
########
|
||||||
|
|
||||||
|
cmd(:EXIT) { |result| raise unless result.nil? }
|
||||||
|
|
Loading…
Add table
Reference in a new issue