@@ -52,8 +52,8 @@ moduleToJs opts (Module pname@(ProperName name) decls) env =
52 [ JSVariableIntroduction name Nothing
52 [ JSVariableIntroduction name Nothing
53 , JSApp (JSFunction Nothing [name]
53 , JSApp (JSFunction Nothing [name]
54 (JSBlock (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts (ModuleName pname) decl env) (sortBy typeClassesLast decls))))
54 (JSBlock (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts (ModuleName pname) decl env) (sortBy typeClassesLast decls))))
55− [JSAssignment (JSAssignVariable name)
55+ [JSAssignment (JSVar name)
56− (JSBinary Or (JSVar name) (JSObjectLiteral []))]
56+ (JSBinary Or (JSVar name) (JSObjectLiteral []))]
59 typeClassesLast (ExternDeclaration TypeClassDictionaryImport _ _ _) (ExternDeclaration TypeClassDictionaryImport _ _ _) = EQ
59 typeClassesLast (ExternDeclaration TypeClassDictionaryImport _ _ _) (ExternDeclaration TypeClassDictionaryImport _ _ _) = EQ
@@ -66,12 +66,12 @@ moduleToJs opts (Module pname@(ProperName name) decls) env =
67declToJs :: Options -> ModuleName -> Declaration -> Environment -> Maybe [JS]
67declToJs :: Options -> ModuleName -> Declaration -> Environment -> Maybe [JS]
68declToJs opts mp (ValueDeclaration ident _ _ val) e =
68declToJs opts mp (ValueDeclaration ident _ _ val) e =
69− Just [ JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val)),
69+ Just $ JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val))
70− setProperty (identToJs ident) (var ident) mp ]
70+ : setProperty ident (var ident) mp
71declToJs opts mp (BindingGroupDeclaration vals) e =
71declToJs opts mp (BindingGroupDeclaration vals) e =
72 Just $ concatMap (\(ident, val) ->
72 Just $ concatMap (\(ident, val) ->
73− [ JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val)),
73+ JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val))
74− setProperty (identToJs ident) (var ident) mp ]
74+ : setProperty ident (var ident) mp
76declToJs _ mp (DataDeclaration _ _ ctors) _ =
76declToJs _ mp (DataDeclaration _ _ ctors) _ =
77 Just $ flip concatMap ctors $ \(pn@(ProperName ctor), maybeTy) ->
77 Just $ flip concatMap ctors $ \(pn@(ProperName ctor), maybeTy) ->
@@ -83,19 +83,22 @@ declToJs _ mp (DataDeclaration _ _ ctors) _ =
83 (JSBlock [JSReturn
83 (JSBlock [JSReturn
84 (JSObjectLiteral [ ("ctor", JSStringLiteral (show (Qualified (Just mp) pn)))
84 (JSObjectLiteral [ ("ctor", JSStringLiteral (show (Qualified (Just mp) pn)))
85 , ("value", JSVar "value") ])])
85 , ("value", JSVar "value") ])])
86− in [ ctorJs, setProperty ctor (JSVar ctor) mp ]
86+ in ctorJs : setProperty (Escaped ctor) (JSVar ctor) mp
87declToJs opts mp (DataBindingGroupDeclaration ds) e =
87declToJs opts mp (DataBindingGroupDeclaration ds) e =
88 Just $ concat $ mapMaybe (flip (declToJs opts mp) e) ds
88 Just $ concat $ mapMaybe (flip (declToJs opts mp) e) ds
89declToJs _ mp (ExternDeclaration importTy ident (Just js) _) _ =
89declToJs _ mp (ExternDeclaration importTy ident (Just js) _) _ =
91− , setProperty (identToJs ident) (var ident) mp ]
90+ Just $ js : setProperty ident (var ident) mp
92declToJs _ _ _ _ = Nothing
91declToJs _ _ _ _ = Nothing
94−setProperty :: String -> JS -> ModuleName -> JS
93+setProperty :: Ident -> JS -> ModuleName -> [JS]
95−setProperty prop val (ModuleName (ProperName moduleName)) = JSAssignment (JSAssignProperty prop (JSAssignVariable moduleName)) val
94+setProperty ident@(Op op) val (ModuleName (ProperName moduleName)) =
95+ [ JSAssignment (JSAccessor (identToJs ident) (JSVar moduleName)) val
96+ , JSAssignment (JSIndexer (JSStringLiteral op) (JSVar moduleName)) (JSAccessor (identToJs ident) (JSVar moduleName)) ]
97+setProperty ident val (ModuleName (ProperName moduleName)) =
98+ [ JSAssignment (JSAccessor (identToJs ident) (JSVar moduleName)) val ]
97var :: Ident -> JS
100var :: Ident -> JS
98−var name = JSVar $ identToJs name
101+var = JSVar . identToJs
100valueToJs :: Options -> ModuleName -> Environment -> Value -> JS
103valueToJs :: Options -> ModuleName -> Environment -> Value -> JS
101valueToJs _ _ _ (NumericLiteral n) = JSNumericLiteral n
104valueToJs _ _ _ (NumericLiteral n) = JSNumericLiteral n
@@ -154,13 +157,15 @@ runtimeTypeChecks arg ty =
154 arrayCheck js = JSIfElse (JSUnary Not (JSApp (JSAccessor "isArray" (JSVar "Array")) [js])) (JSBlock [JSThrow (JSStringLiteral "Array expected")]) Nothing
157 arrayCheck js = JSIfElse (JSUnary Not (JSApp (JSAccessor "isArray" (JSVar "Array")) [js])) (JSBlock [JSThrow (JSStringLiteral "Array expected")]) Nothing
156varToJs :: ModuleName -> Environment -> Qualified Ident -> JS
159varToJs :: ModuleName -> Environment -> Qualified Ident -> JS
157−varToJs m e qual@(Qualified _ ident) = case M.lookup (qualify m qual) (names e) of
160+varToJs m e qual@(Qualified _ ident) = go qual
158− Just (_, ty) | isExtern ty -> var ident
159− Just (_, Alias aliasModule aliasIdent) -> qualifiedToJS identToJs (Qualified (Just aliasModule) aliasIdent)
161− Qualified Nothing _ -> var ident
162− _ -> qualifiedToJS identToJs qual
162+ go qual = case M.lookup (qualify m qual) (names e) of
163+ Just (_, ty) | isExtern ty -> var ident
164+ Just (_, Alias aliasModule aliasIdent) -> go (Qualified (Just aliasModule) aliasIdent)
166+ Qualified Nothing _ -> var ident
167+ Qualified (Just (ModuleName (ProperName mn))) (Op op) -> JSIndexer (JSStringLiteral op) (JSVar mn)
168+ _ -> qualifiedToJS identToJs qual
164 isExtern (Extern ForeignImport) = True
169 isExtern (Extern ForeignImport) = True
165 isExtern (Alias m' ident') = case M.lookup (m', ident') (names e) of
170 isExtern (Alias m' ident') = case M.lookup (m', ident') (names e) of
166 Just (_, ty') -> isExtern ty'
171 Just (_, ty') -> isExtern ty'
@@ -263,7 +268,7 @@ isOnlyConstructor m e ctor =
264statementToJs :: Options -> ModuleName -> Environment -> Statement -> JS
269statementToJs :: Options -> ModuleName -> Environment -> Statement -> JS
265statementToJs opts m e (VariableIntroduction ident value) = JSVariableIntroduction (identToJs ident) (Just (valueToJs opts m e value))
270statementToJs opts m e (VariableIntroduction ident value) = JSVariableIntroduction (identToJs ident) (Just (valueToJs opts m e value))
266−statementToJs opts m e (Assignment target value) = JSAssignment (JSAssignVariable (identToJs target)) (valueToJs opts m e value)
271+statementToJs opts m e (Assignment target value) = JSAssignment (JSVar (identToJs target)) (valueToJs opts m e value)
267statementToJs opts m e (While cond sts) = JSWhile (valueToJs opts m e cond) (JSBlock (map (statementToJs opts m e) sts))
272statementToJs opts m e (While cond sts) = JSWhile (valueToJs opts m e cond) (JSBlock (map (statementToJs opts m e) sts))
268statementToJs opts m e (For ident start end sts) = JSFor (identToJs ident) (valueToJs opts m e start) (valueToJs opts m e end) (JSBlock (map (statementToJs opts m e) sts))
273statementToJs opts m e (For ident start end sts) = JSFor (identToJs ident) (valueToJs opts m e start) (valueToJs opts m e end) (JSBlock (map (statementToJs opts m e) sts))
269statementToJs opts m e (If ifst) = ifToJs ifst
274statementToJs opts m e (If ifst) = ifToJs ifst