Skip to content

Commit d2e9b10

Browse files
committed
v1.0.1
1 parent 39d900f commit d2e9b10

File tree

7 files changed

+29
-18
lines changed

7 files changed

+29
-18
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22

33
```rb
44
require 'haskell'
5+
6+
# Invoke sandbox for executing ghc
7+
Haskell.invoke_sandbox!(File.expand_path('../', __FILE__))
8+
59
Haskell.compile %{
610
add :: Integer -> Integer -> Integer
711
add x y = x + y
812
result = add 1 2
913
}
1014

1115
while Haskell.compiling?
12-
...
16+
# wait for....
1317
end
1418

1519
p Haskell.execute
1620
#=> 3
21+
22+
# Don't forget to revoke sandbox
23+
Haskell.revoke_sandbox!
1724
```

haskell_executing_sandbox/tmp

1.2 MB
Binary file not shown.

haskell_executing_sandbox/tmp.hi

865 Bytes
Binary file not shown.

haskell_executing_sandbox/tmp.hs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Main where
2+
3+
add :: Integer -> Integer -> Integer
4+
add x y = x + y
5+
result = add 1 2
6+
7+
main = do putStrLn $ show result

lib/haskell.rb

+11-14
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,42 @@ module Haskell
33
class HaskellCompileError < StandardError; end
44

55
class << self
6-
def invoke_sandbox!
7-
file_path = File.expand_path('../', __FILE__)
8-
$sandbox_path = "#{file_path}/haskell_executing_sandbox"
9-
FileUtils.mkdir($sandbox_path) unless Dir.exist?($sandbox_path)
6+
def invoke_sandbox!(dir_path)
7+
@@sandbox_path = "#{dir_path}/haskell_executing_sandbox"
8+
FileUtils.mkdir(@@sandbox_path) unless Dir.exist?(@@sandbox_path)
109
end
1110

1211
def revoke_sandbox!
13-
FileUtils.rm_rf($sandbox_path)
12+
FileUtils.rm_rf(@@sandbox_path)
1413
end
1514

1615
def compile(hs_code)
17-
FileUtils.touch("#{$sandbox_path}/COMPILING")
16+
FileUtils.touch("#{@@sandbox_path}/COMPILING")
1817
#TODO: need inform user prefer message
1918
Thread.abort_on_exception = true
2019
Thread.new do
2120
begin
2221
executable_code = executable_code(hs_code)
2322
puts_notation(executable_code)
24-
File.write("#{$sandbox_path}/tmp.hs", executable_code)
25-
Kernel.system("ghc #{$sandbox_path}/tmp.hs")
23+
File.write("#{@@sandbox_path}/tmp.hs", executable_code)
24+
Kernel.system("ghc #{@@sandbox_path}/tmp.hs")
2625
raise HaskellCompileError unless compiled?
2726
ensure
28-
FileUtils.rm("#{$sandbox_path}/COMPILING")
27+
FileUtils.rm("#{@@sandbox_path}/COMPILING")
2928
end
3029
end
31-
rescue
32-
raise "Something wrong...https://github.com/Azabuhs/Ruby/issues"
3330
end
3431

3532
def compiling?
36-
File.exist?("#{$sandbox_path}/COMPILING")
33+
File.exist?("#{@@sandbox_path}/COMPILING")
3734
end
3835

3936
def compiled?
40-
File.exist?("#{$sandbox_path}/tmp")
37+
File.exist?("#{@@sandbox_path}/tmp")
4138
end
4239

4340
def execute
44-
`#{$sandbox_path}/tmp`.gsub(/\n\z/, '')
41+
`#{@@sandbox_path}/tmp`.gsub(/\n\z/, '')
4542
end
4643

4744
private

lib/haskell/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Haskell
2-
VERSION = "1.0.0"
2+
VERSION = "1.0.1"
33
end

test/test_haskell.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ def test_main
2727

2828
private
2929
def assert_equal_execute(str, &block)
30-
Haskell.invoke_sandbox!
30+
Haskell.invoke_sandbox!(File.expand_path('../', __FILE__))
3131
Haskell.compile(block.call)
3232
nil while Haskell.compiling?
3333
assert_equal str, Haskell.execute
3434
Haskell.revoke_sandbox!
3535
end
3636

3737
def assert_raise_compile_error(&block)
38-
Haskell.invoke_sandbox!
38+
Haskell.invoke_sandbox!(File.expand_path('../', __FILE__))
3939
Haskell.compile(block.call)
4040
nil while Haskell.compiling?
4141
# TODO: more prefer test

0 commit comments

Comments
 (0)