所以我有一个haskell程序,我对haskell源文件做了一些格式化,然后我想调用GHC来编译所说的文件,有人能说我怎么能这样做?
如果这有帮助,这是我正在制作的程序,它只是一个表情符号代码haskell解析器:
module EmojiParser where
import Prelude hiding (writeFile, readFile)
import Data.Char
import Data.Text (pack, unpack)
import Path.Text.UTF8 (readFile, writeFile)
import Path.Internal
transpile filePath = do content <- readFile $ Path filePath
writeFile (Path filePath) . pack . parseEmoji . unpack $ content
parseEmoji :: [Char] -> [Char]
parseEmoji = foldl (++) "" . map f
where
minEmoji = 128511
maxEmoji = 128592
f x = let cval = ord x
in if cval > minEmoji && cval < maxEmoji
then "emoji_symbol_" ++ (show cval)
else [x]
答案 0 :(得分:0)
以下是ghc用户指南中的示例: https://downloads.haskell.org/~ghc/master/users-guide/extending_ghc.html#using-ghc-as-a-library
import GHC
import GHC.Paths ( libdir )
import DynFlags ( defaultLogAction )
main =
defaultErrorHandler defaultLogAction $ do
runGhc (Just libdir) $ do
dflags <- getSessionDynFlags
setSessionDynFlags dflags
target <- guessTarget "test_main.hs" Nothing
setTargets [target]
load LoadAllTargets
你应该能够做到这一点:
$ cat test_main.hs
main = putStrLn "hi"
$ ghc -package ghc simple_ghc_api.hs
[1 of 1] Compiling Main ( simple_ghc_api.hs, simple_ghc_api.o )
Linking simple_ghc_api ...
$ ./simple_ghc_api
$ ./test_main
hi
$
一旦它适合你,那么你应该很容易将它包含在你的程序中。