From 1640830bf5dc0630481e698512064215eb3e8249 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sun, 10 Oct 2021 19:55:59 +0200 Subject: WIP --- Language/C/Print.hs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'Language/C') diff --git a/Language/C/Print.hs b/Language/C/Print.hs index 5852601..3d7688c 100644 --- a/Language/C/Print.hs +++ b/Language/C/Print.hs @@ -50,10 +50,14 @@ printType = printString . showType printBits B64 = "64" printBlock :: [Stmt] -> PrintS -printBlock ss = - printString "{\n " - % addIndent 2 (intercalates "\n" (map printStmt ss)) - % printString "\n}" +printBlock ss = printBlock' (intercalates "\n" (map printStmt ss)) + +printBlock' :: PrintS -> PrintS +printBlock' body = + getIndent $ \d -> + printString ("{\n" ++ replicate (d + 2) ' ') + % addIndent 2 body + % printString "\n}" printStmt :: Stmt -> PrintS printStmt (SDecl ty name rhs) = @@ -75,6 +79,13 @@ printStmt (SFor ty name lo hi body) = % printString "; " % printName name % printString "++) " % printBlock body +printStmt (SWhile [] cond body) = + printString "while (" % printExpr cond % printString ") " % printBlock body +printStmt (SWhile pre cond post) = + printString "while (true) " + % printBlock' (intercalates "\n" (map printStmt pre) + % printString "\nif (" % printExpr cond % printString ") break;\n" + % intercalates "\n" (map printStmt post)) printStmt (SIf e b1 b2) = printString "if (" % printExpr e % printString ") " % printBlock b1 % printString " else " % printBlock b2 @@ -93,6 +104,7 @@ printExpr (ECall name args) = printExpr (EIndex name e) = printName name % printString "[" % printExpr e % printString "]" printExpr (EPtrTo e) = printString "&(" % printExpr e % printString ")" printExpr (ESizeOf t) = printString "(sizeof (" % printType t % printString "))" +printExpr (ECast t e) = printString "(" % printType t % printString ")(" % printExpr e % printString ")" addIndent :: Int -> PrintS -> PrintS @@ -101,6 +113,9 @@ addIndent plusd f d = f (d + plusd) withIndent :: Int -> PrintS -> PrintS withIndent d f _ = f d +getIndent :: (Int -> PrintS) -> PrintS +getIndent f d = f d d + intercalates :: String -> [PrintS] -> PrintS intercalates sep l = foldr (%) (\_ -> id) $ intersperse (printString sep) l -- cgit v1.2.3-70-g09d2