diff options
author | Joe Crayne <joe@jerkface.net> | 2019-03-22 03:43:17 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-03-22 03:43:17 -0400 |
commit | b54c31e0cdd832859c14b0c307df3cc39baa2511 (patch) | |
tree | 9f43258f10a3d1a3ef19e60555fab6619233b75a | |
parent | 99dfcb5d8d426c81488da9ae2c29da8a0e92733f (diff) |
Let 0 and 1 translate to False and True when appropriate.
-rw-r--r-- | monkeypatch.hs | 16 |
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 |
377 | grokInitialization _ _ _ = Nothing | 377 | grokInitialization _ _ _ = Nothing |
378 | 378 | ||
379 | hasBool :: HS.Type () -> Bool | ||
380 | hasBool = (1 <=) . gcount (mkQ False (\t -> case t of { HS.Ident () "Bool" -> True; _ -> False })) | ||
381 | |||
382 | promote :: Map String (HS.Type ()) -> HS.Exp () -> HS.Exp () | ||
383 | promote 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" | ||
387 | promote _ y = y | ||
379 | 388 | ||
380 | grokStatement :: FunctionEnvironment -> CCompoundBlockItem a -> Maybe (Computation (HS.Exp ())) | 389 | grokStatement :: FunctionEnvironment -> CCompoundBlockItem a -> Maybe (Computation (HS.Exp ())) |
381 | grokStatement fe (CBlockStmt (CReturn (Just exp) _)) = do | 390 | grokStatement 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 |
386 | grokStatement fe (CBlockStmt (CExpr (Just (C.CCall (CVar (C.Ident "__assert_fail" _ _) _) xs _)) _)) = do | 395 | grokStatement 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 | ||
539 | lastRowOf :: CNode a => a -> Int | ||
530 | lastRowOf x = case getLastTokenPos $ nodeInfo x of | 540 | lastRowOf 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 | ||
544 | firstRowOf :: CNode a => a -> Int | ||
534 | firstRowOf x = case posOfNode $ nodeInfo x of | 545 | firstRowOf x = case posOfNode $ nodeInfo x of |
535 | p | isSourcePos p -> posRow p | 546 | p | isSourcePos p -> posRow p |
536 | _ -> minBound | 547 | _ -> minBound |
537 | 548 | ||
549 | columnOf :: CNode a => a -> Int | ||
538 | columnOf x = case posOfNode $ nodeInfo x of | 550 | columnOf x = case posOfNode $ nodeInfo x of |
539 | p | isSourcePos p -> posColumn p | 551 | p | isSourcePos p -> posColumn p |
540 | _ -> minBound | 552 | _ -> minBound |
541 | 553 | ||
554 | comesBefore :: CNode a => a -> StyledComment -> Bool | ||
542 | comesBefore x c = lastRowOf x < commentRow c | 555 | comesBefore x c = lastRowOf x < commentRow c |
543 | 556 | ||
557 | comesAfter :: CNode a => a -> StyledComment -> Bool | ||
544 | comesAfter x c = firstRowOf x > commentRow c | 558 | comesAfter x c = firstRowOf x > commentRow c |
545 | 559 | ||
546 | insertComment :: Data t => StyledComment -> t -> t | 560 | insertComment :: Data t => StyledComment -> t -> t |