@@ -13,7 +13,7 @@
1313--
1414-----------------------------------------------------------------------------
1515
16−{-# LANGUAGE GADTs #-}
16+{-# LANGUAGE GADTs, ViewPatterns #-}
1717
1818module Language.PureScript.CodeGen.JS (
1919 module AST,
@@ -23,7 +23,7 @@ module Language.PureScript.CodeGen.JS (
2323) where
2424
2525import Data.List ((\\), delete)
26−import Data.Maybe (catMaybes, mapMaybe)
26+import Data.Maybe (mapMaybe)
2727
2828import Control.Applicative
2929import Control.Arrow ((&&&))
@@ -48,7 +48,7 @@ moduleToJs opts (Module name imps exps foreigns decls) = do
4848 let jsImports = map (importToJs opts) . delete (ModuleName [ProperName C.prim]) . (\\ [name]) $ imps
4949 let foreigns' = mapMaybe (\(_, js, _) -> js) foreigns
5050 jsDecls <- mapM (bindToJs name) decls
51− let optimized = concatMap (map $ optimize opts) $ catMaybes jsDecls
51+ let optimized = concatMap (map $ optimize opts) jsDecls
5252 let isModuleEmpty = null exps
5353 let moduleBody = JSStringLiteral "use strict" : jsImports ++ foreigns' ++ optimized
5454 let exps' = JSObjectLiteral $ map (runIdent &&& JSVar . identToJs) exps
@@ -76,15 +76,23 @@ importToJs opts mn =
7676-- |
7777-- Generate code in the simplified Javascript intermediate representation for a declaration
7878--
79−bindToJs :: (Functor m, Applicative m, Monad m) => ModuleName -> Bind Ann -> SupplyT m (Maybe [JS])
80−bindToJs mp (NonRec ident val) = do
79+bindToJs :: (Functor m, Applicative m, Monad m) => ModuleName -> Bind Ann -> SupplyT m [JS]
80+bindToJs mp (NonRec ident val) = return <$> nonRecToJS mp ident val
81+bindToJs mp (Rec vals) = forM vals (uncurry (nonRecToJS mp))
82+
83+-- |
84+-- Generate code in the simplified Javascript intermediate representation for a single non-recursive
85+-- declaration.
86+--
87+-- The main purpose of this function is to handle code generation for comments.
88+--
89+nonRecToJS :: (Functor m, Applicative m, Monad m) => ModuleName -> Ident -> Expr Ann -> SupplyT m JS
90+nonRecToJS m i e@(extractAnn -> (_, com, _, _)) | not (null com) =
91+ JSComment com <$> nonRecToJS m i (modifyAnn removeComments e)
92+nonRecToJS mp ident val = do
8193 js <- valueToJs mp val
82− return $ Just [JSVariableIntroduction (identToJs ident) (Just js)]
83−bindToJs mp (Rec vals) = do
84− jss <- forM vals $ \(ident, val) -> do
85− js <- valueToJs mp val
86− return $ JSVariableIntroduction (identToJs ident) (Just js)
87− return $ Just jss
94+ return $ JSVariableIntroduction (identToJs ident) (Just js)
95+
8896
8997-- |
9098-- Generate code in the simplified Javascript intermediate representation for a variable based on a
@@ -155,7 +163,7 @@ valueToJs m (Case _ values binders) = do
155163 vals <- mapM (valueToJs m) values
156164 bindersToJs m binders vals
157165valueToJs m (Let _ ds val) = do
158− decls <- concat . catMaybes <$> mapM (bindToJs m) ds
166+ decls <- concat <$> mapM (bindToJs m) ds
159167 ret <- valueToJs m val
160168 return $ JSApp (JSFunction Nothing [] (JSBlock (decls ++ [JSReturn ret]))) []
161169valueToJs _ (Constructor (_, _, _, Just IsNewtype) _ (ProperName ctor) _) =