@@ -52,8 +52,6 @@ import qualified Language.PureScript.Environment as E
52import qualified Language.PureScript.Pretty.Cpp as P
52import qualified Language.PureScript.Pretty.Cpp as P
53import qualified Language.PureScript.Types as T
53import qualified Language.PureScript.Types as T
58-- Generate code in the simplified C++1x intermediate representation for all declarations in a
56-- Generate code in the simplified C++1x intermediate representation for all declarations in a
@@ -120,20 +118,8 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
120 declToCpp _ (Op _) (Var (_, _, Nothing, _) _) =
118 declToCpp _ (Op _) (Var (_, _, Nothing, _) _) =
121 return CppNoOp -- skip operator aliases
119 return CppNoOp -- skip operator aliases
123− declToCpp _ ident (Abs _ arg@(Ident "dict")
124− body@(Accessor _ _ (Var _ (Qualified Nothing arg'))))
126− block <- asReturnBlock <$> valueToCpp body
127− let fn' = CppFunction
129− [(identToCpp arg, Just $ CppAny [CppConst, CppRef])]
135 declToCpp vqs ident (Abs (_, com, ty, _) arg body) = do
121 declToCpp vqs ident (Abs (_, com, ty, _) arg body) = do
136− fn <- if argcnt > 0 && CppTopLevel `elem` vqs
122+ fn <- if argcnt > 1 && CppTopLevel `elem` vqs
138 argNames <- replicateM (argcnt - length args' - 1) freshName
124 argNames <- replicateM (argcnt - length args' - 1) freshName
139 let args'' = zip argNames (repeat aty)
125 let args'' = zip argNames (repeat aty)
@@ -231,10 +217,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
231 block <- valueToCpp $
217 block <- valueToCpp $
232 curriedApp
218 curriedApp
233 (tail argNames)
219 (tail argNames)
235− (Nothing, [], Just ty, Nothing)
237− (Var nullAnn . Qualified Nothing . Ident $ head argNames))
220+ (App nullAnn e (Var nullAnn . Qualified Nothing . Ident $ head argNames))
238 let fn' = CppFunction
221 let fn' = CppFunction
239 (curriedName name)
222 (curriedName name)
240 [(head argNames, Just $ CppAny [CppConst, CppRef])]
223 [(head argNames, Just $ CppAny [CppConst, CppRef])]
@@ -245,15 +228,16 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
245 (tail argNames)
228 (tail argNames)
246 (CppApp (CppVar name)
229 (CppApp (CppVar name)
247 (CppVar <$> argNames)))
230 (CppVar <$> argNames)))
248− return $ CppNamespace ""
231+ return $ CppNamespace "" $
249 [ CppFunction
232 [ CppFunction
251 (zip argNames $ repeat aty)
234 (zip argNames $ repeat aty)
254 (asReturnBlock block)
237 (asReturnBlock block)
255− , fn'
239+ then [fn']
258 name = identToCpp ident
242 name = identToCpp ident
259 aty = Just $ CppAny [CppConst, CppRef]
243 aty = Just $ CppAny [CppConst, CppRef]
@@ -362,10 +346,10 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
362 valueToCpp :: Expr Ann -> m Cpp
346 valueToCpp :: Expr Ann -> m Cpp
363 -------------------------------------------------------------------------------------------------
347 -------------------------------------------------------------------------------------------------
364 valueToCpp (Var (_, _, _, Just (IsConstructor _ [])) ident) =
348 valueToCpp (Var (_, _, _, Just (IsConstructor _ [])) ident) =
365− return $ CppApp (CppVar $ qualifiedToStr' id ident) []
349+ return $ CppApp (varToCpp ident) []
367 valueToCpp (Var (_, _, _, Just (IsConstructor _ [_])) ident) =
351 valueToCpp (Var (_, _, _, Just (IsConstructor _ [_])) ident) =
368− return . CppVar $ qualifiedToStr' id ident
352+ return $ varToCpp ident
370 valueToCpp (Var (_, _, _, Just (IsConstructor _ _)) ident) =
354 valueToCpp (Var (_, _, _, Just (IsConstructor _ _)) ident) =
371 return . curriedName' $ varToCpp ident
355 return . curriedName' $ varToCpp ident
@@ -437,14 +421,23 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
437 Var (_, _, _, Just IsTypeClassConstructor) (Qualified mn' (Ident classname)) ->
421 Var (_, _, _, Just IsTypeClassConstructor) (Qualified mn' (Ident classname)) ->
438 let Just (_, constraints, fns) = findClass (Qualified mn' (ProperName classname)) in
422 let Just (_, constraints, fns) = findClass (Qualified mn' (ProperName classname)) in
439 return . CppObjectLiteral $ zip ((sort $ superClassDictionaryNames constraints) ++ (fst <$> fns)) args'
423 return . CppObjectLiteral $ zip ((sort $ superClassDictionaryNames constraints) ++ (fst <$> fns)) args'
440− Var (_, _, Just ty, _) (Qualified (Just _) _)
424+ Var ann (Qualified (Just mn') ident)
441− | argcnt <- countArgs ty,
425+ | argcnt <- maybe 0 countArgs ty,
442− argcnt > 1,
426+ argcnt > 1 -> do
443− length args >= argcnt -> do
444 f' <- valueToCpp f
427 f' <- valueToCpp f
428+ let (uncurriedArgs, curriedArgs) =
429+ if length args' >= argcnt
445− let (uncurArgs, curArgs) = splitAt argcnt args'
430+ then splitAt argcnt args'
433+ if null uncurriedArgs
434+ then curriedName' f'
446− uncurApp = CppApp (uncurried' f') uncurArgs
435+ else CppApp (uncurried' f') uncurriedArgs
447− return $ foldl (\fn a -> CppApp fn [a]) uncurApp curArgs
436+ return $ foldl (\fn a -> CppApp fn [a]) fn' curriedArgs
438+ ty | (_, _, Just t, _) <- ann = Just t
439+ | Just (t, _, _) <- M.lookup (mn', ident) (E.names env) = Just t
440+ | otherwise = Nothing
449 flip (foldl (\fn a -> CppApp fn [a])) args' <$> valueToCpp f
442 flip (foldl (\fn a -> CppApp fn [a])) args' <$> valueToCpp f
@@ -788,18 +781,17 @@ curriedApp args vals = foldl (\val arg -> App
788---------------------------------------------------------------------------------------------------
781---------------------------------------------------------------------------------------------------
789countArgs :: T.Type -> Int
782countArgs :: T.Type -> Int
790---------------------------------------------------------------------------------------------------
783---------------------------------------------------------------------------------------------------
791−countArgs = go 0 . T.everywhereOnTypes rmForAll
784+countArgs = go 0
793− go argcnt (T.TypeApp (T.TypeApp fn _) (T.ConstrainedType _ _)) | fn == E.tyFunction = argcnt
786+ go argcnt (T.ForAll _ t _) = go argcnt t
794 go argcnt (T.TypeApp (T.TypeApp fn _) t) | fn == E.tyFunction = go (argcnt + 1) t
787 go argcnt (T.TypeApp (T.TypeApp fn _) t) | fn == E.tyFunction = go (argcnt + 1) t
788+ go argcnt (T.ConstrainedType ts t) = go (argcnt + length ts) t
795 go argcnt _ = argcnt
789 go argcnt _ = argcnt
796− rmForAll :: T.Type -> T.Type
797− rmForAll (T.ForAll _ t _) = t
800---------------------------------------------------------------------------------------------------
791---------------------------------------------------------------------------------------------------
801curriedName :: String -> String
792curriedName :: String -> String
802---------------------------------------------------------------------------------------------------
793---------------------------------------------------------------------------------------------------
794+curriedName name@('$' : _) = name
803curriedName name = '$' : name
795curriedName name = '$' : name
805---------------------------------------------------------------------------------------------------
797---------------------------------------------------------------------------------------------------