diff options
author | Joe Crayne <joe@jerkface.net> | 2018-11-21 00:26:41 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2018-11-21 00:26:41 -0500 |
commit | 1de5441565058259078b7d52555d551184a6e441 (patch) | |
tree | d09447873bcccad0b65f8c302401233d99feb544 | |
parent | b34ca8a92df409006b8572b629e36bf157cf5584 (diff) |
monkey-patch setter finished.
-rw-r--r-- | c2haskell.hs | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/c2haskell.hs b/c2haskell.hs index dbdde9c..d692558 100644 --- a/c2haskell.hs +++ b/c2haskell.hs | |||
@@ -497,7 +497,9 @@ changeName f d = d | |||
497 | makeAcceptableDecl (HS.TypeDecl a (DHead b (HS.Ident c signame)) ftyp) | 497 | makeAcceptableDecl (HS.TypeDecl a (DHead b (HS.Ident c signame)) ftyp) |
498 | = (HS.TypeDecl a (DHead b (HS.Ident c signame)) (makeFunctionUseIO $ makeAcceptableImport ftyp)) | 498 | = (HS.TypeDecl a (DHead b (HS.Ident c signame)) (makeFunctionUseIO $ makeAcceptableImport ftyp)) |
499 | 499 | ||
500 | makeSetter d@(CDeclExt (CDecl xs ys pos)) = changeReturnValue (const voidReturnType) $ changeArgList (const voidp) $ changeName ("setf_"++) d | 500 | makeSetter d@(CDeclExt (CDecl xs ys pos)) = |
501 | let name = concatMap identToString $ take 1 $ catMaybes $ sym d | ||
502 | in setSetterBody ("f_"++name) $ changeReturnValue (const voidReturnType) $ changeArgList (const voidp) $ changeName ("setf_"++) d | ||
501 | 503 | ||
502 | changeArgList1 f (CDeclr a xs b c d) = CDeclr a (f xs) b c d | 504 | changeArgList1 f (CDeclr a xs b c d) = CDeclr a (f xs) b c d |
503 | 505 | ||
@@ -514,6 +516,11 @@ changeReturnValue f (CDeclExt (CDecl xs ys pos)) = (CDeclExt (CDecl (f xs) ys po | |||
514 | 516 | ||
515 | voidReturnType = [ CTypeSpec (CVoidType undefNode) ] | 517 | voidReturnType = [ CTypeSpec (CVoidType undefNode) ] |
516 | 518 | ||
519 | setSetterBody name (CFDefExt (CFunDef xs ys zs c d)) = (CFDefExt (CFunDef xs ys zs (setterBody name) d)) | ||
520 | setSetterBody name (CDeclExt (CDecl xs ys pos)) = (CFDefExt (CFunDef xs v [] (setterBody name) pos)) | ||
521 | where v = case ys of | ||
522 | (Just y,_,_):_ -> y | ||
523 | _ -> CDeclr Nothing [] Nothing [] pos | ||
517 | 524 | ||
518 | voidp :: [CDerivedDeclarator NodeInfo] | 525 | voidp :: [CDerivedDeclarator NodeInfo] |
519 | voidp = [ CFunDeclr | 526 | voidp = [ CFunDeclr |
@@ -538,6 +545,22 @@ voidp = [ CFunDeclr | |||
538 | where n = undefNode | 545 | where n = undefNode |
539 | 546 | ||
540 | 547 | ||
548 | setterBody :: String -> CStatement NodeInfo | ||
549 | setterBody name = | ||
550 | CCompound [] | ||
551 | [ CBlockStmt | ||
552 | (CExpr | ||
553 | (Just | ||
554 | (CAssign | ||
555 | CAssignOp | ||
556 | (CVar (C.Ident name 0 undefNode) undefNode) | ||
557 | (CVar (C.Ident "p" 0 undefNode) undefNode) | ||
558 | undefNode)) | ||
559 | undefNode) | ||
560 | ] | ||
561 | undefNode | ||
562 | |||
563 | |||
541 | goMissing db cfun = do | 564 | goMissing db cfun = do |
542 | forM_ (Map.lookup cfun $ syms db) $ \si -> do | 565 | forM_ (Map.lookup cfun $ syms db) $ \si -> do |
543 | forM_ (take 1 $ symbolSource si) $ \d -> do | 566 | forM_ (take 1 $ symbolSource si) $ \d -> do |
@@ -579,6 +602,8 @@ goMissing db cfun = do | |||
579 | 602 | ||
580 | readComments fname = parseComments 1 1 <$> readFile fname | 603 | readComments fname = parseComments 1 1 <$> readFile fname |
581 | 604 | ||
605 | findCloser :: (Num a4, Num a3, Num a2, Num a1, Eq a1) => | ||
606 | a1 -> (a4, a2, a3) -> [Char] -> (a4, a2, a3) | ||
582 | findCloser !1 (l,c,b) ('*':'/':_) = (l,c+2,b+2) | 607 | findCloser !1 (l,c,b) ('*':'/':_) = (l,c+2,b+2) |
583 | findCloser !d (l,c,b) ('*':'/':xs) = findCloser (d - 1) (l,c+2,b+2) xs | 608 | findCloser !d (l,c,b) ('*':'/':xs) = findCloser (d - 1) (l,c+2,b+2) xs |
584 | findCloser !d (l,c,b) ('/':'*':xs) = findCloser (d + 1) (l,c+2,b+2) xs | 609 | findCloser !d (l,c,b) ('/':'*':xs) = findCloser (d + 1) (l,c+2,b+2) xs |
@@ -586,6 +611,7 @@ findCloser !d (l,c,b) ('\n':xs) = findCloser d (l+1,1,b+1) xs | |||
586 | findCloser !d (l,c,b) (_:xs) = findCloser d (l,c+1,b+1) xs | 611 | findCloser !d (l,c,b) (_:xs) = findCloser d (l,c+1,b+1) xs |
587 | findCloser !d (l,c,b) [] = (l,c,b) | 612 | findCloser !d (l,c,b) [] = (l,c,b) |
588 | 613 | ||
614 | mkComment :: a -> b -> c -> (a, b, c) | ||
589 | mkComment lin no str = (lin,no,str) | 615 | mkComment lin no str = (lin,no,str) |
590 | 616 | ||
591 | parseComments :: (Num col, Num lin) => lin -> col -> [Char] -> [(lin, col, [Char])] | 617 | parseComments :: (Num col, Num lin) => lin -> col -> [Char] -> [(lin, col, [Char])] |
@@ -621,11 +647,13 @@ usage args = do | |||
621 | (<&>) :: Functor f => f a -> (a -> b) -> f b | 647 | (<&>) :: Functor f => f a -> (a -> b) -> f b |
622 | m <&> f = fmap f m | 648 | m <&> f = fmap f m |
623 | 649 | ||
650 | uniq :: (Ord k, Foldable t) => t k -> [k] | ||
624 | uniq xs = Map.keys $ foldr (\x m -> Map.insert x () m) Map.empty xs | 651 | uniq xs = Map.keys $ foldr (\x m -> Map.insert x () m) Map.empty xs |
625 | 652 | ||
626 | unquote :: String -> String | 653 | unquote :: String -> String |
627 | unquote xs = zipWith const (drop 1 xs) (drop 2 xs) | 654 | unquote xs = zipWith const (drop 1 xs) (drop 2 xs) |
628 | 655 | ||
656 | missingSymbols :: String -> [String] | ||
629 | missingSymbols s = uniq $ do | 657 | missingSymbols s = uniq $ do |
630 | e <- lines s | 658 | e <- lines s |
631 | let (_,us) = break (=="undefined") $ words e | 659 | let (_,us) = break (=="undefined") $ words e |
@@ -637,6 +665,7 @@ missingSymbols s = uniq $ do | |||
637 | return $ unquote q | 665 | return $ unquote q |
638 | 666 | ||
639 | 667 | ||
668 | linker :: [String] -> String -> IO [String] | ||
640 | linker cargs fname = do | 669 | linker cargs fname = do |
641 | (hin,hout,Just herr,hproc) <- createProcess (proc "gcc" $ cargs ++ [fname]) | 670 | (hin,hout,Just herr,hproc) <- createProcess (proc "gcc" $ cargs ++ [fname]) |
642 | { std_err = CreatePipe } | 671 | { std_err = CreatePipe } |