File indexing completed on 2024-04-21 03:45:23

0001 #  SPDX-FileCopyrightText: 2009 Cies Breijs
0002 #  SPDX-FileCopyrightText: 2009 Niels Slot
0003 #
0004 #  SPDX-License-Identifier: GPL-2.0-or-later
0005 
0006 require 'rubygems'
0007 require 'singleton'
0008 
0009 begin
0010   require 'Qt'
0011 rescue LoadError
0012   begin
0013     require 'rbus'
0014   rescue LoadError
0015     puts "Couldn't load either rdbus or Qt, please install one, quiting now.."
0016     exit
0017   end
0018 end
0019 
0020 class Interpreter
0021   include Singleton
0022 
0023   def connect
0024     @pid = ENV['KTURTLE_INTERPRETER_DBUS_PID'] || IO.readlines("pid")[0].to_i
0025     puts "Opening DBus connection with KTurtle (pid: #{@pid})..."
0026 
0027     # Use Qt DBus if it's found, otherwise fall back to rbus
0028     if Object.const_defined?(:Qt)
0029       @interpreter = Qt::DBusInterface.new("org.kde.kturtle-#{@pid}", '/Interpreter', 'org.kde.kturtle.Interpreter')
0030     else
0031       @interpreter = RBus.session_bus.get_object("org.kde.kturtle-#{@pid}", '/Interpreter')
0032       @interpreter.interface!('org.kde.kturtle.Interpreter')
0033     end
0034   end
0035 
0036   def load(code); connect unless @pid; @interpreter.initialize code; nil; end
0037   def interpret;  @interpreter.interpret; nil; end
0038   def errors?;    @interpreter.encounteredErrors; end
0039   def errors;     @interpreter.getErrorStrings; end
0040   def state;      [:uninitialized, :initialized, :parsing, :executing, :finished, :aborted][@interpreter.state]; end
0041   def inspect;    "#<Interpreter pid:#{@pid}>"; end
0042 
0043   def run(code)
0044     load code
0045     while not [:finished, :aborted].include? state
0046       interpret
0047     end
0048     self  # return self for easy method stacking
0049   end
0050 
0051   def should_run_clean(code)
0052     run(code)
0053     errors?.should == false
0054     p errors if errors?
0055   end
0056 end