@@ -6,6 +6,7 @@ module CodeGen.Cpp.Printer
6 , interfaceSource
6 , interfaceSource
7 , implHeaderSource
7 , implHeaderSource
8 , implFooterSource
8 , implFooterSource
11import Prelude.Compat
12import Prelude.Compat
@@ -15,7 +16,7 @@ import Control.Monad (forM, mzero)
15import Control.Monad.State (StateT, evalStateT)
16import Control.Monad.State (StateT, evalStateT)
16import Control.PatternArrows
17import Control.PatternArrows
17import Data.Char (isAscii)
18import Data.Char (isAscii)
18−import Data.List ((\\))
19+import Data.List (filter)
19import qualified Control.Arrow as A
20import qualified Control.Arrow as A
21import Data.Maybe (fromMaybe)
22import Data.Maybe (fromMaybe)
@@ -125,46 +126,36 @@ literals = mkPattern' match'
125 , intercalate (emit ", ") <$> forM args prettyPrintCpp'
126 , intercalate (emit ", ") <$> forM args prettyPrintCpp'
126 , return $ emit ")"
127 , return $ emit ")"
128− match (Function _ (Just name) [] ret@(Block _ [Return _ (Function {})])) = mconcat <$> sequence
129+ match (Function _ (Just name) [] (Block _ [Return _ ret]))
130+ | isLiteral ret = mconcat <$> sequence
129 [ return $ emit "auto "
131 [ return $ emit "auto "
130 , return $ emit name
132 , return $ emit name
131− , return $ emit "() -> boxed "
133+ , return $ emit "() -> const boxed& "
132− , prettyPrintCpp' ret
134− match (Function _ (Just name) [] (Block _ [Return _ ret])) = mconcat <$> sequence
135− [ return $ emit "auto "
136− , return $ emit name
137− , return $ emit "() -> boxed "
138 , return $ emit "{\n"
134 , return $ emit "{\n"
139 , withIndent $ do
135 , withIndent $ do
140 indentString <- currentIndent
136 indentString <- currentIndent
141− return $ indentString <> emit dispatchOnceBegin
137+ return $ indentString <> emit staticValueBegin
142 , withIndent $ do
138 , withIndent $ do
140+ Function _ Nothing args body -> mconcat <$> sequence
141+ [ return $ emit "[]("
142+ , return $ emit $ intercalate ", " (renderArg <$> args)
143+ , return $ emit ") -> boxed "
144+ , prettyPrintCpp' body
143− prettyPrintCpp' ret
147+ prettyPrintCpp' ret
144 , return $ emit ";\n"
148 , return $ emit ";\n"
145 , withIndent $ do
149 , withIndent $ do
146 indentString <- currentIndent
150 indentString <- currentIndent
147− return $ indentString <> emit dispatchOnceEnd
151+ return $ indentString <> emit staticValueEnd
148 , return $ emit "\n}"
152 , return $ emit "\n}"
150− -- match (Function _ (Just name) [] ret) = mconcat <$> sequence
154+ 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 "
155 [ return $ emit "auto "
153 , return $ emit name
156 , return $ emit name
154− , return $ emit "() -> boxed {\n"
157+ , return $ emit "() -> boxed "
156− prettyStatements $ init sts
157− , return $ emit "\n"
159− indentString <- currentIndent
160− return $ indentString <> emit dispatchOnceBegin
162− prettyPrintCpp' ret
158+ , prettyPrintCpp' ret
163− , return $ emit ";\n"
165− indentString <- currentIndent
166− return $ indentString <> emit dispatchOnceEnd
167− , return $ emit "\n}"
169 match (Function _ name args ret) = mconcat <$> sequence
160 match (Function _ name args ret) = mconcat <$> sequence
170 [ return $ emit $ if name == (Just tcoLoop) then "[&]" else "[=]"
161 [ return $ emit $ if name == (Just tcoLoop) then "[&]" else "[=]"
@@ -327,6 +318,15 @@ binary op str = AssocL match (\v1 v2 -> v1 <> emit (" " <> str <> " ") <> v2)
327 match' (Binary _ op' v1 v2) | op' == op = Just (v1, v2)
318 match' (Binary _ op' v1 v2) | op' == op = Just (v1, v2)
328 match' _ = Nothing
319 match' _ = Nothing
321+isLiteral :: AST -> Bool
322+isLiteral Function{} = True
323+isLiteral NumericLiteral{} = True
324+isLiteral StringLiteral{} = True
325+isLiteral BooleanLiteral{} = True
326+isLiteral ObjectLiteral{} = True
327+isLiteral ArrayLiteral{} = True
330prettyStatements :: (Emit gen) => [AST] -> StateT PrinterState Maybe gen
330prettyStatements :: (Emit gen) => [AST] -> StateT PrinterState Maybe gen
331prettyStatements sts = do
331prettyStatements sts = do
332 cpps <- forM sts prettyPrintCpp'
332 cpps <- forM sts prettyPrintCpp'
@@ -428,8 +428,10 @@ renderOp op
428 | op == C.div = Just " / "
428 | op == C.div = Just " / "
429 | otherwise = Nothing
429 | otherwise = Nothing
431−interfaceSource :: Text -> [Ident] -> [Ident] -> Text
431+interfaceSource :: Text -> [(Text,Bool)] -> [Ident] -> Text
432interfaceSource mn exports foreigns =
432interfaceSource mn exports foreigns =
433+ let foreigns' = identToCpp <$> foreigns
434+ exports' = filter ((`notElem` foreigns') . fst) exports in
433 "// Generated by pscpp compiler\n\n" <>
435 "// Generated by pscpp compiler\n\n" <>
434 "#ifndef " <> mn <> "_H\n" <>
436 "#ifndef " <> mn <> "_H\n" <>
435 "#define " <> mn <> "_H\n\n" <>
437 "#define " <> mn <> "_H\n\n" <>
@@ -437,16 +439,18 @@ interfaceSource mn exports foreigns =
438 "namespace " <> mn <> " {\n\n" <>
440 "namespace " <> mn <> " {\n\n" <>
439 "using namespace purescript;\n\n" <>
441 "using namespace purescript;\n\n" <>
442+ (T.concat $ (\(export, static) ->
443+ let rty = if static then "const boxed&" else "boxed" in
440− (T.concat $ (\export -> "auto " <> identToCpp export <> "() -> boxed;\n") <$> (exports \\ foreigns)) <>
444+ "auto " <> export <> "() -> " <> rty <> ";\n") <$> exports') <>
442 (if null foreigns then "" else ("// Foreign values\n\nauto " <> foreignDict <> "() -> " <> dictType <> "&;\n\n")) <>
446 (if null foreigns then "" else ("// Foreign values\n\nauto " <> foreignDict <> "() -> " <> dictType <> "&;\n\n")) <>
443 (T.concat $ (\foreign' -> "inline auto " <>
447 (T.concat $ (\foreign' -> "inline auto " <>
444 identToCpp foreign' <>
448 identToCpp foreign' <>
445− "() -> " <> varDecl <> " { " <>
449+ "() -> const boxed& { " <>
446− dispatchOnceBegin <>
450+ staticValueBegin <>
447 foreignDict <> "().at(" <>
451 foreignDict <> "().at(" <>
448 (stringLiteral . mkString $ runIdent foreign') <> "); " <>
452 (stringLiteral . mkString $ runIdent foreign') <> "); " <>
449− dispatchOnceEnd <>
453+ staticValueEnd <>
450 " };\n") <$> foreigns) <>
454 " };\n") <$> foreigns) <>
452 "} // end namespace " <> mn <>
456 "} // end namespace " <> mn <>
@@ -489,8 +493,8 @@ renderArg arg
489foreignDict :: Text
493foreignDict :: Text
490foreignDict = "foreign"
494foreignDict = "foreign"
492−dispatchOnceBegin :: Text
496+staticValueBegin :: Text
493−dispatchOnceBegin = "static const boxed $value$ = "
497+staticValueBegin = "static const boxed _ = "
495−dispatchOnceEnd :: Text
499+staticValueEnd :: Text
496−dispatchOnceEnd = "return $value$;"
500+staticValueEnd = "return _;"