@@ -52,8 +52,6 @@ import qualified Language.PureScript.Environment as E
5252import qualified Language.PureScript.Pretty.Cpp as P
5353import qualified Language.PureScript.Types as T
5454
55−-- import Debug.Trace
56−
5755-- |
5856-- Generate code in the simplified C++1x intermediate representation for all declarations in a
5957-- module.
@@ -120,20 +118,8 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
120118 declToCpp _ (Op _) (Var (_, _, Nothing, _) _) =
121119 return CppNoOp -- skip operator aliases
122120
123− declToCpp _ ident (Abs _ arg@(Ident "dict")
124− body@(Accessor _ _ (Var _ (Qualified Nothing arg'))))
125− | arg' == arg = do
126− block <- asReturnBlock <$> valueToCpp body
127− let fn' = CppFunction
128− (identToCpp ident)
129− [(identToCpp arg, Just $ CppAny [CppConst, CppRef])]
130− (Just $ CppAny [])
131− []
132− block
133− return fn'
134−
135121 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
137123 then do
138124 argNames <- replicateM (argcnt - length args' - 1) freshName
139125 let args'' = zip argNames (repeat aty)
@@ -231,10 +217,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
231217 block <- valueToCpp $
232218 curriedApp
233219 (tail argNames)
234− (App
235− (Nothing, [], Just ty, Nothing)
236− e
237− (Var nullAnn . Qualified Nothing . Ident $ head argNames))
220+ (App nullAnn e (Var nullAnn . Qualified Nothing . Ident $ head argNames))
238221 let fn' = CppFunction
239222 (curriedName name)
240223 [(head argNames, Just $ CppAny [CppConst, CppRef])]
@@ -245,15 +228,16 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
245228 (tail argNames)
246229 (CppApp (CppVar name)
247230 (CppVar <$> argNames)))
248− return $ CppNamespace ""
231+ return $ CppNamespace "" $
249232 [ CppFunction
250233 name
251234 (zip argNames $ repeat aty)
252235 rty
253236 []
254237 (asReturnBlock block)
255− , fn'
256− ]
238+ ] ++ if argcnt > 1
239+ then [fn']
240+ else []
257241 where
258242 name = identToCpp ident
259243 aty = Just $ CppAny [CppConst, CppRef]
@@ -362,10 +346,10 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
362346 valueToCpp :: Expr Ann -> m Cpp
363347 -------------------------------------------------------------------------------------------------
364348 valueToCpp (Var (_, _, _, Just (IsConstructor _ [])) ident) =
365− return $ CppApp (CppVar $ qualifiedToStr' id ident) []
349+ return $ CppApp (varToCpp ident) []
366350
367351 valueToCpp (Var (_, _, _, Just (IsConstructor _ [_])) ident) =
368− return . CppVar $ qualifiedToStr' id ident
352+ return $ varToCpp ident
369353
370354 valueToCpp (Var (_, _, _, Just (IsConstructor _ _)) ident) =
371355 return . curriedName' $ varToCpp ident
@@ -437,14 +421,23 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
437421 Var (_, _, _, Just IsTypeClassConstructor) (Qualified mn' (Ident classname)) ->
438422 let Just (_, constraints, fns) = findClass (Qualified mn' (ProperName classname)) in
439423 return . CppObjectLiteral $ zip ((sort $ superClassDictionaryNames constraints) ++ (fst <$> fns)) args'
440− Var (_, _, Just ty, _) (Qualified (Just _) _)
441− | argcnt <- countArgs ty,
442− argcnt > 1,
443− length args >= argcnt -> do
424+ Var ann (Qualified (Just mn') ident)
425+ | argcnt <- maybe 0 countArgs ty,
426+ argcnt > 1 -> do
444427 f' <- valueToCpp f
445− let (uncurArgs, curArgs) = splitAt argcnt args'
446− uncurApp = CppApp (uncurried' f') uncurArgs
447− return $ foldl (\fn a -> CppApp fn [a]) uncurApp curArgs
428+ let (uncurriedArgs, curriedArgs) =
429+ if length args' >= argcnt
430+ then splitAt argcnt args'
431+ else ([], args')
432+ fn' =
433+ if null uncurriedArgs
434+ then curriedName' f'
435+ else CppApp (uncurried' f') uncurriedArgs
436+ return $ foldl (\fn a -> CppApp fn [a]) fn' curriedArgs
437+ where
438+ ty | (_, _, Just t, _) <- ann = Just t
439+ | Just (t, _, _) <- M.lookup (mn', ident) (E.names env) = Just t
440+ | otherwise = Nothing
448441 _ ->
449442 flip (foldl (\fn a -> CppApp fn [a])) args' <$> valueToCpp f
450443
@@ -788,18 +781,17 @@ curriedApp args vals = foldl (\val arg -> App
788781---------------------------------------------------------------------------------------------------
789782countArgs :: T.Type -> Int
790783---------------------------------------------------------------------------------------------------
791−countArgs = go 0 . T.everywhereOnTypes rmForAll
784+countArgs = go 0
792785 where
793− go argcnt (T.TypeApp (T.TypeApp fn _) (T.ConstrainedType _ _)) | fn == E.tyFunction = argcnt
786+ go argcnt (T.ForAll _ t _) = go argcnt t
794787 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
795789 go argcnt _ = argcnt
796− rmForAll :: T.Type -> T.Type
797− rmForAll (T.ForAll _ t _) = t
798− rmForAll t = t
799790
800791---------------------------------------------------------------------------------------------------
801792curriedName :: String -> String
802793---------------------------------------------------------------------------------------------------
794+curriedName name@('$' : _) = name
803795curriedName name = '$' : name
804796
805797---------------------------------------------------------------------------------------------------