1414
1515module Language.PureScript.CodeGen.JS (
1616 module AST ,
17- declToJs
17+ declToJs ,
18+ moduleToJs
1819) where
1920
2021import Data.Maybe (mapMaybe )
@@ -31,52 +32,48 @@ import Language.PureScript.Pretty.Common
3132import Language.PureScript.CodeGen.Monad
3233import Language.PureScript.CodeGen.JS.AST as AST
3334import Language.PureScript.TypeChecker.Monad (NameKind (.. ))
34- import Debug.Trace (trace )
3535
36- declToJs :: Maybe Ident -> ModulePath -> Declaration -> Environment -> Maybe [JS ]
37- declToJs curMod mp (ValueDeclaration ident _ _ (Abs args ret)) e =
38- Just $ JSFunction (Just ident) args (JSBlock [JSReturn (valueToJs mp e ret)]) :
39- maybe [] (return . setProperty (identToJs ident) (JSVar ident)) curMod
40- declToJs curMod mp (ValueDeclaration ident _ _ val) e =
41- Just $ JSVariableIntroduction ident (Just (valueToJs mp e val)) :
42- maybe [] (return . setProperty (identToJs ident) (JSVar ident)) curMod
43- declToJs curMod mp (BindingGroupDeclaration vals) e = trace (show [ JSApp (JSFunction Nothing [] (JSBlock (concatMap (\ (ident, val) ->
44- JSVariableIntroduction ident (Just (valueToJs mp e val)) :
45- maybe [] (return . setProperty (identToJs ident) (JSVar ident)) curMod
46- ) vals))) []
47- ]) $
48- Just [ JSApp (JSFunction Nothing [] (JSBlock (concatMap (\ (ident, val) ->
49- JSVariableIntroduction ident (Just (valueToJs mp e val)) :
50- maybe [] (return . setProperty (identToJs ident) (JSVar ident)) curMod
51- ) vals))) []
52- ]
53- declToJs curMod _ (ExternMemberDeclaration member ident _) _ =
54- Just $ JSFunction (Just ident) [Ident " value" ] (JSBlock [JSReturn (JSAccessor member (JSVar (Ident " value" )))]) :
55- maybe [] (return . setProperty (show ident) (JSVar ident)) curMod
56- declToJs curMod mp (DataDeclaration _ _ ctors) _ =
36+ moduleToJs :: Module -> Environment -> [JS ]
37+ moduleToJs (Module pname@ (ProperName name) decls) env =
38+ [ JSVariableIntroduction (Ident name) Nothing
39+ , JSApp (JSFunction Nothing [Ident name]
40+ (JSBlock (concat $ mapMaybe (\ decl -> declToJs (ModuleName pname) decl env) decls)))
41+ [JSAssignment (JSAssignVariable (Ident name))
42+ (JSBinary Or (JSVar (Ident name)) (JSObjectLiteral [] ))]
43+ ]
44+
45+ declToJs :: ModuleName -> Declaration -> Environment -> Maybe [JS ]
46+ declToJs mp (ValueDeclaration ident _ _ (Abs args ret)) e =
47+ Just [ JSFunction (Just ident) args (JSBlock [JSReturn (valueToJs mp e ret)]),
48+ setProperty (identToJs ident) (JSVar ident) mp ]
49+ declToJs mp (ValueDeclaration ident _ _ val) e =
50+ Just [ JSVariableIntroduction ident (Just (valueToJs mp e val)),
51+ setProperty (identToJs ident) (JSVar ident) mp ]
52+ declToJs mp (BindingGroupDeclaration vals) e =
53+ Just $ concatMap (\ (ident, val) ->
54+ [ JSVariableIntroduction ident (Just (valueToJs mp e val)),
55+ setProperty (identToJs ident) (JSVar ident) mp ]
56+ ) vals
57+ declToJs mp (ExternMemberDeclaration member ident _) _ =
58+ Just [ JSFunction (Just ident) [Ident " value" ] (JSBlock [JSReturn (JSAccessor member (JSVar (Ident " value" )))]),
59+ setProperty (show ident) (JSVar ident) mp ]
60+ declToJs mp (DataDeclaration _ _ ctors) _ =
5761 Just $ flip concatMap ctors $ \ (pn@ (ProperName ctor), maybeTy) ->
5862 let
5963 ctorJs =
6064 case maybeTy of
61- Nothing -> JSVariableIntroduction (Ident ctor) (Just (JSObjectLiteral [ (" ctor" , JSStringLiteral (show (Qualified mp pn))) ]))
65+ Nothing -> JSVariableIntroduction (Ident ctor) (Just (JSObjectLiteral [ (" ctor" , JSStringLiteral (show (Qualified ( Just mp) pn))) ]))
6266 Just _ -> JSFunction (Just (Ident ctor)) [Ident " value" ]
6367 (JSBlock [JSReturn
64- (JSObjectLiteral [ (" ctor" , JSStringLiteral (show (Qualified mp pn)))
68+ (JSObjectLiteral [ (" ctor" , JSStringLiteral (show (Qualified ( Just mp) pn)))
6569 , (" value" , JSVar (Ident " value" )) ])])
66- in ctorJs : maybe [] (return . setProperty ctor (JSVar (Ident ctor))) curMod
67- declToJs curMod mp (ModuleDeclaration pn@ (ProperName name) decls) env =
68- Just $ [ JSVariableIntroduction (Ident name) Nothing
69- , JSApp (JSFunction Nothing [Ident name]
70- (JSBlock (concat $ mapMaybe (\ decl -> declToJs (Just (Ident name)) (subModule mp pn) decl env) decls)))
71- [JSAssignment (JSAssignVariable (Ident name))
72- (JSBinary Or (JSVar (Ident name)) (JSObjectLiteral [] ))]] ++
73- maybe [] (return . setProperty name (JSVar (Ident name))) curMod
74- declToJs _ _ _ _ = Nothing
70+ in [ ctorJs, setProperty ctor (JSVar (Ident ctor)) mp ]
71+ declToJs _ _ _ = Nothing
7572
76- setProperty :: String -> JS -> Ident -> JS
77- setProperty prop val curMod = JSAssignment (JSAssignProperty prop (JSAssignVariable curMod )) val
73+ setProperty :: String -> JS -> ModuleName -> JS
74+ setProperty prop val ( ModuleName ( ProperName moduleName)) = JSAssignment (JSAssignProperty prop (JSAssignVariable ( Ident moduleName) )) val
7875
79- valueToJs :: ModulePath -> Environment -> Value -> JS
76+ valueToJs :: ModuleName -> Environment -> Value -> JS
8077valueToJs _ _ (NumericLiteral n) = JSNumericLiteral n
8178valueToJs _ _ (StringLiteral s) = JSStringLiteral s
8279valueToJs _ _ (BooleanLiteral b) = JSBooleanLiteral b
@@ -94,19 +91,16 @@ valueToJs m e (Abs args val) = JSFunction Nothing args (JSBlock [JSReturn (value
9491valueToJs m e (Unary op val) = JSUnary op (valueToJs m e val)
9592valueToJs m e (Binary op v1 v2) = JSBinary op (valueToJs m e v1) (valueToJs m e v2)
9693valueToJs m e (Var ident) = case M. lookup (qualify m ident) (names e) of
97- Just (_, Alias aliasModule aliasIdent) -> qualifiedToJS identToJs (Qualified aliasModule aliasIdent)
94+ Just (_, Alias aliasModule aliasIdent) -> qualifiedToJS identToJs (Qualified ( Just aliasModule) aliasIdent)
9895 _ -> qualifiedToJS identToJs ident
9996valueToJs m e (TypedValue val _) = valueToJs m e val
10097valueToJs _ _ _ = error " Invalid argument to valueToJs"
10198
10299qualifiedToJS :: (a -> String ) -> Qualified a -> JS
103- qualifiedToJS f (Qualified (ModulePath parts) a) =
104- delimited (f a : reverse (map show parts))
105- where delimited [part] = JSVar (Ident (part))
106- delimited (part: parts') = JSAccessor part (delimited parts')
107- delimited _ = error " Invalid argument to delimited"
100+ qualifiedToJS f (Qualified (Just (ModuleName (ProperName m))) a) = JSAccessor (f a) (JSVar (Ident m))
101+ qualifiedToJS f (Qualified Nothing a) = JSVar (Ident (f a))
108102
109- bindersToJs :: ModulePath -> Environment -> [([Binder ], Maybe Guard , Value )] -> [JS ] -> Gen JS
103+ bindersToJs :: ModuleName -> Environment -> [([Binder ], Maybe Guard , Value )] -> [JS ] -> Gen JS
110104bindersToJs m e binders vals = do
111105 setNextName $ firstUnusedName (binders, vals)
112106 valNames <- replicateM (length vals) fresh
@@ -122,7 +116,7 @@ bindersToJs m e binders vals = do
122116 binderToJs m e v done'' b
123117 go _ _ _ _ = error " Invalid arguments to bindersToJs"
124118
125- binderToJs :: ModulePath -> Environment -> String -> [JS ] -> Binder -> Gen [JS ]
119+ binderToJs :: ModuleName -> Environment -> String -> [JS ] -> Binder -> Gen [JS ]
126120binderToJs _ _ _ done NullBinder = return done
127121binderToJs _ _ varName done (StringBinder str) =
128122 return [JSIfElse (JSBinary EqualTo (JSVar (Ident varName)) (JSStringLiteral str)) (JSBlock done) Nothing ]
@@ -135,11 +129,11 @@ binderToJs _ _ varName done (BooleanBinder False) =
135129binderToJs _ _ varName done (VarBinder ident) =
136130 return (JSVariableIntroduction ident (Just (JSVar (Ident varName))) : done)
137131binderToJs m _ varName done (NullaryBinder ctor) =
138- return [JSIfElse (JSBinary EqualTo (JSAccessor " ctor" (JSVar (Ident varName))) (JSStringLiteral (show (uncurry Qualified $ qualify m ctor)))) (JSBlock done) Nothing ]
132+ return [JSIfElse (JSBinary EqualTo (JSAccessor " ctor" (JSVar (Ident varName))) (JSStringLiteral (show (( \ (mp, nm) -> Qualified ( Just mp) nm) $ qualify m ctor)))) (JSBlock done) Nothing ]
139133binderToJs m e varName done (UnaryBinder ctor b) = do
140134 value <- fresh
141135 js <- binderToJs m e value done b
142- return [JSIfElse (JSBinary EqualTo (JSAccessor " ctor" (JSVar (Ident varName))) (JSStringLiteral (show (uncurry Qualified $ qualify m ctor)))) (JSBlock (JSVariableIntroduction (Ident value) (Just (JSAccessor " value" (JSVar (Ident varName)))) : js)) Nothing ]
136+ return [JSIfElse (JSBinary EqualTo (JSAccessor " ctor" (JSVar (Ident varName))) (JSStringLiteral (show (( \ (mp, nm) -> Qualified ( Just mp) nm) $ qualify m ctor)))) (JSBlock (JSVariableIntroduction (Ident value) (Just (JSAccessor " value" (JSVar (Ident varName)))) : js)) Nothing ]
143137binderToJs m e varName done (ObjectBinder bs) = go done bs
144138 where
145139 go :: [JS ] -> [(String , Binder )] -> Gen [JS ]
@@ -174,7 +168,7 @@ binderToJs m e varName done (NamedBinder ident binder) = do
174168 js <- binderToJs m e varName done binder
175169 return (JSVariableIntroduction ident (Just (JSVar (Ident varName))) : js)
176170
177- statementToJs :: ModulePath -> Environment -> Statement -> JS
171+ statementToJs :: ModuleName -> Environment -> Statement -> JS
178172statementToJs m e (VariableIntroduction ident value) = JSVariableIntroduction ident (Just (valueToJs m e value))
179173statementToJs m e (Assignment target value) = JSAssignment (JSAssignVariable target) (valueToJs m e value)
180174statementToJs m e (While cond sts) = JSWhile (valueToJs m e cond) (JSBlock (map (statementToJs m e) sts))
0 commit comments