module Feldspar.Compiler.Frontend.CommandLine.API where
import Feldspar.Compiler.Frontend.CommandLine.API.Library
import Feldspar.Compiler.Backend.C.Library
import System.IO
import Language.Haskell.Interpreter
import qualified Data.Typeable as T
data CompilationResult
= CompilationSuccess
| CompilationFailure
deriving (Eq, Show, T.Typeable)
generalInterpreterBody :: forall a . (T.Typeable (IO a))
=> String
-> Interpreter (IO a)
generalInterpreterBody expression = interpret expression (as::IO a)
highLevelInterpreter :: T.Typeable (IO a)
=> String
-> String
-> [String]
-> Bool
-> Bool
-> Interpreter (IO a)
-> IO CompilationResult
highLevelInterpreter moduleName inputFileName importList needGlobal needQualify interpreterBody = do
actionToExecute <- runInterpreter $ do
set [ languageExtensions := [GADTs, ScopedTypeVariables, TypeSynonymInstances, StandaloneDeriving,
DeriveDataTypeable, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses,
FunctionalDependencies, ExistentialQuantification, Rank2Types, TypeOperators,
EmptyDataDecls, GeneralizedNewtypeDeriving, TypeFamilies]
]
loadModules $ [inputFileName] ++ if needGlobal then importList else []
setTopLevelModules [moduleName]
if needQualify
then setImportsQ $ zip importList $ map Just importList
else setImports importList
interpreterBody
case actionToExecute of
Left err -> do
printInterpreterError err
return CompilationFailure
Right action -> do
action
return CompilationSuccess
printInterpreterError :: InterpreterError -> IO ()
printInterpreterError (WontCompile []) = return ()
printInterpreterError (WontCompile (x:xs)) = do
printGhcError x
printInterpreterError (WontCompile xs)
where
printGhcError (GhcError {errMsg=s}) = hPutStrLn stderr s
printInterpreterError e = hPutStrLn stderr $ "Code generation failed: " ++ show e