@@ -66,11 +66,11 @@ moduleToJs opts (Module pname@(ProperName name) decls) env =
66declToJs :: Options -> ModuleName -> Declaration -> Environment -> Maybe [JS]
66declToJs :: Options -> ModuleName -> Declaration -> Environment -> Maybe [JS]
67declToJs opts mp (ValueDeclaration ident _ _ val) e =
67declToJs opts mp (ValueDeclaration ident _ _ val) e =
68 Just [ JSVariableIntroduction ident (Just (valueToJs opts mp e val)),
68 Just [ JSVariableIntroduction ident (Just (valueToJs opts mp e val)),
69− setProperty ident (JSVar ident) mp ]
69+ setProperty (identToJs ident) (JSVar ident) mp ]
70declToJs opts mp (BindingGroupDeclaration vals) e =
70declToJs opts mp (BindingGroupDeclaration vals) e =
71 Just $ concatMap (\(ident, val) ->
71 Just $ concatMap (\(ident, val) ->
72 [ JSVariableIntroduction ident (Just (valueToJs opts mp e val)),
72 [ JSVariableIntroduction ident (Just (valueToJs opts mp e val)),
73− setProperty ident (JSVar ident) mp ]
73+ setProperty (identToJs ident) (JSVar ident) mp ]
75declToJs _ mp (DataDeclaration _ _ ctors) _ =
75declToJs _ mp (DataDeclaration _ _ ctors) _ =
76 Just $ flip concatMap ctors $ \(pn@(ProperName ctor), maybeTy) ->
76 Just $ flip concatMap ctors $ \(pn@(ProperName ctor), maybeTy) ->
@@ -82,15 +82,15 @@ declToJs _ mp (DataDeclaration _ _ ctors) _ =
82 (JSBlock [JSReturn
82 (JSBlock [JSReturn
83 (JSObjectLiteral [ ("ctor", JSStringLiteral (show (Qualified (Just mp) pn)))
83 (JSObjectLiteral [ ("ctor", JSStringLiteral (show (Qualified (Just mp) pn)))
84 , ("value", JSVar (Ident "value")) ])])
84 , ("value", JSVar (Ident "value")) ])])
85− in [ ctorJs, setProperty (Ident ctor) (JSVar (Ident ctor)) mp ]
85+ in [ ctorJs, setProperty ctor (JSVar (Ident ctor)) mp ]
86declToJs opts mp (DataBindingGroupDeclaration ds) e =
86declToJs opts mp (DataBindingGroupDeclaration ds) e =
87 Just $ concat $ mapMaybe (flip (declToJs opts mp) e) ds
87 Just $ concat $ mapMaybe (flip (declToJs opts mp) e) ds
88declToJs _ mp (ExternDeclaration importTy ident (Just js) _) _ =
88declToJs _ mp (ExternDeclaration importTy ident (Just js) _) _ =
90− , setProperty ident (JSVar ident) mp ]
90+ , setProperty (identToJs ident) (JSVar ident) mp ]
91declToJs _ _ _ _ = Nothing
91declToJs _ _ _ _ = Nothing
93−setProperty :: Ident -> JS -> ModuleName -> JS
93+setProperty :: String -> JS -> ModuleName -> JS
94setProperty prop val (ModuleName (ProperName moduleName)) = JSAssignment (JSAssignProperty prop (JSAssignVariable (Ident moduleName))) val
94setProperty prop val (ModuleName (ProperName moduleName)) = JSAssignment (JSAssignProperty prop (JSAssignVariable (Ident moduleName))) val
96valueToJs :: Options -> ModuleName -> Environment -> Value -> JS
96valueToJs :: Options -> ModuleName -> Environment -> Value -> JS
@@ -99,16 +99,16 @@ valueToJs _ _ _ (StringLiteral s) = JSStringLiteral s
99valueToJs _ _ _ (BooleanLiteral b) = JSBooleanLiteral b
99valueToJs _ _ _ (BooleanLiteral b) = JSBooleanLiteral b
100valueToJs opts m e (ArrayLiteral xs) = JSArrayLiteral (map (valueToJs opts m e) xs)
100valueToJs opts m e (ArrayLiteral xs) = JSArrayLiteral (map (valueToJs opts m e) xs)
101valueToJs opts m e (ObjectLiteral ps) = JSObjectLiteral (map (second (valueToJs opts m e)) ps)
101valueToJs opts m e (ObjectLiteral ps) = JSObjectLiteral (map (second (valueToJs opts m e)) ps)
102−valueToJs opts m e (ObjectUpdate o ps) = JSApp (JSAccessor (Ident "extend") (JSVar (Ident "Object"))) [ valueToJs opts m e o, JSObjectLiteral (map (second (valueToJs opts m e)) ps)]
102+valueToJs opts m e (ObjectUpdate o ps) = JSApp (JSAccessor "extend" (JSVar (Ident "Object"))) [ valueToJs opts m e o, JSObjectLiteral (map (second (valueToJs opts m e)) ps)]
103valueToJs _ m e (Constructor (Qualified Nothing name)) =
103valueToJs _ m e (Constructor (Qualified Nothing name)) =
104 case M.lookup (m, name) (dataConstructors e) of
104 case M.lookup (m, name) (dataConstructors e) of
105− Just (_, Alias aliasModule aliasIdent) -> qualifiedToJS id (Qualified (Just aliasModule) aliasIdent)
105+ Just (_, Alias aliasModule aliasIdent) -> qualifiedToJS identToJs (Qualified (Just aliasModule) aliasIdent)
106 _ -> JSVar . Ident . runProperName $ name
106 _ -> JSVar . Ident . runProperName $ name
107−valueToJs _ _ _ (Constructor name) = qualifiedToJS (Ident . runProperName) name
107+valueToJs _ _ _ (Constructor name) = qualifiedToJS runProperName name
108valueToJs opts m e (Block sts) = JSApp (JSFunction Nothing [] (JSBlock (map (statementToJs opts m e) sts))) []
108valueToJs opts m e (Block sts) = JSApp (JSFunction Nothing [] (JSBlock (map (statementToJs opts m e) sts))) []
109valueToJs opts m e (Case values binders) = bindersToJs opts m e binders (map (valueToJs opts m e) values)
109valueToJs opts m e (Case values binders) = bindersToJs opts m e binders (map (valueToJs opts m e) values)
110valueToJs opts m e (IfThenElse cond th el) = JSConditional (valueToJs opts m e cond) (valueToJs opts m e th) (valueToJs opts m e el)
110valueToJs opts m e (IfThenElse cond th el) = JSConditional (valueToJs opts m e cond) (valueToJs opts m e th) (valueToJs opts m e el)
111−valueToJs opts m e (Accessor prop val) = JSAccessor (Ident prop) (valueToJs opts m e val)
111+valueToJs opts m e (Accessor prop val) = JSAccessor prop (valueToJs opts m e val)
112valueToJs opts m e (Indexer index val) = JSIndexer (valueToJs opts m e index) (valueToJs opts m e val)
112valueToJs opts m e (Indexer index val) = JSIndexer (valueToJs opts m e index) (valueToJs opts m e val)
113valueToJs opts m e (App val arg) = JSApp (valueToJs opts m e val) [valueToJs opts m e arg]
113valueToJs opts m e (App val arg) = JSApp (valueToJs opts m e val) [valueToJs opts m e arg]
114valueToJs opts m e (Abs arg val) = JSFunction Nothing [arg] (JSBlock [JSReturn (valueToJs opts m e val)])
114valueToJs opts m e (Abs arg val) = JSFunction Nothing [arg] (JSBlock [JSReturn (valueToJs opts m e val)])
@@ -140,22 +140,22 @@ runtimeTypeChecks arg ty =
141 (pairs, _) = rowToList row
141 (pairs, _) = rowToList row
143− typeCheck val "object" : concatMap (\(prop, ty') -> argumentCheck (JSAccessor (Ident prop) val) ty') pairs
143+ typeCheck val "object" : concatMap (\(prop, ty') -> argumentCheck (JSAccessor prop val) ty') pairs
144 argumentCheck val (TypeApp (TypeApp t _) _) | t == tyFunction = [typeCheck val "function"]
144 argumentCheck val (TypeApp (TypeApp t _) _) | t == tyFunction = [typeCheck val "function"]
145 argumentCheck val (ForAll _ ty') = argumentCheck val ty'
145 argumentCheck val (ForAll _ ty') = argumentCheck val ty'
146 argumentCheck _ _ = []
146 argumentCheck _ _ = []
147 typeCheck :: JS -> String -> JS
147 typeCheck :: JS -> String -> JS
148 typeCheck js ty' = JSIfElse (JSBinary NotEqualTo (JSTypeOf js) (JSStringLiteral ty')) (JSBlock [JSThrow (JSStringLiteral $ ty' ++ " expected")]) Nothing
148 typeCheck js ty' = JSIfElse (JSBinary NotEqualTo (JSTypeOf js) (JSStringLiteral ty')) (JSBlock [JSThrow (JSStringLiteral $ ty' ++ " expected")]) Nothing
149 arrayCheck :: JS -> JS
149 arrayCheck :: JS -> JS
150− arrayCheck js = JSIfElse (JSUnary Not (JSApp (JSAccessor (Ident "isArray") (JSVar (Ident "Array"))) [js])) (JSBlock [JSThrow (JSStringLiteral "Array expected")]) Nothing
150+ arrayCheck js = JSIfElse (JSUnary Not (JSApp (JSAccessor "isArray" (JSVar (Ident "Array"))) [js])) (JSBlock [JSThrow (JSStringLiteral "Array expected")]) Nothing
152varToJs :: ModuleName -> Environment -> Qualified Ident -> JS
152varToJs :: ModuleName -> Environment -> Qualified Ident -> JS
153varToJs m e qual@(Qualified _ ident) = case M.lookup (qualify m qual) (names e) of
153varToJs m e qual@(Qualified _ ident) = case M.lookup (qualify m qual) (names e) of
154 Just (_, ty) | isExtern ty -> JSVar ident
154 Just (_, ty) | isExtern ty -> JSVar ident
155− Just (_, Alias aliasModule aliasIdent) -> qualifiedToJS id (Qualified (Just aliasModule) aliasIdent)
155+ Just (_, Alias aliasModule aliasIdent) -> qualifiedToJS identToJs (Qualified (Just aliasModule) aliasIdent)
156 _ -> case qual of
156 _ -> case qual of
157 Qualified Nothing _ -> JSVar ident
157 Qualified Nothing _ -> JSVar ident
158− _ -> qualifiedToJS id qual
158+ _ -> qualifiedToJS identToJs qual
160 isExtern (Extern ForeignImport) = True
160 isExtern (Extern ForeignImport) = True
161 isExtern (Alias m' ident') = case M.lookup (m', ident') (names e) of
161 isExtern (Alias m' ident') = case M.lookup (m', ident') (names e) of
@@ -163,9 +163,9 @@ varToJs m e qual@(Qualified _ ident) = case M.lookup (qualify m qual) (names e)
163 Nothing -> error "Undefined alias in varToJs"
163 Nothing -> error "Undefined alias in varToJs"
164 isExtern _ = False
164 isExtern _ = False
166−qualifiedToJS :: (a -> Ident) -> Qualified a -> JS
166+qualifiedToJS :: (a -> String) -> Qualified a -> JS
167qualifiedToJS f (Qualified (Just (ModuleName (ProperName m))) a) = JSAccessor (f a) (JSVar (Ident m))
167qualifiedToJS f (Qualified (Just (ModuleName (ProperName m))) a) = JSAccessor (f a) (JSVar (Ident m))
168−qualifiedToJS f (Qualified Nothing a) = JSVar (f a)
168+qualifiedToJS f (Qualified Nothing a) = JSVar (Ident (f a))
170bindersToJs :: Options -> ModuleName -> Environment -> [([Binder], Maybe Guard, Value)] -> [JS] -> JS
170bindersToJs :: Options -> ModuleName -> Environment -> [([Binder], Maybe Guard, Value)] -> [JS] -> JS
171bindersToJs opts m e binders vals = runGen (unusedNames (binders, vals)) $ do
171bindersToJs opts m e binders vals = runGen (unusedNames (binders, vals)) $ do
@@ -199,16 +199,16 @@ binderToJs m e varName done (NullaryBinder ctor) =
200 return done
200 return done
202− return [JSIfElse (JSBinary EqualTo (JSAccessor (Ident "ctor") (JSVar varName)) (JSStringLiteral (show ((\(mp, nm) -> Qualified (Just mp) nm) $ canonicalizeDataConstructor m e ctor)))) (JSBlock done) Nothing]
202+ return [JSIfElse (JSBinary EqualTo (JSAccessor "ctor" (JSVar varName)) (JSStringLiteral (show ((\(mp, nm) -> Qualified (Just mp) nm) $ canonicalizeDataConstructor m e ctor)))) (JSBlock done) Nothing]
203binderToJs m e varName done (UnaryBinder ctor b) = do
203binderToJs m e varName done (UnaryBinder ctor b) = do
204 value <- fresh
204 value <- fresh
205 js <- binderToJs m e value done b
205 js <- binderToJs m e value done b
206− let success = JSBlock (JSVariableIntroduction value (Just (JSAccessor (Ident "value") (JSVar varName))) : js)
206+ let success = JSBlock (JSVariableIntroduction value (Just (JSAccessor "value" (JSVar varName))) : js)
207 if isOnlyConstructor m e ctor
207 if isOnlyConstructor m e ctor
209 return [success]
209 return [success]
211− return [JSIfElse (JSBinary EqualTo (JSAccessor (Ident "ctor") (JSVar varName)) (JSStringLiteral (show ((\(mp, nm) -> Qualified (Just mp) nm) $ canonicalizeDataConstructor m e ctor))))
211+ return [JSIfElse (JSBinary EqualTo (JSAccessor "ctor" (JSVar varName)) (JSStringLiteral (show ((\(mp, nm) -> Qualified (Just mp) nm) $ canonicalizeDataConstructor m e ctor))))
214binderToJs m e varName done (ObjectBinder bs) = go done bs
214binderToJs m e varName done (ObjectBinder bs) = go done bs
@@ -219,10 +219,10 @@ binderToJs m e varName done (ObjectBinder bs) = go done bs
219 propVar <- fresh
219 propVar <- fresh
220 done'' <- go done' bs'
220 done'' <- go done' bs'
221 js <- binderToJs m e propVar done'' binder
221 js <- binderToJs m e propVar done'' binder
222− return (JSVariableIntroduction propVar (Just (JSAccessor (Ident prop) (JSVar varName))) : js)
222+ return (JSVariableIntroduction propVar (Just (JSAccessor prop (JSVar varName))) : js)
223binderToJs m e varName done (ArrayBinder bs) = do
223binderToJs m e varName done (ArrayBinder bs) = do
224 js <- go done 0 bs
224 js <- go done 0 bs
225− return [JSIfElse (JSBinary EqualTo (JSAccessor (Ident "length") (JSVar varName)) (JSNumericLiteral (Left (fromIntegral $ length bs)))) (JSBlock js) Nothing]
225+ return [JSIfElse (JSBinary EqualTo (JSAccessor "length" (JSVar varName)) (JSNumericLiteral (Left (fromIntegral $ length bs)))) (JSBlock js) Nothing]
227 go :: [JS] -> Integer -> [Binder] -> Gen [JS]
227 go :: [JS] -> Integer -> [Binder] -> Gen [JS]
228 go done' _ [] = return done'
228 go done' _ [] = return done'
@@ -236,9 +236,9 @@ binderToJs m e varName done (ConsBinder headBinder tailBinder) = do
236 tailVar <- fresh
236 tailVar <- fresh
237 js1 <- binderToJs m e headVar done headBinder
237 js1 <- binderToJs m e headVar done headBinder
238 js2 <- binderToJs m e tailVar js1 tailBinder
238 js2 <- binderToJs m e tailVar js1 tailBinder
239− return [JSIfElse (JSBinary GreaterThan (JSAccessor (Ident "length") (JSVar varName)) (JSNumericLiteral (Left 0))) (JSBlock
239+ return [JSIfElse (JSBinary GreaterThan (JSAccessor "length" (JSVar varName)) (JSNumericLiteral (Left 0))) (JSBlock
240 ( JSVariableIntroduction headVar (Just (JSIndexer (JSNumericLiteral (Left 0)) (JSVar varName))) :
240 ( JSVariableIntroduction headVar (Just (JSIndexer (JSNumericLiteral (Left 0)) (JSVar varName))) :
241− JSVariableIntroduction tailVar (Just (JSApp (JSAccessor (Ident "slice") (JSVar varName)) [JSNumericLiteral (Left 1)])) :
241+ JSVariableIntroduction tailVar (Just (JSApp (JSAccessor "slice" (JSVar varName)) [JSNumericLiteral (Left 1)])) :
243 )) Nothing]
243 )) Nothing]
244binderToJs m e varName done (NamedBinder ident binder) = do
244binderToJs m e varName done (NamedBinder ident binder) = do