From 23fbc6d4afd3787f9e74df149d0235c6dbfd50fd Mon Sep 17 00:00:00 2001 From: Joe Crayne Date: Wed, 21 Nov 2018 13:18:01 -0500 Subject: wip: monkey-patch stub. --- c2haskell.hs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/c2haskell.hs b/c2haskell.hs index 847458c..7a345e6 100644 --- a/c2haskell.hs +++ b/c2haskell.hs @@ -458,7 +458,8 @@ c2haskell opts cs missings (CTranslUnit edecls _) = do putStrLn $ show $ pretty d putStrLn $ show $ pretty $ makeFunctionPointer d putStrLn $ show $ pretty $ makeSetter d - putStrLn $ take 2048 $ ppShow $ everywhere (mkT eraseNodeInfo) <$> makeFunctionPointer d + putStrLn $ show $ pretty $ makeStub d + putStrLn $ ppShow $ everywhere (mkT eraseNodeInfo) d -- <$> makeFunctionPointer d -- TODO: make idempotent makeStatic :: [CDeclarationSpecifier NodeInfo] -> [CDeclarationSpecifier NodeInfo] @@ -511,6 +512,15 @@ changeArgList f (CFDefExt (CFunDef xs ys zs c d)) = CFDefExt (CFunDef xs (change changeArgList f (CDeclExt (CDecl xs ys pos)) = (CDeclExt (CDecl xs (changeArgList2 f ys) pos)) +getArgList1 (CDeclr a xs b c d) = xs + +getArgList2 ((a,b,c):zs) = getArgList3 a + +getArgList3 (Just (CDeclr a x b c d)) = x + +getArgList (CFDefExt (CFunDef xs ys zs c d)) = getArgList1 ys +getArgList (CDeclExt (CDecl xs ys pos)) = getArgList2 ys + changeReturnValue f (CFDefExt (CFunDef xs ys zs c d)) = (CFDefExt (CFunDef (f xs) ys zs c d)) changeReturnValue f (CDeclExt (CDecl xs ys pos)) = (CDeclExt (CDecl (f xs) ys pos)) @@ -522,21 +532,30 @@ setSetterBody name (CDeclExt (CDecl xs ys pos)) = (CFDefExt (CFunDef xs v [] (se (Just y,_,_):_ -> y _ -> CDeclr Nothing [] Nothing [] pos +makeStub d@(CDeclExt (CDecl xs ys pos)) = + let oargs:xs = getArgList d + (args,vs) = makeParameterNames oargs + in changeArgList (const $ args:xs) d + parameterIdent :: CDeclaration a -> Maybe Ident parameterIdent (CDecl _ xs n) = listToMaybe $ do (Just (CDeclr (Just x) _ _ _ _),_,_) <- xs return x makeParameterNames :: CDerivedDeclarator NodeInfo -> (CDerivedDeclarator NodeInfo,[CExpression NodeInfo]) -makeParameterNames (CFunDeclr (Right (CDecl rtyp ps n : ds, flg)) z2 z3) - = ( CFunDeclr (Right (CDecl rtyp qs n : ds, flg)) z2 z3 , [] ) - -- FIXME: Each paramter gets its own CDecl. +makeParameterNames (CFunDeclr (Right (ps, flg)) z2 z3) + = ( CFunDeclr (Right (qs, flg)) z2 z3 , map expr qs ) where + -- TODO: ensure uniqueness of generated parameter names qs = zipWith mkp [0..] ps - mkp num (Just (CDeclr Nothing typ x ys z),a,b) = - (Just (CDeclr (Just $ mkidn num n) typ x ys z),a,b) + mkp num (CDecl rtyp ((Just (CDeclr Nothing typ x ys z),a,b):xs) n) + = (CDecl rtyp ((Just (CDeclr (Just $ mkidn num n) typ x ys z),a,b):xs) n) + mkp num (CDecl rtyp [] n) + = (CDecl rtyp ((Just (CDeclr (Just $ mkidn num n) [] Nothing [] n),Nothing,Nothing):[]) n) mkp num p = p +expr :: CDeclaration a -> CExpression a +expr (CDecl rtyp ((Just (CDeclr (Just i) typ x ys z),a,b):xs) n) = CVar i n mkidn :: Show a => a -> NodeInfo -> Ident mkidn num n = C.Ident ("a"++show num) 0 n -- cgit v1.2.3