summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-03-18 20:20:50 -0400
committerJoe Crayne <joe@jerkface.net>2019-03-18 20:20:50 -0400
commit1541cb7d36aa8aff07ac4a45fa467e131408c8df (patch)
treee0b9083c8da26b5f4517f0786e973db92744db34
parentd7c3ede006e7a767bf5906e908d40caaa2951d4b (diff)
Transpile code to create structs.
-rw-r--r--monkeypatch.hs37
1 files changed, 34 insertions, 3 deletions
diff --git a/monkeypatch.hs b/monkeypatch.hs
index beaa58f..d39ec98 100644
--- a/monkeypatch.hs
+++ b/monkeypatch.hs
@@ -10,6 +10,7 @@
10module Main where 10module Main where
11 11
12import Control.Arrow (left,first,second) 12import Control.Arrow (left,first,second)
13import Data.Either
13import Data.Generics.Aliases 14import Data.Generics.Aliases
14import Data.Generics.Schemes 15import Data.Generics.Schemes
15import Debug.Trace 16import Debug.Trace
@@ -251,6 +252,33 @@ grokExpression (C.CCall (CVar fn _) exps _) = do
251grokExpression _ = Nothing 252grokExpression _ = Nothing
252 253
253 254
255grokInitialization _ (Just (CDeclr (Just cv0) _ _ _ _),CInitExpr exp _) = do
256 let v = identToString cv0
257 (xs,x) <- grokExpression exp
258 let hsexp = fmap (App () (hsvar "return")) x -- Paren () (
259 ret = flip (foldr applyComputation) xs $
260 fmap (\exp -> InfixApp () exp hsopBind
261 $ Lambda () [hspvar v] (hsvar k)) hsexp
262 k = uniqIdentifier "go" (compFree ret)
263 return $ fmap (\exp -> Lambda () [hspvar k] exp) ret
264grokInitialization ts (Just (CDeclr (Just cv0) _ _ _ _),CInitList exps _) = do
265 let v = identToString cv0
266 let k = uniqIdentifier "go" (varmap [v])
267 case lefts $ concatMap hsTypeSpec ts of
268 (ident:_) ->
269 -- TODO: intialize fields.
270 let hident = HS.Ident () $ capitalize $ identToString ident
271 in return Computation
272 { compFree = Map.empty -- todo
273 , compIntro = Map.singleton v ()
274 , comp = Lambda () [hspvar k]
275 $ InfixApp () (App () (hsvar "newStruct") (TypeApp () (TyCon () (UnQual () hident)))) hsopBind
276 $ Lambda () [hspvar v] (hsvar k)
277 }
278 _ -> Nothing
279grokInitialization _ _ = Nothing
280
281
254grokStatement :: CCompoundBlockItem a -> Maybe (Computation (HS.Exp ())) 282grokStatement :: CCompoundBlockItem a -> Maybe (Computation (HS.Exp ()))
255grokStatement (CBlockStmt (CReturn (Just exp) _)) = do 283grokStatement (CBlockStmt (CReturn (Just exp) _)) = do
256 (xs,x) <- grokExpression exp 284 (xs,x) <- grokExpression exp
@@ -280,17 +308,20 @@ grokStatement (CBlockStmt (CIf exp (CCompound [] stmts _) Nothing _)) = do
280 , compIntro = compIntro s 308 , compIntro = compIntro s
281 , comp = Lambda () [hspvar k] $ If () (comp x) (comp s) (hsvar k) 309 , comp = Lambda () [hspvar k] $ If () (comp x) (comp s) (hsvar k)
282 } 310 }
283grokStatement (CBlockDecl (CDecl (t:_) (v:vs) _)) = do 311grokStatement (CBlockDecl (CDecl (t:ts) (v:vs) _)) = do
284 -- case mapMaybe (\(cdeclr,_,_) -> cdeclr >>= \(CDeclr i _ initial _ _) -> initial) (v:vs) of 312 -- case mapMaybe (\(cdeclr,_,_) -> cdeclr >>= \(CDeclr i _ initial _ _) -> initial) (v:vs) of
285 -- case mapMaybe (\(cdeclr,_,_) -> cdeclr >>= \(CInitList xs _) -> Just xs) (v:vs) of 313 -- case mapMaybe (\(cdeclr,_,_) -> cdeclr >>= \(CInitList xs _) -> Just xs) (v:vs) of
286 case mapMaybe (\(_,inits,_) -> inits) (v:vs) of 314 case mapMaybe (\(i,inits,_) -> fmap ((,) i) inits) (v:vs) of
287 [] -> 315 [] ->
288 return Computation 316 return Computation
289 { compFree = Map.empty 317 { compFree = Map.empty
290 , compIntro = Map.empty 318 , compIntro = Map.empty
291 , comp = Lambda () [hspvar "go"] $ hsvar "go" 319 , comp = Lambda () [hspvar "go"] $ hsvar "go"
292 } 320 }
293 initials -> Nothing -- TODO 321 initials -> do
322 gs <- mapM (grokInitialization $ t:ts) initials
323 return $ fmap (\exp -> Lambda () [hspvar "go"] exp)
324 $ foldr applyComputation (Computation Map.empty Map.empty (hsvar "go")) gs
294grokStatement _ = Nothing 325grokStatement _ = Nothing
295 326
296isFunctionDecl (CDeclExt (CDecl _ [(Just (CDeclr _ [CFunDeclr _ _ _] _ _ _),_,_)] _)) = True 327isFunctionDecl (CDeclExt (CDecl _ [(Just (CDeclr _ [CFunDeclr _ _ _] _ _ _),_,_)] _)) = True