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
Either String TranslationUnit
source <- String -> IO (Either String TranslationUnit)
parseCFile String
fp
case TranslationUnit -> Either String [String]
cstructs2CopilotDecls (TranslationUnit -> Either String [String])
-> Either String TranslationUnit -> Either String [String]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Either String TranslationUnit
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
Either String String
content <- String -> IO (Either String String)
S.safeReadFile String
fp'
Either String TranslationUnit -> IO (Either String TranslationUnit)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String TranslationUnit
-> IO (Either String TranslationUnit))
-> Either String TranslationUnit
-> IO (Either String TranslationUnit)
forall a b. (a -> b) -> a -> b
$ [Token] -> Either String TranslationUnit
C.pTranslationUnit ([Token] -> Either String TranslationUnit)
-> (String -> [Token]) -> String -> Either String TranslationUnit
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [Token]
C.myLexer (String -> Either String TranslationUnit)
-> Either String String -> Either String TranslationUnit
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Either String String
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