@@ -13,7 +13,7 @@
14-----------------------------------------------------------------------------
14-----------------------------------------------------------------------------
16−{-# LANGUAGE GADTs #-}
16+{-# LANGUAGE GADTs, ViewPatterns #-}
18module Language.PureScript.CodeGen.JS (
18module Language.PureScript.CodeGen.JS (
19 module AST,
19 module AST,
@@ -23,7 +23,7 @@ module Language.PureScript.CodeGen.JS (
25import Data.List ((\\), delete)
25import Data.List ((\\), delete)
26−import Data.Maybe (catMaybes, mapMaybe)
26+import Data.Maybe (mapMaybe)
28import Control.Applicative
28import Control.Applicative
29import Control.Arrow ((&&&))
29import Control.Arrow ((&&&))
@@ -48,7 +48,7 @@ moduleToJs opts (Module name imps exps foreigns decls) = do
48 let jsImports = map (importToJs opts) . delete (ModuleName [ProperName C.prim]) . (\\ [name]) $ imps
48 let jsImports = map (importToJs opts) . delete (ModuleName [ProperName C.prim]) . (\\ [name]) $ imps
49 let foreigns' = mapMaybe (\(_, js, _) -> js) foreigns
49 let foreigns' = mapMaybe (\(_, js, _) -> js) foreigns
50 jsDecls <- mapM (bindToJs name) decls
50 jsDecls <- mapM (bindToJs name) decls
51− let optimized = concatMap (map $ optimize opts) $ catMaybes jsDecls
51+ let optimized = concatMap (map $ optimize opts) jsDecls
52 let isModuleEmpty = null exps
52 let isModuleEmpty = null exps
53 let moduleBody = JSStringLiteral "use strict" : jsImports ++ foreigns' ++ optimized
53 let moduleBody = JSStringLiteral "use strict" : jsImports ++ foreigns' ++ optimized
54 let exps' = JSObjectLiteral $ map (runIdent &&& JSVar . identToJs) exps
54 let exps' = JSObjectLiteral $ map (runIdent &&& JSVar . identToJs) exps
@@ -76,15 +76,23 @@ importToJs opts mn =
77-- Generate code in the simplified Javascript intermediate representation for a declaration
77-- Generate code in the simplified Javascript intermediate representation for a declaration
79−bindToJs :: (Functor m, Applicative m, Monad m) => ModuleName -> Bind Ann -> SupplyT m (Maybe [JS])
79+bindToJs :: (Functor m, Applicative m, Monad m) => ModuleName -> Bind Ann -> SupplyT m [JS]
80−bindToJs mp (NonRec ident val) = do
80+bindToJs mp (NonRec ident val) = return <$> nonRecToJS mp ident val
81+bindToJs mp (Rec vals) = forM vals (uncurry (nonRecToJS mp))
84+-- Generate code in the simplified Javascript intermediate representation for a single non-recursive
87+-- The main purpose of this function is to handle code generation for comments.
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
81 js <- valueToJs mp val
93 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)
94+ return $ JSVariableIntroduction (identToJs ident) (Just js)
90-- Generate code in the simplified Javascript intermediate representation for a variable based on a
98-- Generate code in the simplified Javascript intermediate representation for a variable based on a
@@ -155,7 +163,7 @@ valueToJs m (Case _ values binders) = do
155 vals <- mapM (valueToJs m) values
163 vals <- mapM (valueToJs m) values
156 bindersToJs m binders vals
164 bindersToJs m binders vals
157valueToJs m (Let _ ds val) = do
165valueToJs m (Let _ ds val) = do
158− decls <- concat . catMaybes <$> mapM (bindToJs m) ds
166+ decls <- concat <$> mapM (bindToJs m) ds
159 ret <- valueToJs m val
167 ret <- valueToJs m val
160 return $ JSApp (JSFunction Nothing [] (JSBlock (decls ++ [JSReturn ret]))) []
168 return $ JSApp (JSFunction Nothing [] (JSBlock (decls ++ [JSReturn ret]))) []
161valueToJs _ (Constructor (_, _, _, Just IsNewtype) _ (ProperName ctor) _) =
169valueToJs _ (Constructor (_, _, _, Just IsNewtype) _ (ProperName ctor) _) =