Sha256: a0802e88386943e7927c680f4aeec050fa1b4a6b39bbe154014c41e3acb7a131

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

module Haskell
  # TODO:
  class HaskellCompileError < StandardError; end

  class << self
    def invoke_sandbox!(dir_path)
      @@sandbox_path = "#{dir_path}/haskell_executing_sandbox"
      FileUtils.mkdir(@@sandbox_path) unless Dir.exist?(@@sandbox_path)
    end

    def revoke_sandbox!
      FileUtils.rm_rf(@@sandbox_path)
    end

    def compile(hs_code)
      FileUtils.touch("#{@@sandbox_path}/COMPILING")
      #TODO: need inform user prefer message
      Thread.abort_on_exception = true
      Thread.new do
        begin
          executable_code = executable_code(hs_code)
          puts_notation(executable_code)
          File.write("#{@@sandbox_path}/tmp.hs", executable_code)
          Kernel.system("ghc #{@@sandbox_path}/tmp.hs")
          raise HaskellCompileError unless compiled?
        ensure
          FileUtils.rm("#{@@sandbox_path}/COMPILING")
        end
      end
    end

    def compiling?
      File.exist?("#{@@sandbox_path}/COMPILING")
    end

    def compiled?
      File.exist?("#{@@sandbox_path}/tmp")
    end

    def execute
      `#{@@sandbox_path}/tmp`.gsub(/\n\z/, '')
    end

  private

    def executable_code(hs_code)
      # TODO: other white space
      hs_code =~/\A\n( *)/
      hs_code.gsub!(/\n#{$1}/, "\n")
<<-HASKELL_CODE
module Main where
#{hs_code}
main = do putStrLn $ show result
HASKELL_CODE
    end

    def puts_notation(executable_code)
puts <<-NOTATION
# GHC will compile below code
###############################
NOTATION

puts '# ' + executable_code.gsub("\n", "\n# ")

puts <<-NOTATION
###############################
NOTATION
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
haskell-1.0.1 lib/haskell.rb