git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: use vectors instead of maps for ADTs
commit8d700016dc351536bf21f8844eac63386d28e803 authorAndy Arvanitis <andyarvanitis@gmail.com> dateTue 19 Jan 2016, 20:45 (11 years ago) parentc0ec8bf
5 files changed+11−9
pcc/include/purescript.hh+1 src/Language/PureScript/CodeGen/Cpp.hs+6−3 src/Language/PureScript/CodeGen/Cpp/Common.hs+1−4 src/Language/PureScript/CodeGen/Cpp/Types.hs+1−1 src/Language/PureScript/Pretty/Cpp.hs+2−1
pcc/include/purescript.hh+1
@@ -56,6 +56,7 @@ public:
5656};
5757
5858const bool undefined = false;
59+const std::vector<int>::size_type kDataCtor = 0;
5960
6061// A variant data class designed to provide some features of dynamic typing.
6162//
src/Language/PureScript/CodeGen/Cpp.hs+6−3
@@ -26,6 +26,7 @@ module Language.PureScript.CodeGen.Cpp
2626 , P.prettyPrintCpp
2727 ) where
2828
29+import Data.Char (isLetter)
2930import Data.List
3031import Data.Function (on)
3132import qualified Data.Map as M
@@ -149,7 +150,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
149150 CppLambda [CppCaptureAll] (farg <$> [f'])
150151 (Just $ CppAny [])
151152 (CppBlock $ fieldLambdas fs')]
152− | otherwise = [CppReturn (CppObjectLiteral ((ctorKey, CppStringLiteral name) : zip fields' (CppVar <$> fields')))]
153+ | otherwise = [CppReturn (CppArrayLiteral (CppStringLiteral name : (CppVar <$> fields')))]
153154
154155 declToCpp _ ident val = do
155156 val' <- valueToCpp val
@@ -407,7 +408,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
407408 return $ case ctorType of
408409 ProductType -> cpps
409410 SumType ->
410− [ CppIfElse (CppBinary Equal (CppIndexer (CppStringLiteral ctorKey) (CppVar varName))
411+ [ CppIfElse (CppBinary Equal (CppIndexer (CppVar ctorKey) (CppVar varName))
411412 (CppStringLiteral ctor'))
412413 (CppBlock cpps)
413414 Nothing ]
@@ -420,8 +421,10 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
420421 cpps <- binderToCpp argVar done'' binder
421422 return (CppVariableIntroduction (argVar, Nothing)
422423 []
423− (Just (CppIndexer (CppStringLiteral $ identToCpp field) (CppVar varName)))
424+ (Just (CppIndexer (fieldToIndex field) (CppVar varName)))
424425 : cpps)
426+ fieldToIndex :: Ident -> Cpp
427+ fieldToIndex = CppNumericLiteral . Left . (+1) . read . dropWhile isLetter . runIdent
425428
426429 binderToCpp _ _ b@(ConstructorBinder{}) =
427430 error $ "Invalid ConstructorBinder in binderToCpp: " ++ show b
src/Language/PureScript/CodeGen/Cpp/Common.hs+1−4
@@ -106,7 +106,6 @@ nameIsCppReserved name =
106106 , "decltype"
107107 , "default"
108108 , "delete"
109− , "djb2"
110109 , "do"
111110 , "double"
112111 , "dynamic_cast"
@@ -121,15 +120,13 @@ nameIsCppReserved name =
121120 , "for"
122121 , "friend"
123122 , "goto"
124− , "HASH"
125123 , "if"
126124 , "import"
125+ , "kDataCtor"
127126 , "inline"
128127 , "int"
129− , "KEY"
130128 , "list"
131129 , "long"
132− , "map_key_t"
133130 , "mutable"
134131 , "namespace"
135132 , "new"
src/Language/PureScript/CodeGen/Cpp/Types.hs+1−1
@@ -119,4 +119,4 @@ unthunkMarkerValue :: String
119119unthunkMarkerValue = "any::unthunk"
120120
121121ctorKey :: String
122−ctorKey = "constructor"
122+ctorKey = "kDataCtor"
src/Language/PureScript/Pretty/Cpp.hs+2−1
@@ -303,7 +303,8 @@ accessor = mkPattern match
303303indexer :: Pattern PrinterState Cpp (String, Cpp)
304304indexer = mkPattern' match
305305 where
306− match (CppIndexer (CppNumericLiteral (Left index)) val) = return (show index ++ "UL", val)
306+ match (CppIndexer (CppNumericLiteral (Left 0)) val) = return ("0UL", val)
307+ match (CppIndexer (CppNumericLiteral (Left index)) val) = return (show index, val)
307308 match (CppIndexer index val) = do
308309 index' <- prettyPrintCpp' index
309310 return (index', val)