diff options
author | Joe Crayne <joe@jerkface.net> | 2018-11-21 15:11:29 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2018-11-21 15:11:29 -0500 |
commit | 1e20d8c100104e15d80c7f5ea61223957460a754 (patch) | |
tree | 6a75b560ef16fd9621e70eb9772dbeaab1a6fe56 | |
parent | 247fc0956e73356ec9339d9d8033749f707a3f38 (diff) |
Write output files.
-rw-r--r-- | c2haskell.hs | 46 |
1 files changed, 31 insertions, 15 deletions
diff --git a/c2haskell.hs b/c2haskell.hs index df97487..0e857a9 100644 --- a/c2haskell.hs +++ b/c2haskell.hs | |||
@@ -28,6 +28,7 @@ import Language.Haskell.Exts.Syntax as HS | |||
28 | import Language.Haskell.TH | 28 | import Language.Haskell.TH |
29 | import Language.Haskell.TH.Ppr | 29 | import Language.Haskell.TH.Ppr |
30 | import Language.Haskell.TH.Syntax as TH | 30 | import Language.Haskell.TH.Syntax as TH |
31 | import System.Directory | ||
31 | import System.Environment | 32 | import System.Environment |
32 | import System.IO | 33 | import System.IO |
33 | import System.Process | 34 | import System.Process |
@@ -390,10 +391,16 @@ c2haskell opts cs missings (CTranslUnit edecls _) = do | |||
390 | es = Map.filter (\d -> symbolLocal d && not (symbolStatic d)) (syms db) | 391 | es = Map.filter (\d -> symbolLocal d && not (symbolStatic d)) (syms db) |
391 | case selectFunction opts of | 392 | case selectFunction opts of |
392 | Nothing -> do | 393 | Nothing -> do |
393 | putStrLn $ "module T where" | 394 | createDirectoryIfMissing False "MonkeyPatch" |
394 | putStrLn $ "import Foreign.Ptr" | 395 | let fname = ("MonkeyPatch/" ++ modname ++ ".hs") |
395 | putStrLn $ "import Data.Word" | 396 | modname = "T" -- todo |
396 | putStrLn $ "import Data.Int" | 397 | stubsname = "MonkeyPatch/t_stubs.c" -- todo |
398 | putStrLn $ "writing " ++ fname | ||
399 | withFile fname WriteMode $ \haskmod -> do | ||
400 | hPutStrLn haskmod $ "module MonkeyPatch." ++ modname ++" where" | ||
401 | hPutStrLn haskmod $ "import Foreign.Ptr" | ||
402 | hPutStrLn haskmod $ "import Data.Word" | ||
403 | hPutStrLn haskmod $ "import Data.Int" | ||
397 | putStrLn $ "-- " ++ show (fmap (fmap (fmap (const ()))) <$> Map.lookup "ip_is_lan" (syms db)) | 404 | putStrLn $ "-- " ++ show (fmap (fmap (fmap (const ()))) <$> Map.lookup "ip_is_lan" (syms db)) |
398 | let sigs = concatMap getsig (Map.toList es) | 405 | let sigs = concatMap getsig (Map.toList es) |
399 | sigs2 = concatMap (\s -> do | 406 | sigs2 = concatMap (\s -> do |
@@ -407,7 +414,7 @@ c2haskell opts cs missings (CTranslUnit edecls _) = do | |||
407 | putStrLn $ "-- ip_is_lan `elem` db = " ++ show (length . symbolSource <$> Map.lookup "ip_is_lan" (syms db)) | 414 | putStrLn $ "-- ip_is_lan `elem` db = " ++ show (length . symbolSource <$> Map.lookup "ip_is_lan" (syms db)) |
408 | putStrLn $ "-- ip_is_lan `elem` sigs2 = " ++ show (elem "ip_is_lan" sigs2) | 415 | putStrLn $ "-- ip_is_lan `elem` sigs2 = " ++ show (elem "ip_is_lan" sigs2) |
409 | forM_ (uniq $ ts ++ sigs2) $ \t -> do | 416 | forM_ (uniq $ ts ++ sigs2) $ \t -> do |
410 | putStrLn $ "data " ++ t | 417 | hPutStrLn haskmod $ "data " ++ t |
411 | forM_ sigs $ \(_,(k,hs)) -> do | 418 | forM_ sigs $ \(_,(k,hs)) -> do |
412 | forM_ hs $ \hdecl -> do | 419 | forM_ hs $ \hdecl -> do |
413 | {- | 420 | {- |
@@ -423,11 +430,11 @@ c2haskell opts cs missings (CTranslUnit edecls _) = do | |||
423 | htyp -> -- putStr $ commented $ "Unsupported haskell type: " ++ HS.prettyPrint htyp | 430 | htyp -> -- putStr $ commented $ "Unsupported haskell type: " ++ HS.prettyPrint htyp |
424 | -} | 431 | -} |
425 | let htyp = makeFunctionUseIO $ extractType hdecl | 432 | let htyp = makeFunctionUseIO $ extractType hdecl |
426 | putStrLn $ (if isAcceptableImport htyp then id else commented) | 433 | hPutStrLn haskmod $ (if isAcceptableImport htyp then id else commented) |
427 | $ HS.prettyPrint $ (HS.ForImp () (HS.CCall ()) Nothing (Just k) | 434 | $ HS.prettyPrint $ (HS.ForImp () (HS.CCall ()) Nothing (Just k) |
428 | (HS.Ident () k) | 435 | (HS.Ident () k) |
429 | htyp) | 436 | htyp) |
430 | forM_ missings $ \sym -> goMissing db sym | 437 | forM_ missings $ \sym -> goMissing haskmod db sym |
431 | {- | 438 | {- |
432 | forM_ (Map.lookup sym $ syms db) $ \si -> do | 439 | forM_ (Map.lookup sym $ syms db) $ \si -> do |
433 | forM_ (take 1 $ symbolSource si) $ \d -> do | 440 | forM_ (take 1 $ symbolSource si) $ \d -> do |
@@ -452,6 +459,15 @@ c2haskell opts cs missings (CTranslUnit edecls _) = do | |||
452 | mapM_ (putStr . commented . show . pretty) (mutations (body d)) | 459 | mapM_ (putStr . commented . show . pretty) (mutations (body d)) |
453 | -} | 460 | -} |
454 | -} | 461 | -} |
462 | putStrLn $ "writing " ++ stubsname | ||
463 | withFile stubsname WriteMode $ \stubsfile -> do | ||
464 | forM_ missings $ \sym -> | ||
465 | forM_ (Map.lookup sym$ syms db) $ \si -> do | ||
466 | forM_ (take 1 $ symbolSource si) $ \d -> do | ||
467 | hPutStrLn stubsfile $ show $ pretty $ makeFunctionPointer d | ||
468 | hPutStrLn stubsfile $ show $ pretty $ makeSetter d | ||
469 | hPutStrLn stubsfile $ show $ pretty $ makeStub d | ||
470 | |||
455 | Just cfun -> do | 471 | Just cfun -> do |
456 | forM_ (Map.lookup cfun $ syms db) $ \si -> do | 472 | forM_ (Map.lookup cfun $ syms db) $ \si -> do |
457 | forM_ (take 1 $ symbolSource si) $ \d -> do | 473 | forM_ (take 1 $ symbolSource si) $ \d -> do |
@@ -638,8 +654,8 @@ setterBody name = | |||
638 | 654 | ||
639 | 655 | ||
640 | goMissing :: Show b => | 656 | goMissing :: Show b => |
641 | Transpile [CExternalDeclaration b] -> String -> IO () | 657 | Handle -> Transpile [CExternalDeclaration b] -> String -> IO () |
642 | goMissing db cfun = do | 658 | goMissing haskmod db cfun = do |
643 | forM_ (Map.lookup cfun $ syms db) $ \si -> do | 659 | forM_ (Map.lookup cfun $ syms db) $ \si -> do |
644 | forM_ (take 1 $ symbolSource si) $ \d -> do | 660 | forM_ (take 1 $ symbolSource si) $ \d -> do |
645 | -- putStr $ commented (ppShow (fmap (const ()) d)) | 661 | -- putStr $ commented (ppShow (fmap (const ()) d)) |
@@ -648,16 +664,16 @@ goMissing db cfun = do | |||
648 | let ts = filter notKnown $ map tname $ pointers $ concatMap types $ sigf hsTransSig d | 664 | let ts = filter notKnown $ map tname $ pointers $ concatMap types $ sigf hsTransSig d |
649 | -- forM_ ts $ \t -> putStrLn $ "data " ++ t | 665 | -- forM_ ts $ \t -> putStrLn $ "data " ++ t |
650 | forM_ (sigf hsTransSig d) $ \hs -> do | 666 | forM_ (sigf hsTransSig d) $ \hs -> do |
651 | putStrLn . HS.prettyPrint $ makeAcceptableDecl hs | 667 | hPutStrLn haskmod . HS.prettyPrint $ makeAcceptableDecl hs |
652 | case hs of | 668 | case hs of |
653 | HS.TypeDecl _ (DHead _ (HS.Ident _ signame)) ftyp -> do | 669 | HS.TypeDecl _ (DHead _ (HS.Ident _ signame)) ftyp -> do |
654 | let wrapname = "wrap" ++ drop 3 signame | 670 | let wrapname = "wrap" ++ drop 3 signame |
655 | settername = "setf" ++ drop 3 signame | 671 | settername = "setf" ++ drop 3 signame |
656 | funptr = (TyApp () (TyCon () (UnQual () (HS.Ident () "FunPtr"))) | 672 | funptr = (TyApp () (TyCon () (UnQual () (HS.Ident () "FunPtr"))) |
657 | (TyCon () (UnQual () (HS.Ident () signame)))) | 673 | (TyCon () (UnQual () (HS.Ident () signame)))) |
658 | -- putStrLn $ ppShow $ HS.parseDecl "foreign import ccall \"wrapper\" fname :: Spec -> IO (FunPtr Spec)" | 674 | -- hPutStrLn haskmod $ ppShow $ HS.parseDecl "foreign import ccall \"wrapper\" fname :: Spec -> IO (FunPtr Spec)" |
659 | -- mapM_ (putStrLn . HS.prettyPrint) (importWrapper $ sigf hsTransSig d) | 675 | -- mapM_ (hPutStrLn haskmod . HS.prettyPrint) (importWrapper $ sigf hsTransSig d) |
660 | putStrLn $ HS.prettyPrint $ | 676 | hPutStrLn haskmod $ HS.prettyPrint $ |
661 | (HS.ForImp () (HS.CCall ()) Nothing (Just "wrapper") | 677 | (HS.ForImp () (HS.CCall ()) Nothing (Just "wrapper") |
662 | (HS.Ident () wrapname) | 678 | (HS.Ident () wrapname) |
663 | (TyFun () | 679 | (TyFun () |
@@ -665,7 +681,7 @@ goMissing db cfun = do | |||
665 | (TyApp () | 681 | (TyApp () |
666 | (TyCon () (UnQual () (HS.Ident () "IO"))) | 682 | (TyCon () (UnQual () (HS.Ident () "IO"))) |
667 | (TyParen () funptr)))) | 683 | (TyParen () funptr)))) |
668 | putStrLn $ HS.prettyPrint $ | 684 | hPutStrLn haskmod $ HS.prettyPrint $ |
669 | (HS.ForImp () (HS.CCall ()) Nothing (Just settername) | 685 | (HS.ForImp () (HS.CCall ()) Nothing (Just settername) |
670 | (HS.Ident () settername) | 686 | (HS.Ident () settername) |
671 | (TyFun () | 687 | (TyFun () |
@@ -675,7 +691,7 @@ goMissing db cfun = do | |||
675 | (TyCon () (Special () (UnitCon ())))))) | 691 | (TyCon () (Special () (UnitCon ())))))) |
676 | 692 | ||
677 | 693 | ||
678 | htyp -> putStr $ commented $ "Unsupported haskell type: " ++ HS.prettyPrint htyp | 694 | htyp -> hPutStr haskmod $ commented $ "Unsupported haskell type: " ++ HS.prettyPrint htyp |
679 | 695 | ||
680 | 696 | ||
681 | readComments :: (Num lin, Num col) => | 697 | readComments :: (Num lin, Num col) => |