Sha256: f36d8faa9ca339c81cd24e6d39a890c9136bdb3959b774274152962ab5bf67b9

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module Qlang
  module Exec
    class Compiler
      def initialize(args)
        @args = args
      end

      def output!
        ch_compile_type(@args.first)
        parse_string = parse(@args[1])
        write!(@args[2], parse_string)

      rescue Exception => e
        raise e if @options[:trace] || e.is_a?(SystemExit)

        print "#{e.class}: " unless e.class == RuntimeError
        puts "#{e.message}"
        puts "  Use --trace for backtrace."
        exit 1
      ensure
        exit 0
      end

      private

        def ch_compile_type(lang)
          case lang
          when '-Ruby'
            Qlang.to_ruby
          when '-R'
            Qlang.to_r
          else
            print 'Q support only Ruby and R now.'
          end
        end

        def parse(file_path)
          file = open_file(file_path)
          input_string = read_file(file)
          file.close
          input_string.gsub(/(.*)I love mathematics\.(.*)Q\.E\.D(.*)/m) {
            "#{$1}#{Kconv.tosjis(Qlang.compile($2))}#{$3}"
          }
        end

        def write!(output_path, string)
          open(output_path, 'w') do |f|
            f.puts string
          end
        end

        def open_file(filename, flag = 'r')
          return if filename.nil?
          File.open(filename, flag)
        end

        def read_file(file)
          file.read
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
qlang-0.0.27182100 lib/qlang/exec.rb
qlang-0.0.27182000 lib/qlang/exec.rb