summaryrefslogtreecommitdiff
path: root/Language/C/Print.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Language/C/Print.hs')
-rw-r--r--Language/C/Print.hs23
1 files changed, 19 insertions, 4 deletions
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