1-- | Pretty printer for the AST
2module CodeGen.Cpp.Printer
5 , prettyPrintCppWithSourceMaps
13import Control.Arrow ((<+>))
14import Control.Monad (forM, mzero)
15import Control.Monad.State (StateT, evalStateT)
16import Control.PatternArrows
17import Data.Char (isAscii)
18import Data.List ((\\))
19import qualified Control.Arrow as A
21import Data.Maybe (fromMaybe)
22import Data.Monoid ((<>))
23import Data.Text (Text)
24import qualified Data.Text as T
26import Language.PureScript.AST (SourceSpan(..))
27import Language.PureScript.CoreImp.AST
28import Language.PureScript.Comments
29import Language.PureScript.Crash
30import Language.PureScript.Names (Ident, runIdent)
31import Language.PureScript.Pretty.Common
32import Language.PureScript.PSString (PSString, decodeString, mkString)
33import CodeGen.Cpp.Common
35import qualified Language.PureScript.Constants as C
37-- TODO (Christoph): Get rid of T.unpack / pack
39literals :: (Emit gen) => Pattern PrinterState AST gen
40literals = mkPattern' match'
42 match' :: (Emit gen) => AST -> StateT PrinterState Maybe gen
43 match' cpp = (addMapping' (getSourceSpan cpp) <>) <$> match cpp
45 match :: (Emit gen) => AST -> StateT PrinterState Maybe gen
46 match (NumericLiteral _ n) = return $ emit $ T.pack $ either show show n
47 match (StringLiteral _ s) = return $ emit $ stringLiteral s
48 match (BooleanLiteral _ True) = return $ emit "true"
49 match (BooleanLiteral _ False) = return $ emit "false"
50 match (ArrayLiteral _ xs) = mconcat <$> sequence
51 [ return . emit $ arrayType <> "{"
52 , intercalate (emit ", ") <$> forM xs prettyPrintCpp'
55 -- match (ObjectLiteral _ []) = return $ emit "std::initializer_list<std::pair<const string, boxed>>{}"
56 match (ObjectLiteral _ ps) = mconcat <$> sequence
57 [ return . emit $ dictType <> "{\n"
59 cpps <- forM ps $ \(key, value) -> do
60 value' <- prettyPrintCpp' value
61 return $ emit "{ " <> objectPropertyToString key <> value' <> emit " }"
62 indentString <- currentIndent
63 return $ intercalate (emit ",\n") $ map (indentString <>) cpps
69 objectPropertyToString :: (Emit gen) => PSString -> gen
70 objectPropertyToString s = emit $ stringLiteral s <> ", "
71 match (Block _ sts) = mconcat <$> sequence
73 , withIndent $ prettyStatements sts
78 match (Var _ ident) = return $ emit ident
79 match (VariableIntroduction _ ident value) = mconcat <$> sequence
80 [ return $ emit $ "boxed " <> ident
81 , maybe (return mempty) (fmap (emit " = " <>) . prettyPrintCpp') value
83 match (Assignment _ target@(Var _ var) value@(Function _ Nothing args body)) = mconcat <$> sequence
84 [ prettyPrintCpp' target
86 , return $ emit "[=, &"
87 , prettyPrintCpp' target
90 , return $ emit $ intercalate ", " (("const boxed& " <>) <$> args)
91 , return $ emit ") -> boxed "
92 , prettyPrintCpp' body
94 match (Assignment _ target value) = mconcat <$> sequence
95 [ prettyPrintCpp' target
97 , prettyPrintCpp' value
99 match (App _ val []) = mconcat <$> sequence
100 [ prettyPrintCpp' val
103 match (App ss (App _ (App _ (Indexer _ (Var _ fn) (Var _ fnMod)) [Indexer _ (Var _ dict) (Var _ dictMod)]) [x]) [y])
105 , dictMod == C.dataEq || dictMod == C.dataSemiring || dictMod == C.dataRing || dictMod == C.dataEuclideanRing
106 , Just op <- renderOp fn
107 , Just t <- unboxType dict
108 = mconcat <$> sequence
109 [ return $ emit $ unbox t x
111 , return $ emit $ unbox t y
113 match (App _ (App _ (Indexer _ (Var _ fn) (Var _ fnMod)) [Indexer _ (Var _ dict) (Var _ dictMod)]) [n])
115 , dictMod == C.dataRing
117 , Just t <- unboxType dict
118 = mconcat <$> sequence
120 , return $ emit $ unbox t n
122 match (App _ val args) = mconcat <$> sequence
123 [ prettyPrintCpp' val
125 , intercalate (emit ", ") <$> forM args prettyPrintCpp'
128 match (Function _ (Just name) [] ret@(Block _ [Return _ (Function {})])) = mconcat <$> sequence
129 [ return $ emit "auto "
131 , return $ emit "() -> boxed "
132 , prettyPrintCpp' ret
134 match (Function _ (Just name) [] (Block _ [Return _ ret])) = mconcat <$> sequence
135 [ return $ emit "auto "
137 , return $ emit "() -> boxed "
138 , return $ emit "{\n"
140 indentString <- currentIndent
141 return $ indentString <> emit dispatchOnceBegin
144 , return $ emit ";\n"
146 indentString <- currentIndent
147 return $ indentString <> emit dispatchOnceEnd
148 , return $ emit "\n}"
150 -- match (Function _ (Just name) [] ret) = mconcat <$> sequence
151 match (Function _ (Just name) [] (Block ss sts)) | Return _ ret <- last sts = mconcat <$> sequence
152 [ return $ emit "auto "
154 , return $ emit "() -> boxed {\n"
156 prettyStatements $ init sts
159 indentString <- currentIndent
160 return $ indentString <> emit dispatchOnceBegin
163 , return $ emit ";\n"
165 indentString <- currentIndent
166 return $ indentString <> emit dispatchOnceEnd
167 , return $ emit "\n}"
169 match (Function _ name args ret) = mconcat <$> sequence
170 [ return $ emit $ if name == (Just tcoLoop) then "[&]" else "[=]"
172 , return $ emit $ intercalate ", " (("const boxed& " <>) <$> args)
173 , return $ emit ") -> boxed "
174 , prettyPrintCpp' ret
176 match (Indexer _ prop@(Var _ name) val) = mconcat <$> sequence
177 [ prettyPrintCpp' val
179 , prettyPrintCpp' prop
182 match (Indexer _ (ArrayLiteral _ [name]) val) = mconcat <$> sequence
183 [ prettyPrintCpp' val
185 , prettyPrintCpp' name
188 match (Indexer _ (NumericLiteral _ (Left index)) val) = mconcat <$> sequence
189 [ prettyPrintCpp' val
191 , return . emit . T.pack $ show index
194 match (Indexer _ prop val) = mconcat <$> sequence
195 [ prettyPrintCpp' val
197 , prettyPrintCpp' prop
200 match (InstanceOf _ val ty) = mconcat <$> sequence
201 [ return . emit $ unbox dictType val
202 , return $ emit ".count("
204 , return $ emit ") == 1"
206 match (While _ cond sts) = mconcat <$> sequence
207 [ return $ emit "while ("
208 , prettyPrintCpp' cond
210 , prettyPrintCpp' sts
212 match (For _ ident start end sts) = mconcat <$> sequence
213 [ return $ emit $ "for (boxed " <> ident <> " = "
214 , prettyPrintCpp' start
215 , return $ emit $ "; " <> ident <> " < "
216 , prettyPrintCpp' end
217 , return $ emit $ "; " <> ident <> "++) "
218 , prettyPrintCpp' sts
220 match (ForIn _ ident obj sts) = mconcat <$> sequence
221 [ return $ emit $ "for (boxed " <> ident <> " in "
222 , prettyPrintCpp' obj
224 , prettyPrintCpp' sts
226 match (IfElse _ (Binary _ EqualTo cond (BooleanLiteral Nothing True)) thens elses) = mconcat <$> sequence
227 [ return $ emit "if ("
228 , return $ emit $ unbox "bool" cond
230 , prettyPrintCpp' thens
231 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintCpp') elses
233 match (IfElse _ (Binary _ EqualTo cond (BooleanLiteral Nothing False)) thens elses) = mconcat <$> sequence
234 [ return $ emit "if (!("
235 , return $ emit $ unbox "bool" cond
236 , return $ emit ")) "
237 , prettyPrintCpp' thens
238 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintCpp') elses
240 match (IfElse _ (Binary _ EqualTo x y@(NumericLiteral _ n)) thens elses) = mconcat <$> sequence
241 [ return $ emit "if ("
242 , return $ emit $ unbox t x
243 , return $ emit $ " == "
246 , prettyPrintCpp' thens
247 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintCpp') elses
250 t | Left _ <- n = int
251 | Right _ <- n = float
252 match (IfElse _ (Binary _ EqualTo a b@StringLiteral{}) thens elses) = mconcat <$> sequence
253 [ return $ emit "if ("
254 , return $ emit $ unbox string a
255 , return $ emit $ " == "
258 , prettyPrintCpp' thens
259 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintCpp') elses
261 match (IfElse _ cond thens elses) = mconcat <$> sequence
262 [ return $ emit "if ("
263 , prettyPrintCpp' cond
265 , prettyPrintCpp' thens
266 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintCpp') elses
268 match (Return _ value) = mconcat <$> sequence
269 [ return $ emit "return "
270 , prettyPrintCpp' value
272 match (ReturnNoResult _) = return $ emit "return nullptr"
273 -- match (Throw _ _) = return mempty
274 match (Throw _ value) = mconcat <$> sequence
275 [ return $ emit "throw "
276 , return $ emit "std::runtime_error(\"PatternMatchFailure: \""
277 , prettyPrintCpp' value
280 match (Comment _ com cpp) = mconcat <$> sequence
282 , mconcat <$> forM com comment
283 , prettyPrintCpp' cpp
287 comment :: (Emit gen) => Comment -> StateT PrinterState Maybe gen
288 comment (LineComment com) = fmap mconcat $ sequence $
290 , return $ emit "//" <> emit com <> emit "\n"
292 comment (BlockComment com) = fmap mconcat $ sequence $
294 , return $ emit "/**\n"
296 map asLine (T.lines com) ++
298 , return $ emit " */\n"
302 asLine :: (Emit gen) => Text -> StateT PrinterState Maybe gen
305 return $ i <> emit " * " <> (emit . removeComments) s <> emit "\n"
307 removeComments :: Text -> Text
309 case T.stripPrefix "*/" t of
310 Just rest -> removeComments rest
311 Nothing -> case T.uncons t of
312 Just (x, xs) -> x `T.cons` removeComments xs
315unary' :: (Emit gen) => UnaryOperator -> (AST -> Text) -> Operator PrinterState AST gen
316unary' op mkStr = Wrap match (const)
318 match :: (Emit gen) => Pattern PrinterState AST (gen, AST)
319 match = mkPattern match'
321 match' (Unary _ op' val) | op' == op = Just (emit $ mkStr val, val)
324unary :: (Emit gen) => UnaryOperator -> Text -> Operator PrinterState AST gen
325unary op str = unary' op (\v -> str <> "(" <> prettyPrintCpp1 v <> ")")
327negateOperator :: (Emit gen) => Operator PrinterState AST gen
328negateOperator = unary' Negate (\v -> if isNegate v then "- " else "-")
330 isNegate (Unary _ Negate _) = True
333binary :: (Emit gen) => BinaryOperator -> Text -> Operator PrinterState AST gen
334binary op str = AssocL match (\v1 v2 -> v1 <> emit (" " <> str <> " ") <> v2)
336 match :: Pattern PrinterState AST (AST, AST)
337 match = mkPattern match'
339 match' (Binary _ op' v1 v2) | op' == op = Just (v1, v2)
342prettyStatements :: (Emit gen) => [AST] -> StateT PrinterState Maybe gen
343prettyStatements sts = do
344 cpps <- forM sts prettyPrintCpp'
345 indentString <- currentIndent
346 return $ intercalate (emit "\n") $ map ((<> emit ";") . (indentString <>)) cpps
348-- | Generate a pretty-printed string representing a collection of Objective-C expressions at the same indentation level
349prettyPrintCppWithSourceMaps :: [AST] -> (Text, [SMap])
350prettyPrintCppWithSourceMaps cpp =
351 let StrPos (_, s, mp) = (fromMaybe (internalError "Incomplete pattern") . flip evalStateT (PrinterState 0) . prettyStatements) cpp
354prettyPrintCpp :: [AST] -> Text
355prettyPrintCpp = maybe (internalError "Incomplete pattern") runPlainString . flip evalStateT (PrinterState 0) . prettyStatements
357-- | Generate an indented, pretty-printed string representing a Objective-C expression
358prettyPrintCpp' :: (Emit gen) => AST -> StateT PrinterState Maybe gen
359prettyPrintCpp' = A.runKleisli $ runPattern matchValue
361 matchValue :: (Emit gen) => Pattern PrinterState AST gen
362 matchValue = buildPrettyPrinter operators (literals <+> fmap parensPos matchValue)
363 operators :: (Emit gen) => OperatorTable PrinterState AST gen
365 OperatorTable [ [ unary New "box" ]
367 , unary BitwiseNot "~"
370 , [ binary Multiply "*"
372 , binary Modulus "%" ]
374 , binary Subtract "-" ]
375 , [ binary ShiftLeft "<<"
376 , binary ShiftRight ">>"
377 , binary ZeroFillShiftRight ">>>" ]
378 , [ binary LessThan "<"
379 , binary LessThanOrEqualTo "<="
380 , binary GreaterThan ">"
381 , binary GreaterThanOrEqualTo ">=" ]
382 , [ binary EqualTo "=="
383 , binary NotEqualTo "!=" ]
384 , [ binary BitwiseAnd "&" ]
385 , [ binary BitwiseXor "^" ]
386 , [ binary BitwiseOr "|" ]
387 , [ binary And "&&" ]
391prettyPrintCpp1 :: AST -> Text
392prettyPrintCpp1 = maybe (internalError "Incomplete pattern") runPlainString . flip evalStateT (PrinterState 0) . prettyPrintCpp'
394stringLiteral :: PSString -> Text
395stringLiteral pss | Just s <- decodeString pss =
398 else ("u8")) <> stringLiteral' s
400 stringLiteral' :: Text -> Text
401 stringLiteral' s = "\"" <> T.concatMap encodeChar s <> "\""
402 encodeChar :: Char -> Text
403 encodeChar '\0' = "\\0"
404 encodeChar '\b' = "\\b"
405 encodeChar '\t' = "\\t"
406 encodeChar '\n' = "\\n"
407 encodeChar '\v' = "\\v"
408 encodeChar '\f' = "\\f"
409 encodeChar '\r' = "\\r"
410 encodeChar '"' = "\\\""
411 encodeChar '\\' = "\\\\"
412 encodeChar c = T.singleton $ c
413stringLiteral _ = "\"\\uFFFD\""
415unbox :: Text -> AST -> Text
416unbox _ v@(NumericLiteral{}) = prettyPrintCpp1 v
417unbox _ v@(BooleanLiteral{}) = prettyPrintCpp1 v
418unbox _ v@(StringLiteral{}) = prettyPrintCpp1 v
419unbox t v = "unbox<" <> t <> ">(" <> prettyPrintCpp1 v <> ")"
421unboxType :: Text -> Maybe Text
424 t == C.semiringInt ||
427 | t == C.semiringNumber ||
429 t == C.euclideanRingNumber
431 | otherwise = Nothing
433renderOp :: Text -> Maybe Text
435 | op == C.eq = Just " == "
436 | op == C.notEq = Just " != "
437 | op == C.add = Just " + "
438 | op == C.sub = Just " - "
439 | op == C.mul = Just " * "
440 | op == C.div = Just " / "
441 | otherwise = Nothing
443interfaceSource :: Text -> [Ident] -> [Ident] -> Text
444interfaceSource mn exports foreigns =
445 "// Generated by pscpp compiler\n\n" <>
446 "#ifndef " <> mn <> "_H\n" <>
447 "#define " <> mn <> "_H\n\n" <>
448 "#include \"purescript.h\"\n" <>
450 "namespace " <> mn <> " {\n\n" <>
451 "using namespace purescript;\n\n" <>
452 (T.concat $ (\export -> "auto " <> identToCpp export <> "() -> boxed;\n") <$> (exports \\ foreigns)) <>
454 (if null foreigns then "" else ("// Foreign values\n\nauto " <> foreignDict <> "() -> " <> dictType <> "&;\n\n")) <>
455 (T.concat $ (\foreign' -> "inline auto " <>
456 identToCpp foreign' <>
457 "() -> const boxed& { " <>
459 foreignDict <> "().at(" <>
460 (stringLiteral . mkString $ runIdent foreign') <> "); " <>
462 " };\n") <$> foreigns) <>
464 "} // end namespace " <> mn <>
466 "#endif // " <> mn <> "_H\n\n"
468implHeaderSource :: Text -> [Text] -> Text -> Text
469implHeaderSource mn imports interfaceImport =
470 T.concat imports <> "\n"<>
471 interfaceImport <> "\n\n" <>
472 "namespace " <> mn <> " {\n\n"
474implFooterSource :: Text -> [Ident] -> Text
475implFooterSource mn foreigns =
477 (if null foreigns then "" else ("auto " <> foreignDict <> "() -> " <> dictType <> "& {\n" <>
478 " static " <> dictType <> " _dict_;\n" <>
479 " return _dict_;\n" <>
481 "} // end namespace " <> mn <>
483 if mn == "Main" then mainSource else "\n"
487 \int main(int argc, const char * argv[]) {\n\
488 \ Main::main()(nullptr);\n\
512foreignDict = "_foreign_"
514dispatchOnceBegin :: Text
515dispatchOnceBegin = "static const boxed _value_ = "
517dispatchOnceEnd :: Text
518dispatchOnceEnd = "return _value_;"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list