git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: use vectors instead of maps for ADTs
8d70001Andy Arvanitis
pcc/include/purescript.hh+1
@@ -56,6 +56,7 @@ public:
56};
56};
57
57
58const bool undefined = false;
58const bool undefined = false;
59+const std::vector<int>::size_type kDataCtor = 0;
59
60
60// A variant data class designed to provide some features of dynamic typing.
61// A variant data class designed to provide some features of dynamic typing.
61//
62//
src/Language/PureScript/CodeGen/Cpp.hs+6−3
@@ -26,6 +26,7 @@ module Language.PureScript.CodeGen.Cpp
26 , P.prettyPrintCpp
26 , P.prettyPrintCpp
27 ) where
27 ) where
28
28
29+import Data.Char (isLetter)
29import Data.List
30import Data.List
30import Data.Function (on)
31import Data.Function (on)
31import qualified Data.Map as M
32import qualified Data.Map as M
@@ -149,7 +150,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
149 CppLambda [CppCaptureAll] (farg <$> [f'])
150 CppLambda [CppCaptureAll] (farg <$> [f'])
150 (Just $ CppAny [])
151 (Just $ CppAny [])
151 (CppBlock $ fieldLambdas fs')]
152 (CppBlock $ fieldLambdas fs')]
152− | otherwise = [CppReturn (CppObjectLiteral ((ctorKey, CppStringLiteral name) : zip fields' (CppVar <$> fields')))]
153+ | otherwise = [CppReturn (CppArrayLiteral (CppStringLiteral name : (CppVar <$> fields')))]
153
154
154 declToCpp _ ident val = do
155 declToCpp _ ident val = do
155 val' <- valueToCpp val
156 val' <- valueToCpp val
@@ -407,7 +408,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
407 return $ case ctorType of
408 return $ case ctorType of
408 ProductType -> cpps
409 ProductType -> cpps
409 SumType ->
410 SumType ->
410− [ CppIfElse (CppBinary Equal (CppIndexer (CppStringLiteral ctorKey) (CppVar varName))
411+ [ CppIfElse (CppBinary Equal (CppIndexer (CppVar ctorKey) (CppVar varName))
411 (CppStringLiteral ctor'))
412 (CppStringLiteral ctor'))
412 (CppBlock cpps)
413 (CppBlock cpps)
413 Nothing ]
414 Nothing ]
@@ -420,8 +421,10 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
420 cpps <- binderToCpp argVar done'' binder
421 cpps <- binderToCpp argVar done'' binder
421 return (CppVariableIntroduction (argVar, Nothing)
422 return (CppVariableIntroduction (argVar, Nothing)
422 []
423 []
423− (Just (CppIndexer (CppStringLiteral $ identToCpp field) (CppVar varName)))
424+ (Just (CppIndexer (fieldToIndex field) (CppVar varName)))
424 : cpps)
425 : cpps)
426+ fieldToIndex :: Ident -> Cpp
427+ fieldToIndex = CppNumericLiteral . Left . (+1) . read . dropWhile isLetter . runIdent
425
428
426 binderToCpp _ _ b@(ConstructorBinder{}) =
429 binderToCpp _ _ b@(ConstructorBinder{}) =
427 error $ "Invalid ConstructorBinder in binderToCpp: " ++ show b
430 error $ "Invalid ConstructorBinder in binderToCpp: " ++ show b
src/Language/PureScript/CodeGen/Cpp/Common.hs+1−4
@@ -106,7 +106,6 @@ nameIsCppReserved name =
106 , "decltype"
106 , "decltype"
107 , "default"
107 , "default"
108 , "delete"
108 , "delete"
109− , "djb2"
110 , "do"
109 , "do"
111 , "double"
110 , "double"
112 , "dynamic_cast"
111 , "dynamic_cast"
@@ -121,15 +120,13 @@ nameIsCppReserved name =
121 , "for"
120 , "for"
122 , "friend"
121 , "friend"
123 , "goto"
122 , "goto"
124− , "HASH"
125 , "if"
123 , "if"
126 , "import"
124 , "import"
125+ , "kDataCtor"
127 , "inline"
126 , "inline"
128 , "int"
127 , "int"
129− , "KEY"
130 , "list"
128 , "list"
131 , "long"
129 , "long"
132− , "map_key_t"
133 , "mutable"
130 , "mutable"
134 , "namespace"
131 , "namespace"
135 , "new"
132 , "new"
src/Language/PureScript/CodeGen/Cpp/Types.hs+1−1
@@ -119,4 +119,4 @@ unthunkMarkerValue :: String
119unthunkMarkerValue = "any::unthunk"
119unthunkMarkerValue = "any::unthunk"
120
120
121ctorKey :: String
121ctorKey :: String
122−ctorKey = "constructor"
122+ctorKey = "kDataCtor"
src/Language/PureScript/Pretty/Cpp.hs+2−1
@@ -303,7 +303,8 @@ accessor = mkPattern match
303indexer :: Pattern PrinterState Cpp (String, Cpp)
303indexer :: Pattern PrinterState Cpp (String, Cpp)
304indexer = mkPattern' match
304indexer = mkPattern' match
305 where
305 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)
307 match (CppIndexer index val) = do
308 match (CppIndexer index val) = do
308 index' <- prettyPrintCpp' index
309 index' <- prettyPrintCpp' index
309 return (index', val)
310 return (index', val)