git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: cleanup -- CppMapLiteral instead of CppObjectLiteral
5973897Andy Arvanitis
src/Language/PureScript/CodeGen/Cpp.hs+3−3
@@ -442,7 +442,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
442 CppArrayLiteral <$> mapM valueToCpp xs
442 CppArrayLiteral <$> mapM valueToCpp xs
443
443
444 valueToCpp (Literal _ (ObjectLiteral ps)) =
444 valueToCpp (Literal _ (ObjectLiteral ps)) =
445− CppObjectLiteral CppRecord <$>
445+ CppMapLiteral CppRecord <$>
446 mapM (sndM valueToCpp) ((\(k,v) -> (CppSymbol k, v)) <$> sortBy (compare `on` fst) ps)
446 mapM (sndM valueToCpp) ((\(k,v) -> (CppSymbol k, v)) <$> sortBy (compare `on` fst) ps)
447
447
448 valueToCpp (Accessor _ prop val) =
448 valueToCpp (Accessor _ prop val) =
@@ -454,7 +454,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
454 updatedFields <- mapM (sndM valueToCpp) ps
454 updatedFields <- mapM (sndM valueToCpp) ps
455 let origKeys = (allKeys ty) \\ (fst <$> updatedFields)
455 let origKeys = (allKeys ty) \\ (fst <$> updatedFields)
456 origFields = (\key -> (key, CppIndexer (CppSymbol key) obj')) <$> origKeys
456 origFields = (\key -> (key, CppIndexer (CppSymbol key) obj')) <$> origKeys
457− return $ CppObjectLiteral CppRecord $
457+ return $ CppMapLiteral CppRecord $
458 (\(k,v) -> (CppSymbol k, v)) <$> sortBy (compare `on` fst) (origFields ++ updatedFields)
458 (\(k,v) -> (CppSymbol k, v)) <$> sortBy (compare `on` fst) (origFields ++ updatedFields)
459 where
459 where
460 allKeys :: T.Type -> [String]
460 allKeys :: T.Type -> [String]
@@ -487,7 +487,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
487 Var (_, _, _, Just IsNewtype) _ -> return (head args')
487 Var (_, _, _, Just IsNewtype) _ -> return (head args')
488 Var (_, _, _, Just IsTypeClassConstructor) (Qualified mn' (Ident classname)) ->
488 Var (_, _, _, Just IsTypeClassConstructor) (Qualified mn' (Ident classname)) ->
489 let Just (_, constraints, fns) = findClass (Qualified mn' (ProperName classname)) in
489 let Just (_, constraints, fns) = findClass (Qualified mn' (ProperName classname)) in
490− return . CppObjectLiteral CppInstance $
490+ return . CppMapLiteral CppInstance $
491 zip
491 zip
492 (CppSymbol <$> (sort $ superClassDictionaryNames constraints) ++ (fst <$> fns))
492 (CppSymbol <$> (sort $ superClassDictionaryNames constraints) ++ (fst <$> fns))
493 args'
493 args'
src/Language/PureScript/CodeGen/Cpp/AST.hs+4−4
@@ -185,7 +185,7 @@ data Cpp
185 -- |
185 -- |
186 -- An object literal
186 -- An object literal
187 --
187 --
188− | CppObjectLiteral CppObjectType [(Cpp, Cpp)]
188+ | CppMapLiteral CppMapType [(Cpp, Cpp)]
189 -- |
189 -- |
190 -- An general property accessor expression (property, expr)
190 -- An general property accessor expression (property, expr)
191 --
191 --
@@ -302,7 +302,7 @@ everywhereOnCpp f = go
302 go (CppArrayLiteral cpp) = f (CppArrayLiteral (map go cpp))
302 go (CppArrayLiteral cpp) = f (CppArrayLiteral (map go cpp))
303 go (CppDataLiteral cpp) = f (CppDataLiteral (map go cpp))
303 go (CppDataLiteral cpp) = f (CppDataLiteral (map go cpp))
304 go (CppIndexer j1 j2) = f (CppIndexer (go j1) (go j2))
304 go (CppIndexer j1 j2) = f (CppIndexer (go j1) (go j2))
305− go (CppObjectLiteral t cpps) = f (CppObjectLiteral t (map (fmap go) cpps))
305+ go (CppMapLiteral t cpps) = f (CppMapLiteral t (map (fmap go) cpps))
306 go (CppAccessor prop j) = f (CppAccessor (go prop) (go j))
306 go (CppAccessor prop j) = f (CppAccessor (go prop) (go j))
307 go (CppFunction name args rty qs j) = f (CppFunction name args rty qs (go j))
307 go (CppFunction name args rty qs j) = f (CppFunction name args rty qs (go j))
308 go (CppLambda cps args rty j) = f (CppLambda cps args rty (go j))
308 go (CppLambda cps args rty j) = f (CppLambda cps args rty (go j))
@@ -333,7 +333,7 @@ everywhereOnCppTopDownM f = f >=> go
333 go (CppArrayLiteral cpp) = CppArrayLiteral <$> traverse f' cpp
333 go (CppArrayLiteral cpp) = CppArrayLiteral <$> traverse f' cpp
334 go (CppDataLiteral cpp) = CppDataLiteral <$> traverse f' cpp
334 go (CppDataLiteral cpp) = CppDataLiteral <$> traverse f' cpp
335 go (CppIndexer j1 j2) = CppIndexer <$> f' j1 <*> f' j2
335 go (CppIndexer j1 j2) = CppIndexer <$> f' j1 <*> f' j2
336− go (CppObjectLiteral t cpps) = CppObjectLiteral t <$> traverse (pairM f' f') cpps
336+ go (CppMapLiteral t cpps) = CppMapLiteral t <$> traverse (pairM f' f') cpps
337 go (CppAccessor prop j) = CppAccessor prop <$> f' j
337 go (CppAccessor prop j) = CppAccessor prop <$> f' j
338 go (CppFunction name args rty qs j) = CppFunction name args rty qs <$> f' j
338 go (CppFunction name args rty qs j) = CppFunction name args rty qs <$> f' j
339 go (CppLambda cps args rty j) = CppLambda cps args rty <$> f' j
339 go (CppLambda cps args rty j) = CppLambda cps args rty <$> f' j
@@ -360,7 +360,7 @@ everythingOnCpp (<>) f = go
360 go j@(CppArrayLiteral cpp) = foldl (<>) (f j) (map go cpp)
360 go j@(CppArrayLiteral cpp) = foldl (<>) (f j) (map go cpp)
361 go j@(CppDataLiteral cpp) = foldl (<>) (f j) (map go cpp)
361 go j@(CppDataLiteral cpp) = foldl (<>) (f j) (map go cpp)
362 go j@(CppIndexer j1 j2) = f j <> go j1 <> go j2
362 go j@(CppIndexer j1 j2) = f j <> go j1 <> go j2
363− go j@(CppObjectLiteral _ cpps) = foldl (<>) (f j) ((map (go . fst) cpps) ++ (map (go . snd) cpps))
363+ go j@(CppMapLiteral _ cpps) = foldl (<>) (f j) ((map (go . fst) cpps) ++ (map (go . snd) cpps))
364 go j@(CppAccessor j1 j2) = f j <> go j1 <> go j2
364 go j@(CppAccessor j1 j2) = f j <> go j1 <> go j2
365 go j@(CppFunction _ _ _ _ j1) = f j <> go j1
365 go j@(CppFunction _ _ _ _ j1) = f j <> go j1
366 go j@(CppLambda _ _ _ j1) = f j <> go j1
366 go j@(CppLambda _ _ _ j1) = f j <> go j1
src/Language/PureScript/CodeGen/Cpp/File.hs+3−3
@@ -58,7 +58,7 @@ toHeader = catMaybes . map go
58 -- Generate thunks for top-level values
58 -- Generate thunks for top-level values
59 go (CppVariableIntroduction (name, _) _ (Just cpp)) =
59 go (CppVariableIntroduction (name, _) _ (Just cpp)) =
60 case cpp of
60 case cpp of
61− CppObjectLiteral CppInstance _ ->
61+ CppMapLiteral CppInstance _ ->
62 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] CppNoOp
62 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] CppNoOp
63 _ ->
63 _ ->
64 Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [CppExtern] Nothing
64 Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [CppExtern] Nothing
@@ -130,7 +130,7 @@ toBody = catMaybes . map go
130 -- Generate thunks for top-level values
130 -- Generate thunks for top-level values
131 go (CppVariableIntroduction (name, _) _ (Just cpp)) =
131 go (CppVariableIntroduction (name, _) _ (Just cpp)) =
132 case cpp of
132 case cpp of
133− CppObjectLiteral CppInstance _ ->
133+ CppMapLiteral CppInstance _ ->
134 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] block
134 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] block
135 _ ->
135 _ ->
136 Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [] (Just lambda)
136 Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [] (Just lambda)
@@ -139,7 +139,7 @@ toBody = catMaybes . map go
139 block = CppBlock [val, CppReturn (CppVar "$value$")]
139 block = CppBlock [val, CppReturn (CppVar "$value$")]
140 lambda = CppLambda [] [("", Just $ thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) block
140 lambda = CppLambda [] [("", Just $ thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) block
141 addCaptures :: Cpp -> Cpp
141 addCaptures :: Cpp -> Cpp
142− addCaptures (CppObjectLiteral t objs) = CppObjectLiteral t (objlam <$> objs)
142+ addCaptures (CppMapLiteral t objs) = CppMapLiteral t (objlam <$> objs)
143 where
143 where
144 objlam :: (Cpp, Cpp) -> (Cpp, Cpp)
144 objlam :: (Cpp, Cpp) -> (Cpp, Cpp)
145 objlam (name', (CppLambda _ args rty body)) = (name' , CppLambda [] args rty $ addCaptures body)
145 objlam (name', (CppLambda _ args rty body)) = (name' , CppLambda [] args rty $ addCaptures body)
src/Language/PureScript/CodeGen/Cpp/Optimizer/MagicDo.hs+4−4
@@ -74,12 +74,12 @@ magicDo' = everywhereOnCpp undo . everywhereOnCppTopDown convert
74 | otherwise = CppVariableIntroduction arg [] (Just (CppApp m []))
74 | otherwise = CppVariableIntroduction arg [] (Just (CppApp m []))
75 -- Desugar untilE
75 -- Desugar untilE
76 convert (CppApp (CppApp f [arg]) []) | isEffFunc C.untilE f =
76 convert (CppApp (CppApp f [arg]) []) | isEffFunc C.untilE f =
77− CppApp (CppLambda [CppCaptureAll] [] Nothing (CppBlock [ CppWhile (CppUnary Not (CppApp arg [])) (CppBlock []), CppReturn $ CppObjectLiteral CppRecord []])) []
77+ CppApp (CppLambda [CppCaptureAll] [] Nothing (CppBlock [ CppWhile (CppUnary Not (CppApp arg [])) (CppBlock []), CppReturn $ CppMapLiteral CppRecord []])) []
78 -- Desugar whileE
78 -- Desugar whileE
79 convert (CppApp (CppApp f [arg1, arg2]) []) | isEffFunc C.whileE f =
79 convert (CppApp (CppApp f [arg1, arg2]) []) | isEffFunc C.whileE f =
80− CppApp (CppLambda [CppCaptureAll] [] Nothing (CppBlock [ CppWhile (CppApp arg1 []) (CppBlock [ CppApp arg2 [] ]), CppReturn $ CppObjectLiteral CppRecord []])) []
80+ CppApp (CppLambda [CppCaptureAll] [] Nothing (CppBlock [ CppWhile (CppApp arg1 []) (CppBlock [ CppApp arg2 [] ]), CppReturn $ CppMapLiteral CppRecord []])) []
81 convert (CppApp (CppApp (CppApp f [arg1]) [arg2]) []) | isEffFunc C.whileE f =
81 convert (CppApp (CppApp (CppApp f [arg1]) [arg2]) []) | isEffFunc C.whileE f =
82− CppApp (CppLambda [CppCaptureAll] [] Nothing (CppBlock [ CppWhile (CppApp arg1 []) (CppBlock [ CppApp arg2 [] ]), CppReturn $ CppObjectLiteral CppRecord []])) []
82+ CppApp (CppLambda [CppCaptureAll] [] Nothing (CppBlock [ CppWhile (CppApp arg1 []) (CppBlock [ CppApp arg2 [] ]), CppReturn $ CppMapLiteral CppRecord []])) []
83 convert other = other
83 convert other = other
84 -- Check if an expression represents a monomorphic call to >>= for the Eff monad
84 -- Check if an expression represents a monomorphic call to >>= for the Eff monad
85 isBind (CppApp fn [dict]) | isDict (C.eff, C.bindEffDictionary) dict && isBindPoly fn = True
85 isBind (CppApp fn [dict]) | isDict (C.eff, C.bindEffDictionary) dict && isBindPoly fn = True
@@ -128,7 +128,7 @@ inlineST = everywhereOnCpp convertBlock
128 -- or in a more aggressive way, turning wrappers into local variables depending on the
128 -- or in a more aggressive way, turning wrappers into local variables depending on the
129 -- agg(ressive) parameter.
129 -- agg(ressive) parameter.
130 convert agg (CppApp f [arg]) | isSTFunc C.newSTRef f =
130 convert agg (CppApp f [arg]) | isSTFunc C.newSTRef f =
131− CppLambda [CppCaptureAll] [] Nothing (CppBlock [CppReturn $ if agg then arg else CppObjectLiteral CppRecord [(CppSymbol C.stRefValue, arg)]])
131+ CppLambda [CppCaptureAll] [] Nothing (CppBlock [CppReturn $ if agg then arg else CppMapLiteral CppRecord [(CppSymbol C.stRefValue, arg)]])
132 convert agg (CppApp (CppApp f [ref]) []) | isSTFunc C.readSTRef f =
132 convert agg (CppApp (CppApp f [ref]) []) | isSTFunc C.readSTRef f =
133 if agg then ref else CppAccessor (CppVar C.stRefValue) ref
133 if agg then ref else CppAccessor (CppVar C.stRefValue) ref
134 convert agg (CppApp (CppApp (CppApp f [ref]) [arg]) []) | isSTFunc C.writeSTRef f =
134 convert agg (CppApp (CppApp (CppApp f [ref]) [arg]) []) | isSTFunc C.writeSTRef f =
src/Language/PureScript/CodeGen/Cpp/Types.hs+1−1
@@ -73,7 +73,7 @@ data CppCaptureType = CppCaptureAll
73-- |
73-- |
74-- C++ object/map literal type
74-- C++ object/map literal type
75--
75--
76−data CppObjectType = CppInstance | CppRecord
76+data CppMapType = CppInstance | CppRecord
77 deriving (Show, Eq)
77 deriving (Show, Eq)
78
78
79runType :: CppType -> String
79runType :: CppType -> String
src/Language/PureScript/Pretty/Cpp.hs+1−1
@@ -116,7 +116,7 @@ literals = mkPattern' match
116 , return $ intercalate ", " es
116 , return $ intercalate ", " es
117 , return " }"
117 , return " }"
118 ]
118 ]
119− match (CppObjectLiteral _ ps) = fmap concat $ sequence
119+ match (CppMapLiteral _ ps) = fmap concat $ sequence
120 [ return $ runType (mapType $ length ps + 1) ++ "{{\n"
120 [ return $ runType (mapType $ length ps + 1) ++ "{{\n"
121 , withIndent $ do
121 , withIndent $ do
122 cpps <- forM ps $ \(key, value) -> do
122 cpps <- forM ps $ \(key, value) -> do