@@ -71,7 +71,14 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
71 (snd <$> imps)
71 (snd <$> imps)
72 let cppImports' = "PureScript" : cppImports
72 let cppImports' = "PureScript" : cppImports
73 cppDecls <- concat <$> mapM (bindToCpp [CppTopLevel]) decls
73 cppDecls <- concat <$> mapM (bindToCpp [CppTopLevel]) decls
74+ let namesmap = M.toList .
75+ M.mapKeysMonotonic (qualifiedToCpp id) . M.map (\(t, _, _) -> arity t) $
78+ M.mapKeysMonotonic (qualifiedToCpp (Ident . runProperName)) .
79+ M.map (\(_, _, _, fields) -> length fields) $
80+ E.dataConstructors env
74− cppDecls' <- traverse optimize cppDecls
81+ cppDecls' <- traverse (optimize $ namesmap ++ datamap) cppDecls
75 foreignWrappers <- curriedForeigns
82 foreignWrappers <- curriedForeigns
76 let optimized = cppDecls' ++ foreignWrappers
83 let optimized = cppDecls' ++ foreignWrappers
77 let moduleHeader =
84 let moduleHeader =
@@ -127,7 +134,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
127 in return $ CppStruct className [CppEnum Nothing Nothing (sort supers ++ members)]
134 in return $ CppStruct className [CppEnum Nothing Nothing (sort supers ++ members)]
129 declToCpp vqs (_, ident) (Abs (_, com, ty, _) arg body) = do
136 declToCpp vqs (_, ident) (Abs (_, com, ty, _) arg body) = do
130− fn <- if arity' > 1 && CppTopLevel `elem` vqs
137+ fn <- if CppTopLevel `elem` vqs
132 argNames <- replicateM (arity' - length args' - 1) freshName
139 argNames <- replicateM (arity' - length args' - 1) freshName
133 let args'' = zip argNames (repeat aty)
140 let args'' = zip argNames (repeat aty)
@@ -151,7 +158,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
151 (curriedName name)
158 (curriedName name)
154− []
161+ (if arity' == 1 then [CppInline] else [])
155 (asReturnBlock $
162 (asReturnBlock $
156 curriedLambda
163 curriedLambda
157 (fst <$> args' ++ args'')
164 (fst <$> args' ++ args'')
@@ -210,7 +217,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
210 CppDataLiteral $
217 CppDataLiteral $
211 CppAccessor (CppVar name) (CppVar "data") : (CppVar <$> fields'))
218 CppAccessor (CppVar name) (CppVar "data") : (CppVar <$> fields'))
213− if length fields > 1
220+ if length fields > 0
215 [ CppFunction
222 [ CppFunction
216 (curriedName name)
223 (curriedName name)
@@ -263,7 +270,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
265 (asReturnBlock block)
272 (asReturnBlock block)
266− ] ++ if arity' > 1
273+ ] ++ if arity' > 0
267 then [fn']
274 then [fn']
@@ -387,17 +394,14 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
387 -------------------------------------------------------------------------------------------------
394 -------------------------------------------------------------------------------------------------
388 valueToCpp :: Expr Ann -> m Cpp
395 valueToCpp :: Expr Ann -> m Cpp
389 -------------------------------------------------------------------------------------------------
396 -------------------------------------------------------------------------------------------------
390− valueToCpp (Var (_, _, _, Just (IsConstructor _ [])) ident) =
397+ valueToCpp (Var (_, _, _, Just IsNewtype) ident) =
391− return $ CppApp (varToCpp ident) []
393− valueToCpp (Var (_, _, _, Just (IsConstructor _ [_])) ident) =
394 return $ varToCpp ident
398 return $ varToCpp ident
396− valueToCpp (Var (_, _, _, Just (IsConstructor _ _)) ident) =
400+ valueToCpp (Var (_, _, _, Just (IsConstructor _ [])) ident) =
397− return . curriedName' $ varToCpp ident
401+ return $ CppApp (varToCpp ident) []
399 valueToCpp (Var (_, _, Just ty, _) ident@(Qualified (Just _) _))
403 valueToCpp (Var (_, _, Just ty, _) ident@(Qualified (Just _) _))
400− | arity ty > 1 =
404+ | arity ty > 0 =
401 return . curriedName' $ varToCpp ident
405 return . curriedName' $ varToCpp ident
403 valueToCpp (Var _ ident) =
407 valueToCpp (Var _ ident) =
@@ -460,34 +464,16 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
460 args' <- mapM valueToCpp args
464 args' <- mapM valueToCpp args
461 case f of
465 case f of
462 Var (_, _, _, Just IsNewtype) _ -> return (head args')
466 Var (_, _, _, Just IsNewtype) _ -> return (head args')
463− Var (_, _, _, Just (IsConstructor _ fields)) ident
464− | length args == length fields ->
465− return $ CppApp (uncurried' $ qualifiedToCpp id ident) args'
466 Var (_, _, _, Just IsTypeClassConstructor) (Qualified mn' (Ident classname)) ->
467 Var (_, _, _, Just IsTypeClassConstructor) (Qualified mn' (Ident classname)) ->
467 let Just (_, constraints, fns) = findClass (Qualified mn' (ProperName classname)) in
468 let Just (_, constraints, fns) = findClass (Qualified mn' (ProperName classname)) in
468 return . CppObjectLiteral CppInstance $
469 return . CppObjectLiteral CppInstance $
470 ((sort $ superClassDictionaryNames constraints) ++ (fst <$> fns))
471 ((sort $ superClassDictionaryNames constraints) ++ (fst <$> fns))
472− Var ann (Qualified (Just mn') ident)
473+ Var _ (Qualified (Just _) _) -> applied f args' curriedName'
473− | arity' <- maybe 0 arity ty,
474+ _ -> applied f args' id
476− let (uncurriedArgs, curriedArgs) =
477− if length args' >= arity'
478− then splitAt arity' args'
481− if null uncurriedArgs
482− then curriedName' f'
483− else CppApp (uncurried' f') uncurriedArgs
484− return $ foldl (\fn a -> CppApp fn [a]) fn' curriedArgs
486− ty | (_, _, Just t, _) <- ann = Just t
487− | Just (t, _, _) <- M.lookup (Qualified (Just mn') ident) (E.names env) = Just t
488− | otherwise = Nothing
490− flip (foldl (\fn a -> CppApp fn [a])) args' <$> valueToCpp f
476+ applied f args g = flip (foldl (\fn a -> CppApp (g fn) [a])) args <$> valueToCpp f
492 valueToCpp (Case (maybeSpan, _, ty, _) values binders) = do
478 valueToCpp (Case (maybeSpan, _, ty, _) values binders) = do
493 vals <- mapM valueToCpp values
479 vals <- mapM valueToCpp values
@@ -792,7 +778,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
793 go (ident, ty)
779 go (ident, ty)
794 | arity' <- arity ty,
780 | arity' <- arity ty,
795− arity' > 1 = do
781+ arity' > 0 = do
796 let name = identToCpp ident
782 let name = identToCpp ident
797 argNames <- replicateM arity' freshName
783 argNames <- replicateM arity' freshName
@@ -800,7 +786,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
800 (curriedName name)
786 (curriedName name)
801 [(head argNames, Just $ CppAny [CppConst, CppRef])]
787 [(head argNames, Just $ CppAny [CppConst, CppRef])]
802 (Just $ CppAny [])
788 (Just $ CppAny [])
803− []
789+ (if arity' == 1 then [CppInline] else [])
804 (asReturnBlock $
790 (asReturnBlock $
805 curriedLambda
791 curriedLambda
806 (tail argNames)
792 (tail argNames)
@@ -876,13 +862,6 @@ curriedName :: String -> String
876curriedName name@('$' : _) = name
862curriedName name@('$' : _) = name
877curriedName name = '$' : name
863curriedName name = '$' : name
879−---------------------------------------------------------------------------------------------------
880−uncurried' :: Cpp -> Cpp
881−---------------------------------------------------------------------------------------------------
882−uncurried' (CppVar ('$':name)) = CppVar name
883−uncurried' (CppAccessor a b) = CppAccessor (uncurried' a) b
886---------------------------------------------------------------------------------------------------
865---------------------------------------------------------------------------------------------------
887curriedName' :: Cpp -> Cpp
866curriedName' :: Cpp -> Cpp
888---------------------------------------------------------------------------------------------------
867---------------------------------------------------------------------------------------------------