summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-03-22 03:43:17 -0400
committerJoe Crayne <joe@jerkface.net>2019-03-22 03:43:17 -0400
commitb54c31e0cdd832859c14b0c307df3cc39baa2511 (patch)
tree9f43258f10a3d1a3ef19e60555fab6619233b75a
parent99dfcb5d8d426c81488da9ae2c29da8a0e92733f (diff)
Let 0 and 1 translate to False and True when appropriate.
-rw-r--r--monkeypatch.hs16
1 files changed, 15 insertions, 1 deletions
diff --git a/monkeypatch.hs b/monkeypatch.hs
index f12552e..95cf709 100644
--- a/monkeypatch.hs
+++ b/monkeypatch.hs
@@ -376,12 +376,21 @@ grokInitialization fe ts (Just (CDeclr (Just cv0) _ _ _ _),CInitList exps _) = d
376 _ -> Nothing 376 _ -> Nothing
377grokInitialization _ _ _ = Nothing 377grokInitialization _ _ _ = Nothing
378 378
379hasBool :: HS.Type () -> Bool
380hasBool = (1 <=) . gcount (mkQ False (\t -> case t of { HS.Ident () "Bool" -> True; _ -> False }))
381
382promote :: Map String (HS.Type ()) -> HS.Exp () -> HS.Exp ()
383promote fe y@(Lit () (Int () n _)) | (n==0 || n==1) && hasBool (fe Map.! "") =
384 HS.Con () $ UnQual () $ HS.Ident () $ case n of
385 0 -> "False"
386 1 -> "True"
387promote _ y = y
379 388
380grokStatement :: FunctionEnvironment -> CCompoundBlockItem a -> Maybe (Computation (HS.Exp ())) 389grokStatement :: FunctionEnvironment -> CCompoundBlockItem a -> Maybe (Computation (HS.Exp ()))
381grokStatement fe (CBlockStmt (CReturn (Just exp) _)) = do 390grokStatement fe (CBlockStmt (CReturn (Just exp) _)) = do
382 (xs,x) <- grokExpression fe exp 391 (xs,x) <- grokExpression fe exp
383 let k = uniqIdentifier "go" (compFree x `Map.union` compIntro x) 392 let k = uniqIdentifier "go" (compFree x `Map.union` compIntro x)
384 x' = fmap (\y -> App () (hsvar "return") y) x 393 x' = fmap (\y -> App () (hsvar "return") $ promote (fnArgs fe) y) x
385 return $ fmap (\y -> Lambda () [hspvar k] y) $ foldr applyComputation x' xs 394 return $ fmap (\y -> Lambda () [hspvar k] y) $ foldr applyComputation x' xs
386grokStatement fe (CBlockStmt (CExpr (Just (C.CCall (CVar (C.Ident "__assert_fail" _ _) _) xs _)) _)) = do 395grokStatement fe (CBlockStmt (CExpr (Just (C.CCall (CVar (C.Ident "__assert_fail" _ _) _) xs _)) _)) = do
387 x <- case xs of 396 x <- case xs of
@@ -527,20 +536,25 @@ getSymbolExtent sym bdy =
527 stop = maximumBy (comparing posRow) allposss 536 stop = maximumBy (comparing posRow) allposss
528 in SymbolExtent start stop 537 in SymbolExtent start stop
529 538
539lastRowOf :: CNode a => a -> Int
530lastRowOf x = case getLastTokenPos $ nodeInfo x of 540lastRowOf x = case getLastTokenPos $ nodeInfo x of
531 (p,len) | isSourcePos p -> posRow p + len 541 (p,len) | isSourcePos p -> posRow p + len
532 _ -> maxBound 542 _ -> maxBound
533 543
544firstRowOf :: CNode a => a -> Int
534firstRowOf x = case posOfNode $ nodeInfo x of 545firstRowOf x = case posOfNode $ nodeInfo x of
535 p | isSourcePos p -> posRow p 546 p | isSourcePos p -> posRow p
536 _ -> minBound 547 _ -> minBound
537 548
549columnOf :: CNode a => a -> Int
538columnOf x = case posOfNode $ nodeInfo x of 550columnOf x = case posOfNode $ nodeInfo x of
539 p | isSourcePos p -> posColumn p 551 p | isSourcePos p -> posColumn p
540 _ -> minBound 552 _ -> minBound
541 553
554comesBefore :: CNode a => a -> StyledComment -> Bool
542comesBefore x c = lastRowOf x < commentRow c 555comesBefore x c = lastRowOf x < commentRow c
543 556
557comesAfter :: CNode a => a -> StyledComment -> Bool
544comesAfter x c = firstRowOf x > commentRow c 558comesAfter x c = firstRowOf x > commentRow c
545 559
546insertComment :: Data t => StyledComment -> t -> t 560insertComment :: Data t => StyledComment -> t -> t