diff options
author | Joe Crayne <joe@jerkface.net> | 2019-03-10 19:42:22 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-03-10 19:42:22 -0400 |
commit | ef0bd9baee906ebf7c3293f0e5ec531bca0b4801 (patch) | |
tree | 9347d3cb5f0b5b1ad05f51da37c0a907708ccff7 | |
parent | e34407b0080fa5c7176522b42783ad3c55a0f722 (diff) |
* getsig: Keep original c-names for haskell types.
* Remove trailing closer from comment parser result.
* Upper bound for base dependency.
-rw-r--r-- | monkeypatch.cabal | 2 | ||||
-rw-r--r-- | monkeypatch.hs | 104 |
2 files changed, 77 insertions, 29 deletions
diff --git a/monkeypatch.cabal b/monkeypatch.cabal index 4c66cb7..21146e4 100644 --- a/monkeypatch.cabal +++ b/monkeypatch.cabal | |||
@@ -54,7 +54,7 @@ executable monkeypatch | |||
54 | main-is: monkeypatch.hs | 54 | main-is: monkeypatch.hs |
55 | -- other-modules: | 55 | -- other-modules: |
56 | -- other-extensions: | 56 | -- other-extensions: |
57 | build-depends: base ^>=4.10.1.0 | 57 | build-depends: base >=4.10.1.0 && <=4.12 |
58 | , containers ^>=0.5.10.2 | 58 | , containers ^>=0.5.10.2 |
59 | , language-c | 59 | , language-c |
60 | , haskell-src-exts | 60 | , haskell-src-exts |
diff --git a/monkeypatch.hs b/monkeypatch.hs index 7c9d75d..ac67afa 100644 --- a/monkeypatch.hs +++ b/monkeypatch.hs | |||
@@ -1,17 +1,18 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | ||
2 | {-# LANGUAGE BangPatterns #-} | 1 | {-# LANGUAGE BangPatterns #-} |
3 | {-# LANGUAGE DeriveFunctor #-} | 2 | {-# LANGUAGE DeriveFunctor #-} |
4 | {-# LANGUAGE FlexibleContexts #-} | 3 | {-# LANGUAGE FlexibleContexts #-} |
5 | {-# LANGUAGE LambdaCase #-} | 4 | {-# LANGUAGE LambdaCase #-} |
6 | {-# LANGUAGE NondecreasingIndentation #-} | 5 | {-# LANGUAGE NondecreasingIndentation #-} |
6 | {-# LANGUAGE OverloadedStrings #-} | ||
7 | {-# LANGUAGE QuasiQuotes #-} | 7 | {-# LANGUAGE QuasiQuotes #-} |
8 | {-# LANGUAGE TemplateHaskell #-} | 8 | {-# LANGUAGE TemplateHaskell #-} |
9 | {-# LANGUAGE TupleSections #-} | ||
9 | module Main where | 10 | module Main where |
10 | 11 | ||
11 | import Control.Arrow (left) | 12 | import Control.Arrow (left,first,second) |
12 | import Data.Generics.Aliases | 13 | import Data.Generics.Aliases |
13 | import Data.Generics.Schemes | 14 | import Data.Generics.Schemes |
14 | -- import Debug.Trace | 15 | import Debug.Trace |
15 | import Control.Monad | 16 | import Control.Monad |
16 | import qualified Data.ByteString.Char8 as B | 17 | import qualified Data.ByteString.Char8 as B |
17 | import Data.Char | 18 | import Data.Char |
@@ -45,7 +46,7 @@ import Text.PrettyPrint (Doc, doubleQuotes, empty, text, vcat, ($$), | |||
45 | (<+>)) | 46 | (<+>)) |
46 | import Text.Show.Pretty | 47 | import Text.Show.Pretty |
47 | 48 | ||
48 | trace _ = id | 49 | -- trace _ = id |
49 | 50 | ||
50 | -- | Pretty print the given tranlation unit, but replace declarations from header files with @#include@ directives. | 51 | -- | Pretty print the given tranlation unit, but replace declarations from header files with @#include@ directives. |
51 | -- | 52 | -- |
@@ -247,16 +248,19 @@ hsTransField :: Show b => | |||
247 | [CDeclarationSpecifier b] -- c structure name | 248 | [CDeclarationSpecifier b] -- c structure name |
248 | -- -> [(Maybe (CDeclarator b), Maybe (CInitializer b), Maybe (CExpression b))] -- c variable declarations | 249 | -- -> [(Maybe (CDeclarator b), Maybe (CInitializer b), Maybe (CExpression b))] -- c variable declarations |
249 | -> [CDeclarator b] -- c variable declarations | 250 | -> [CDeclarator b] -- c variable declarations |
250 | -> [(String{-field name-}, HS.Type () {- haskell type -}) ] | 251 | -> [ ( (String{-field name-}, HS.Type () {- haskell type -}) |
252 | , Maybe String{- c type -})] | ||
251 | hsTransField ctyps vars | 253 | hsTransField ctyps vars |
252 | = do | 254 | = do |
253 | typname <- hsMkName . either (capitalize . identToString) id <$> (hsTypeSpec =<< ctyps) | 255 | (mcname,typname) <- second hsMkName . either ((\s -> (Just s,capitalize s)) . identToString) |
256 | (Nothing,) | ||
257 | <$> (hsTypeSpec =<< ctyps) | ||
254 | trace ("typname="++show typname) $ return () | 258 | trace ("typname="++show typname) $ return () |
255 | -- (var,Nothing,Nothing) <- vars | 259 | -- (var,Nothing,Nothing) <- vars |
256 | var <- vars | 260 | var <- vars |
257 | trace ("var="++show var) $ return () | 261 | trace ("var="++show var) $ return () |
258 | -- CDeclr (Just fident) ptrdeclr Nothing [] _ <- maybeToList var | 262 | -- CDeclr (Just fident) ptrdeclr Nothing [] _ <- maybeToList var |
259 | let CDeclr mfident ptrdeclr Nothing [] _ = var -- TODO: Look into: Irrefutable pattern failed (ctox/toxcore/DHT.c) | 263 | let CDeclr mfident ptrdeclr Nothing ignored_attrs _ = var -- TODO: Look into: Irrefutable pattern failed (ctox/toxcore/DHT.c) |
260 | -- let CDeclr mfident ptrdeclr _ _ _ = var | 264 | -- let CDeclr mfident ptrdeclr _ _ _ = var |
261 | trace ("fident="++show mfident) $ return () | 265 | trace ("fident="++show mfident) $ return () |
262 | trace ("ptrdeclr="++show ptrdeclr) $ return () | 266 | trace ("ptrdeclr="++show ptrdeclr) $ return () |
@@ -265,7 +269,7 @@ hsTransField ctyps vars | |||
265 | [] -> b | 269 | [] -> b |
266 | (CPtrDeclr [] _:cs) -> HS.TyApp () (HS.TyCon () (hsMkName "Ptr")) (grok cs b) | 270 | (CPtrDeclr [] _:cs) -> HS.TyApp () (HS.TyCon () (hsMkName "Ptr")) (grok cs b) |
267 | CFunDeclr (Right (args,flg)) attrs _:p -> | 271 | CFunDeclr (Right (args,flg)) attrs _:p -> |
268 | let (as,ts) = unzip $ concatMap (\(CDecl rs as _) -> hsTransField rs $ concatMap fieldInfo as) args | 272 | let (as,ts) = unzip $ concatMap (\(CDecl rs as _) -> map fst $ hsTransField rs $ concatMap fieldInfo as) args |
269 | b0 = case p of | 273 | b0 = case p of |
270 | CPtrDeclr [] _:cs -> HS.TyApp () (HS.TyCon () (hsMkName "Ptr")) (grok cs b) | 274 | CPtrDeclr [] _:cs -> HS.TyApp () (HS.TyCon () (hsMkName "Ptr")) (grok cs b) |
271 | [] -> b | 275 | [] -> b |
@@ -273,7 +277,7 @@ hsTransField ctyps vars | |||
273 | _ -> HS.TyCon () (hsMkName $ show $ map (fmap (const ())) ptrdeclr) | 277 | _ -> HS.TyCon () (hsMkName $ show $ map (fmap (const ())) ptrdeclr) |
274 | ftyp = grok ptrdeclr btyp | 278 | ftyp = grok ptrdeclr btyp |
275 | fieldName = maybe ("_") identToString mfident | 279 | fieldName = maybe ("_") identToString mfident |
276 | [ ( fieldName, ftyp ) ] | 280 | [ ( ( fieldName, ftyp ), mcname ) ] |
277 | {- | 281 | {- |
278 | transField (CDecl [CTypeSpec (CSUType (CStruct CStructTag mctyp Nothing [] _) _)] vars _) | 282 | transField (CDecl [CTypeSpec (CSUType (CStruct CStructTag mctyp Nothing [] _) _)] vars _) |
279 | | Just typname <- mkName . capitalize . identToString <$> mctyp | 283 | | Just typname <- mkName . capitalize . identToString <$> mctyp |
@@ -302,16 +306,23 @@ hsTransFieldExt :: Show b => | |||
302 | hsTransFieldExt :: Show b => | 306 | hsTransFieldExt :: Show b => |
303 | [CDeclarationSpecifier b] -> [CDeclarator b] -> [Decl ()] | 307 | [CDeclarationSpecifier b] -> [CDeclarator b] -> [Decl ()] |
304 | hsTransFieldExt rs as = concatMap (\(fieldName,ftyp)-> [ HS.TypeSig () [ HS.Ident () fieldName ] ftyp ]) | 308 | hsTransFieldExt rs as = concatMap (\(fieldName,ftyp)-> [ HS.TypeSig () [ HS.Ident () fieldName ] ftyp ]) |
305 | $ hsTransField rs as | 309 | $ map fst $ hsTransField rs as |
306 | 310 | ||
307 | hsTransSig rs as = concatMap (\(fieldName,ftyp)-> [ HS.TypeDecl () (DHead () (HS.Ident () ("Sig_" ++ fieldName))) ftyp ]) | 311 | hsTransSig :: Show b => |
312 | [CDeclarationSpecifier b] -> [CDeclarator b] -> [(Decl (),Maybe String)] | ||
313 | hsTransSig rs as = map (\((fieldName,ftyp),ctyp) -> ( HS.TypeDecl () (DHead () (HS.Ident () ("Sig_" ++ fieldName))) ftyp, ctyp )) | ||
308 | $ hsTransField rs as | 314 | $ hsTransField rs as |
309 | 315 | ||
316 | -- Extract argument types from a haskell function type declaration. | ||
317 | types :: Decl l -> [HS.Type l] | ||
310 | types (HS.TypeDecl _ _ typ) = primtypes typ | 318 | types (HS.TypeDecl _ _ typ) = primtypes typ |
311 | 319 | ||
320 | primtypes :: HS.Type l -> [HS.Type l] | ||
312 | primtypes (HS.TyFun _ a b) = primtypes a ++ primtypes b | 321 | primtypes (HS.TyFun _ a b) = primtypes a ++ primtypes b |
313 | primtypes t = [t] | 322 | primtypes t = [t] |
314 | 323 | ||
324 | -- Haskell type name as string. | ||
325 | tname :: HS.Type () -> String | ||
315 | tname (HS.TyCon () (HS.UnQual () (HS.Ident () str))) = str | 326 | tname (HS.TyCon () (HS.UnQual () (HS.Ident () str))) = str |
316 | tname _ = "_unkonwn" | 327 | tname _ = "_unkonwn" |
317 | 328 | ||
@@ -324,12 +335,15 @@ pointers decls = do | |||
324 | d <- decls | 335 | d <- decls |
325 | maybeToList $ getPtrType d | 336 | maybeToList $ getPtrType d |
326 | 337 | ||
338 | -- If it's a haskell Ptr type, then return the pointed type. | ||
339 | -- Otherwise, no op. | ||
340 | unpointer :: HS.Type l -> HS.Type l | ||
327 | unpointer t = case getPtrType t of | 341 | unpointer t = case getPtrType t of |
328 | Nothing -> t | 342 | Nothing -> t |
329 | Just t' -> t' | 343 | Just t' -> t' |
330 | 344 | ||
331 | -- sig :: Show t => CExternalDeclaration t -> [HS.Decl ()] | 345 | -- sig :: Show t => CExternalDeclaration t -> [HS.Decl ()] |
332 | sig :: CExternalDeclaration NodeInfo -> [Decl ()] | 346 | sig :: CExternalDeclaration NodeInfo -> [HS.Decl ()] |
333 | sig = sigf hsTransFieldExt | 347 | sig = sigf hsTransFieldExt |
334 | 348 | ||
335 | -- • Couldn't match expected type ‘CDerivedDeclarator a -> (Maybe (CDeclarator a), Maybe (CInitializer a), Maybe (CExpression a))’ | 349 | -- • Couldn't match expected type ‘CDerivedDeclarator a -> (Maybe (CDeclarator a), Maybe (CInitializer a), Maybe (CExpression a))’ |
@@ -422,13 +436,21 @@ parseOptions ("-v":args) opts = parseOptions args opts | |||
422 | } | 436 | } |
423 | parseOptions as x = error (show as) | 437 | parseOptions as x = error (show as) |
424 | 438 | ||
439 | tnames d = filter (notKnown . fst) $ map (first $ tname . unpointer) $ concatMap (\(t,c) -> map (,c) (types t)) $ sigf hsTransSig d | ||
440 | |||
441 | |||
442 | getsig :: (a, SymbolInformation [CExternalDeclaration NodeInfo]) | ||
443 | -> [([(String,Maybe String)] -- List of haskell/c type names to define | ||
444 | , ( a | ||
445 | , [Decl ()] -- haskell declaration | ||
446 | , CExternalDeclaration NodeInfo))] -- c declaration (with fixups) | ||
425 | getsig (k,si) = do | 447 | getsig (k,si) = do |
426 | d0 <- take 1 $ symbolSource si | 448 | d0 <- take 1 $ symbolSource si |
427 | let d = case getArgList d0 of | 449 | let d = case getArgList d0 of |
428 | oargs:xs -> let args = fst $ makeParameterNames oargs | 450 | oargs:xs -> let args = fst $ makeParameterNames oargs |
429 | in changeArgList (const $ args:xs) d0 | 451 | in changeArgList (const $ args:xs) d0 |
430 | _ -> d0 | 452 | _ -> d0 |
431 | ts = filter notKnown $ map tname $ map unpointer $ concatMap types $ sigf hsTransSig d | 453 | ts = tnames d |
432 | s = sig d | 454 | s = sig d |
433 | [(ts,(k,s,d))] | 455 | [(ts,(k,s,d))] |
434 | 456 | ||
@@ -452,11 +474,17 @@ makeAcceptableImport (HS.TyFun a (TyCon c (UnQual d (HS.Ident e x))) xs) | |||
452 | = (HS.TyFun a (TyApp c (TyCon c (UnQual d (HS.Ident e "Ptr"))) (TyCon e (UnQual e (HS.Ident e x)))) (makeAcceptableImport xs)) | 474 | = (HS.TyFun a (TyApp c (TyCon c (UnQual d (HS.Ident e "Ptr"))) (TyCon e (UnQual e (HS.Ident e x)))) (makeAcceptableImport xs)) |
453 | makeAcceptableImport t = t | 475 | makeAcceptableImport t = t |
454 | 476 | ||
477 | enumCases (CDeclExt (CDecl xs _ ni)) = do | ||
478 | CTypeSpec (CEnumType (CEnum _ (Just cs))) <- xs | ||
479 | return (ni,cs) | ||
480 | |||
481 | |||
455 | -- c2haskell :: Foldable t => C2HaskellOptions -> p -> t String -> CTranslationUnit NodeInfo -> IO () | 482 | -- c2haskell :: Foldable t => C2HaskellOptions -> p -> t String -> CTranslationUnit NodeInfo -> IO () |
456 | c2haskell :: C2HaskellOptions | 483 | c2haskell :: C2HaskellOptions |
457 | -> p1 -> FilePath -> [String] -> IncludeStack -> CTranslationUnit NodeInfo -> IO () | 484 | -> p1 -> FilePath -> [String] -> IncludeStack -> CTranslationUnit NodeInfo -> IO () |
458 | c2haskell opts cs cmodname missings incs (CTranslUnit edecls _) = do | 485 | c2haskell opts cs cmodname missings incs (CTranslUnit edecls _) = do |
459 | let db = foldr update initTranspile edecls | 486 | let db = foldr update initTranspile edecls |
487 | {- exported symbols in this module -} | ||
460 | es = Map.filter (\d -> symbolLocal d && not (symbolStatic d)) (syms db) | 488 | es = Map.filter (\d -> symbolLocal d && not (symbolStatic d)) (syms db) |
461 | case selectFunction opts of | 489 | case selectFunction opts of |
462 | Nothing -> do | 490 | Nothing -> do |
@@ -475,18 +503,28 @@ c2haskell opts cs cmodname missings incs (CTranslUnit edecls _) = do | |||
475 | hPutStrLn haskmod $ "import Data.Int" | 503 | hPutStrLn haskmod $ "import Data.Int" |
476 | putStrLn $ "-- " ++ show (fmap (fmap (fmap (const ()))) <$> Map.lookup "ip_is_lan" (syms db)) | 504 | putStrLn $ "-- " ++ show (fmap (fmap (fmap (const ()))) <$> Map.lookup "ip_is_lan" (syms db)) |
477 | let sigs = concatMap getsig (Map.toList es) | 505 | let sigs = concatMap getsig (Map.toList es) |
506 | {- referenced haskell type names by missing symbols -} | ||
478 | sigs2 = concatMap (\s -> do | 507 | sigs2 = concatMap (\s -> do |
479 | x <- maybeToList $ Map.lookup s (syms db) | 508 | x <- maybeToList $ Map.lookup s (syms db) |
480 | (y,_) <- getsig (s,x) | 509 | (y,_) <- getsig (s,x) |
481 | y) | 510 | y) |
482 | missings | 511 | missings |
512 | {- referenced haskell type names by all exported symbols -} | ||
483 | ts = concatMap fst sigs | 513 | ts = concatMap fst sigs |
484 | putStrLn $ "-- IP `elem` db = " ++ show (length . symbolSource <$> Map.lookup "IP" (syms db)) | 514 | hPutStrLn haskmod "" |
485 | putStrLn $ "-- IP `elem` sigs2 = " ++ show (elem "IP" sigs2) | 515 | forM_ (uniq $ ts ++ sigs2) $ \(t,ct) -> do |
486 | putStrLn $ "-- ip_is_lan `elem` db = " ++ show (length . symbolSource <$> Map.lookup "ip_is_lan" (syms db)) | 516 | case ct >>= (`Map.lookup` syms db) of |
487 | putStrLn $ "-- ip_is_lan `elem` sigs2 = " ++ show (elem "ip_is_lan" sigs2) | 517 | Just si -> do |
488 | forM_ (uniq $ ts ++ sigs2) $ \t -> do | 518 | let symfile :: Maybe FilePath |
519 | symfile = (listToMaybe (symbolSource si) >>= fileOfNode) | ||
520 | hPutStrLn haskmod $ "-- " ++ show symfile | ||
521 | mapM_ (hPutStrLn haskmod . commented . show . pretty) $ symbolSource si | ||
522 | cs <- maybe (return []) readComments symfile | ||
523 | mapM_ (hPutStrLn haskmod . commented . ppShow) $ cs | ||
524 | mapM_ (hPutStrLn haskmod . commented . ppShow) $ symbolSource si | ||
525 | Nothing -> return () | ||
489 | hPutStrLn haskmod $ "data " ++ t | 526 | hPutStrLn haskmod $ "data " ++ t |
527 | hPutStrLn haskmod "" | ||
490 | forM_ sigs $ \(_,(k,hs,d)) -> do | 528 | forM_ sigs $ \(_,(k,hs,d)) -> do |
491 | forM_ hs $ \hdecl -> do | 529 | forM_ hs $ \hdecl -> do |
492 | {- | 530 | {- |
@@ -634,11 +672,18 @@ getArgList1 (CDeclr a xs b c d) = xs | |||
634 | 672 | ||
635 | getArgList2 ((a,b,c):zs) = getArgList3 a | 673 | getArgList2 ((a,b,c):zs) = getArgList3 a |
636 | 674 | ||
675 | getArgList3 (Just (CDeclr a [CPtrDeclr [] _] b c d)) = [] -- struct prototype, no fields. | ||
637 | getArgList3 (Just (CDeclr a x b c d)) = x | 676 | getArgList3 (Just (CDeclr a x b c d)) = x |
638 | 677 | ||
678 | getArgList_ :: CExternalDeclaration a -> [CDerivedDeclarator a] | ||
679 | getArgList_ (CFDefExt (CFunDef xs ys zs c d)) = getArgList1 ys | ||
680 | getArgList_ (CDeclExt (CDecl xs ys pos)) = getArgList2 ys | ||
681 | |||
639 | getArgList :: CExternalDeclaration a -> [CDerivedDeclarator a] | 682 | getArgList :: CExternalDeclaration a -> [CDerivedDeclarator a] |
640 | getArgList (CFDefExt (CFunDef xs ys zs c d)) = getArgList1 ys | 683 | getArgList x = let v=getArgList_ x in trace ("getArgList ("++show (u x)++") = "++show (fmap u v)) v |
641 | getArgList (CDeclExt (CDecl xs ys pos)) = getArgList2 ys | 684 | where |
685 | u :: Functor f => f a -> f () | ||
686 | u = fmap (const ()) | ||
642 | 687 | ||
643 | changeReturnValue f (CFDefExt (CFunDef xs ys zs c d)) = (CFDefExt (CFunDef (f xs) ys zs c d)) | 688 | changeReturnValue f (CFDefExt (CFunDef xs ys zs c d)) = (CFDefExt (CFunDef (f xs) ys zs c d)) |
644 | changeReturnValue f (CDeclExt (CDecl xs ys pos)) = (CDeclExt (CDecl (f xs) ys pos)) | 689 | changeReturnValue f (CDeclExt (CDecl xs ys pos)) = (CDeclExt (CDecl (f xs) ys pos)) |
@@ -686,6 +731,8 @@ makeParameterNames (CFunDeclr (Right (ps, flg)) z2 z3) = case ps of | |||
686 | mkp num (CDecl rtyp [] n) | 731 | mkp num (CDecl rtyp [] n) |
687 | = (CDecl rtyp ((Just (CDeclr (Just $ mkidn num undefNode) [] Nothing [] n),Nothing,Nothing):[]) n) | 732 | = (CDecl rtyp ((Just (CDeclr (Just $ mkidn num undefNode) [] Nothing [] n),Nothing,Nothing):[]) n) |
688 | mkp num p = p | 733 | mkp num p = p |
734 | -- CPtrDeclr [] () | ||
735 | makeParameterNames x = error $ "makeParameterNames " ++ show (fmap (const ()) x) | ||
689 | 736 | ||
690 | expr :: CDeclaration a -> CExpression a | 737 | expr :: CDeclaration a -> CExpression a |
691 | expr (CDecl rtyp ((Just (CDeclr (Just i) typ x ys z),a,b):xs) n) = CVar i n | 738 | expr (CDecl rtyp ((Just (CDeclr (Just i) typ x ys z),a,b):xs) n) = CVar i n |
@@ -777,9 +824,9 @@ goMissing haskmod db cfun = do | |||
777 | oargs:xs -> let args = fst $ makeParameterNames oargs | 824 | oargs:xs -> let args = fst $ makeParameterNames oargs |
778 | in changeArgList (const $ args:xs) d0 | 825 | in changeArgList (const $ args:xs) d0 |
779 | _ -> d0 | 826 | _ -> d0 |
780 | let ts = filter notKnown $ map tname $ pointers $ concatMap types $ sigf hsTransSig d | 827 | let ts = tnames d |
781 | -- forM_ ts $ \t -> putStrLn $ "data " ++ t | 828 | -- forM_ ts $ \(t,_) -> putStrLn $ "data " ++ t |
782 | forM_ (sigf hsTransSig d) $ \hs -> do | 829 | forM_ (sigf hsTransSig d) $ \(hs,ctypname) -> do |
783 | hPutStrLn haskmod . HS.prettyPrint $ makeAcceptableDecl hs | 830 | hPutStrLn haskmod . HS.prettyPrint $ makeAcceptableDecl hs |
784 | case hs of | 831 | case hs of |
785 | HS.TypeDecl _ (DHead _ (HS.Ident _ signame)) ftyp -> do | 832 | HS.TypeDecl _ (DHead _ (HS.Ident _ signame)) ftyp -> do |
@@ -810,8 +857,7 @@ goMissing haskmod db cfun = do | |||
810 | htyp -> hPutStr haskmod $ commented $ "Unsupported haskell type: " ++ HS.prettyPrint htyp | 857 | htyp -> hPutStr haskmod $ commented $ "Unsupported haskell type: " ++ HS.prettyPrint htyp |
811 | 858 | ||
812 | 859 | ||
813 | readComments :: (Num lin, Num col) => | 860 | readComments :: (Num lin, Num col) => FilePath -> IO [(lin, col, [Char])] |
814 | FilePath -> IO [(lin, col, [Char])] | ||
815 | readComments fname = parseComments 1 1 <$> readFile fname | 861 | readComments fname = parseComments 1 1 <$> readFile fname |
816 | 862 | ||
817 | findCloser :: (Num a4, Num a3, Num a2, Num a1, Eq a1) => | 863 | findCloser :: (Num a4, Num a3, Num a2, Num a1, Eq a1) => |
@@ -829,7 +875,9 @@ mkComment lin no str = (lin,no,str) | |||
829 | parseComments :: (Num col, Num lin) => lin -> col -> [Char] -> [(lin, col, [Char])] | 875 | parseComments :: (Num col, Num lin) => lin -> col -> [Char] -> [(lin, col, [Char])] |
830 | parseComments !lin !col = \case | 876 | parseComments !lin !col = \case |
831 | ('/':'*':cs) -> let (lcnt,col',bcnt) = findCloser 1 (0,col,0) cs | 877 | ('/':'*':cs) -> let (lcnt,col',bcnt) = findCloser 1 (0,col,0) cs |
832 | (xs,cs') = splitAt bcnt cs | 878 | (xs,cs') = case drop (bcnt-2) cs of |
879 | '*':'/':_ -> second (drop 2) $ splitAt (bcnt-2) cs | ||
880 | _ -> splitAt bcnt cs | ||
833 | in mkComment lin col xs : parseComments (lin + lcnt) col' cs' | 881 | in mkComment lin col xs : parseComments (lin + lcnt) col' cs' |
834 | ('/':'/':cs) -> let (comment,ds) = break (=='\n') cs | 882 | ('/':'/':cs) -> let (comment,ds) = break (=='\n') cs |
835 | in mkComment lin col comment : parseComments (lin + 1) 1 cs | 883 | in mkComment lin col comment : parseComments (lin + 1) 1 cs |
@@ -938,7 +986,7 @@ main = do | |||
938 | return (includeStack pre,c) | 986 | return (includeStack pre,c) |
939 | -- putStrLn $ "fname = " ++ fname | 987 | -- putStrLn $ "fname = " ++ fname |
940 | -- putStrLn $ "includes = " ++ ppShow (fmap fst r) | 988 | -- putStrLn $ "includes = " ++ ppShow (fmap fst r) |
941 | cs <- readComments fname | 989 | -- cs <- readComments fname |
942 | case () of | 990 | case () of |
943 | _ | preprocess hopts -- --cpp | 991 | _ | preprocess hopts -- --cpp |
944 | -> do | 992 | -> do |
@@ -953,4 +1001,4 @@ main = do | |||
953 | putStrLn $ ppShow $ everywhere (mkT eraseNodeInfo) . snd <$> r | 1001 | putStrLn $ ppShow $ everywhere (mkT eraseNodeInfo) . snd <$> r |
954 | _ -> do | 1002 | _ -> do |
955 | syms <- linker (cargs ++ reverse fs) fname | 1003 | syms <- linker (cargs ++ reverse fs) fname |
956 | either print (uncurry $ c2haskell hopts cs fname syms) r | 1004 | either print (uncurry $ c2haskell hopts () fname syms) r |