@@ -68,7 +68,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
68 delete (ModuleName [ProperName C.prim]) . (\\ [mn]) $
68 delete (ModuleName [ProperName C.prim]) . (\\ [mn]) $
70 let cppImports' = "PureScript" : cppImports
70 let cppImports' = "PureScript" : cppImports
71− cppDecls <- concat <$> mapM bindToCpp decls
71+ cppDecls <- concat <$> mapM (bindToCpp [CppTopLevel]) decls
72 optimized <- traverse optimize cppDecls
72 optimized <- traverse optimize cppDecls
73 let moduleHeader =
73 let moduleHeader =
74 fileBegin mn "HH" ++
74 fileBegin mn "HH" ++
@@ -101,10 +101,10 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
101 -- Generate code in the simplified C++1x intermediate representation for a declaration
101 -- Generate code in the simplified C++1x intermediate representation for a declaration
103 -------------------------------------------------------------------------------------------------
103 -------------------------------------------------------------------------------------------------
104− bindToCpp :: Bind Ann -> m [Cpp]
104+ bindToCpp :: [CppValueQual] -> Bind Ann -> m [Cpp]
105 -------------------------------------------------------------------------------------------------
105 -------------------------------------------------------------------------------------------------
106− bindToCpp (NonRec ident val) = return <$> declToCpp [] ident val
106+ bindToCpp qs (NonRec ident val) = return <$> declToCpp qs ident val
107− bindToCpp (Rec vals) = forM vals (uncurry $ declToCpp [CppRecursive])
107+ bindToCpp qs (Rec vals) = forM vals (uncurry $ declToCpp (CppRecursive:qs))
110 -- Desugar a declaration into a variable introduction or named function
110 -- Desugar a declaration into a variable introduction or named function
@@ -177,6 +177,33 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
177 | otherwise = fullyConstructed
177 | otherwise = fullyConstructed
178 fullyConstructed = [CppReturn (CppDataLiteral (CppStringLiteral name : (CppVar <$> fields')))]
178 fullyConstructed = [CppReturn (CppDataLiteral (CppStringLiteral name : (CppVar <$> fields')))]
180+ declToCpp qs ident e@(App (_, _, Just ty, _) _ _)
181+ | CppTopLevel `elem` qs,
182+ count <- countArgs ty,
184+ argNames <- replicateM count freshName
185+ block <- flip (foldl (\fn a -> CppApp fn [a])) (CppVar <$> argNames) <$> valueToCpp e
186+ return $ CppFunction
188+ [(head argNames, Just $ CppAny [CppConst, CppRef])]
191+ (asReturnBlock $ curried (tail argNames) block)
193+ curried :: [String] -> Cpp -> Cpp
194+ curried args vals = foldl (\val arg -> CppLambda
196+ [(arg, Just $ CppAny [CppConst, CppRef])]
200+ countArgs :: T.Type -> Int
203+ go argcnt (T.ForAll _ t _) = go argcnt t
204+ go argcnt (T.TypeApp (T.TypeApp fn _) t) | fn == E.tyFunction = go (argcnt + 1) t
205+ go argcnt _ = argcnt
180 declToCpp _ ident val = do
207 declToCpp _ ident val = do
181 val' <- valueToCpp val
208 val' <- valueToCpp val
@@ -351,7 +378,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
351 bindersToCpp maybeSpan ty binders vals
378 bindersToCpp maybeSpan ty binders vals
353 valueToCpp (Let _ ds val) = do
380 valueToCpp (Let _ ds val) = do
354− ds' <- concat <$> mapM bindToCpp ds
381+ ds' <- concat <$> mapM (bindToCpp []) ds
355 ret <- valueToCpp val
382 ret <- valueToCpp val
356 let ds'' = tco defaultOptions <$> ds'
383 let ds'' = tco defaultOptions <$> ds'
357 let rs = if hasRecursion ds''
384 let rs = if hasRecursion ds''