module Command.CStructs2Copilot
( cstructs2Copilot
, ErrorCode
)
where
import Data.String.Extra as S ( safeReadFile )
import Command.Result ( Result (..) )
import Data.Location ( Location (..) )
import qualified Language.C.AbsC as C ( TranslationUnit )
import qualified Language.C.ParC as C ( myLexer, pTranslationUnit )
import Language.Trans.CStructs2Copilot ( cstructs2CopilotDecls )
cstructs2Copilot :: FilePath
-> IO (Result ErrorCode)
cstructs2Copilot :: String -> IO (Result ErrorCode)
cstructs2Copilot String
fp = do
source <- String -> IO (Either String TranslationUnit)
parseCFile String
fp
case cstructs2CopilotDecls =<< source of
Right [String]
decls -> [String] -> IO ()
printDecls [String]
decls IO () -> IO (Result ErrorCode) -> IO (Result ErrorCode)
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Result ErrorCode -> IO (Result ErrorCode)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Result ErrorCode
forall a. Result a
Success
Left String
msg -> Result ErrorCode -> IO (Result ErrorCode)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Result ErrorCode -> IO (Result ErrorCode))
-> Result ErrorCode -> IO (Result ErrorCode)
forall a b. (a -> b) -> a -> b
$ ErrorCode -> String -> Location -> Result ErrorCode
forall a. a -> String -> Location -> Result a
Error ErrorCode
ecCStructError String
msg (String -> Location
LocationFile String
fp)
where
parseCFile :: FilePath -> IO (Either String C.TranslationUnit)
parseCFile :: String -> IO (Either String TranslationUnit)
parseCFile String
fp' = do
content <- String -> IO (Either String String)
S.safeReadFile String
fp'
return $ C.pTranslationUnit . C.myLexer =<< content
printDecls :: [ String ] -> IO ()
printDecls :: [String] -> IO ()
printDecls = String -> IO ()
putStrLn (String -> IO ()) -> ([String] -> String) -> [String] -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [String] -> String
unlines
type ErrorCode = Int
ecCStructError :: ErrorCode
ecCStructError :: ErrorCode
ecCStructError = ErrorCode
1