@@ -26,7 +26,7 @@ indent = concatMap f
28parens s = "(" ++ s ++ ")"
28parens s = "(" ++ s ++ ")"
29−braces s = "{" ++ s ++ "}"
29+brackets s = "{" ++ s ++ "}"
30jsList ss = "["++ intercalate "," ss ++"]"
30jsList ss = "["++ intercalate "," ss ++"]"
31jsFunc args body = "function(" ++ args ++ "){" ++ indent body ++ "}"
31jsFunc args body = "function(" ++ args ++ "){" ++ indent body ++ "}"
32assign x e = "\nvar " ++ x ++ "=" ++ e ++ ";"
32assign x e = "\nvar " ++ x ++ "=" ++ e ++ ";"
@@ -81,7 +81,7 @@ jsModule (escapees, Module names exports imports stmts) =
81 defs = concat [ assign "$op" "{}"
81 defs = concat [ assign "$op" "{}"
82 , "\nfor(Elm['i'] in Elm){eval('var '+Elm['i']+'=Elm[Elm.i];');}" ]
82 , "\nfor(Elm['i'] in Elm){eval('var '+Elm['i']+'=Elm[Elm.i];');}" ]
84−getExports names stmts = ret . braces $ intercalate ",\n" (op : map fnPair fns)
84+getExports names stmts = ret . brackets $ intercalate ",\n" (op : map fnPair fns)
85 where exNames n = either derename id n `elem` names
85 where exNames n = either derename id n `elem` names
86 exports | null names = concatMap get stmts
86 exports | null names = concatMap get stmts
87 | otherwise = filter exNames (concatMap get stmts)
87 | otherwise = filter exNames (concatMap get stmts)
@@ -91,7 +91,7 @@ getExports names stmts = ret . braces $ intercalate ",\n" (op : map fnPair fns)
91 opPair op = "'" ++ op ++ "' : $op['" ++ op ++ "']"
91 opPair op = "'" ++ op ++ "' : $op['" ++ op ++ "']"
92 fnPair fn = let fn' = derename fn in fn' ++ ":" ++ fn
92 fnPair fn = let fn' = derename fn in fn' ++ ":" ++ fn
94− op = ("$op : "++) . braces . intercalate ", " $ map opPair ops
94+ op = ("$op : "++) . brackets . intercalate ", " $ map opPair ops
96 get' (FnDef x _ _) = Left x
96 get' (FnDef x _ _) = Left x
97 get' (OpDef op _ _ _) = Right op
97 get' (OpDef op _ _ _) = Right op
@@ -122,7 +122,7 @@ jsImport' (modul, Hiding vs) =
122 concat [ assign "hiddenVars" . ("{"++) . (++"}") . intercalate "," $
122 concat [ assign "hiddenVars" . ("{"++) . (++"}") . intercalate "," $
123 map (\v -> v ++ ":true") (map deprime vs)
123 map (\v -> v ++ ":true") (map deprime vs)
124 , "\nfor (Elm['i'] in " ++ modul ++ ") "
124 , "\nfor (Elm['i'] in " ++ modul ++ ") "
125− , braces . indent . concat $
125+ , brackets . indent . concat $
126 [ "\nif (hiddenVars[Elm['i']]) continue;"
126 [ "\nif (hiddenVars[Elm['i']]) continue;"
127 , "\neval('var ' + Elm['i'] + ' = "
127 , "\neval('var ' + Elm['i'] + ' = "
128 , modul, "[Elm.i];');" ]
128 , modul, "[Elm.i];');" ]
@@ -148,7 +148,7 @@ instance ToJS Def where
148 toJS (FnDef x [] e) = assign x `liftM` toJS' e
148 toJS (FnDef x [] e) = assign x `liftM` toJS' e
149 toJS (FnDef f (a:as) e) =
149 toJS (FnDef f (a:as) e) =
150 do body <- toJS' (foldr (\x e -> noContext (Lambda x e)) e as)
150 do body <- toJS' (foldr (\x e -> noContext (Lambda x e)) e as)
151− return $ concat ["\nfunction ",f,parens a, braces . indent $ ret body]
151+ return $ concat ["\nfunction ",f,parens a, brackets . indent $ ret body]
152 toJS (OpDef op a1 a2 e) =
152 toJS (OpDef op a1 a2 e) =
153 do body <- toJS' (foldr (\x e -> noContext (Lambda x e)) e [a1,a2])
153 do body <- toJS' (foldr (\x e -> noContext (Lambda x e)) e [a1,a2])
154 return $ concat [ "\n$op['", op, "'] = ", body, ";" ]
154 return $ concat [ "\n$op['", op, "'] = ", body, ";" ]
@@ -194,7 +194,7 @@ makeRecord kvs = record `liftM` collect kvs
194 do v <- toJS' (foldr (\x e -> C t s $ Lambda x e) e as)
194 do v <- toJS' (foldr (\x e -> C t s $ Lambda x e) e as)
195 return (k,[v])
195 return (k,[v])
196 fields fs =
196 fields fs =
197− braces ("\n "++intercalate ",\n " (map (\(k,v) -> k++":"++v) fs))
197+ brackets ("\n "++intercalate ",\n " (map (\(k,v) -> k++":"++v) fs))
198 hidden = fields . map (second jsList) .
198 hidden = fields . map (second jsList) .
199 filter (not . null . snd) . Map.toList . Map.map tail
199 filter (not . null . snd) . Map.toList . Map.map tail
200 record kvs = fields . (("_", hidden kvs) :) . Map.toList . Map.map head $ kvs
200 record kvs = fields . (("_", hidden kvs) :) . Map.toList . Map.map head $ kvs
@@ -234,7 +234,10 @@ instance ToJS Expr where
235 App e1 e2 -> (++) `liftM` (toJS' e1) `ap` (parens `liftM` toJS' e2)
235 App e1 e2 -> (++) `liftM` (toJS' e1) `ap` (parens `liftM` toJS' e2)
236 Let defs e -> jsLet defs e
236 Let defs e -> jsLet defs e
237− Data name es -> (\ss -> jsList $ quoted name : ss) `liftM` mapM toJS' es
237+ Data name es ->
238+ do fs <- mapM toJS' es
239+ let fields = zipWith (\n e -> "_" ++ show n ++ ":" ++ e) [0..] fs
240+ return (brackets ("ctor:" ++ show name ++ concatMap (", "++) fields))
239 Markdown doc -> return $ "text('" ++ pad ++ md ++ pad ++ "')"
242 Markdown doc -> return $ "text('" ++ pad ++ md ++ pad ++ "')"
240 where pad = "<div style=\"height:0;width:0;\"> </div>"
243 where pad = "<div style=\"height:0;width:0;\"> </div>"
@@ -276,7 +279,7 @@ caseToJS span e ps = do
276matchToJS span (Match name clauses def) = do
279matchToJS span (Match name clauses def) = do
277 cases <- concat `liftM` mapM (clauseToJS span name) clauses
280 cases <- concat `liftM` mapM (clauseToJS span name) clauses
278 finally <- matchToJS span def
281 finally <- matchToJS span def
279− return $ concat [ "\nswitch(", name, "[0]){", indent cases, "\n}", finally ]
282+ return $ concat [ "\nswitch(", name, ".ctor){", indent cases, "\n}", finally ]
280matchToJS span Fail = return ("\nthrow new Error(\"Non-exhaustive pattern match " ++
283matchToJS span Fail = return ("\nthrow new Error(\"Non-exhaustive pattern match " ++
281 "in case expression (" ++ show span ++ ")\");")
284 "in case expression (" ++ show span ++ ")\");")
282matchToJS span Break = return "break;"
285matchToJS span Break = return "break;"
@@ -284,7 +287,7 @@ matchToJS span (Other e) = ret `liftM` toJS' e
284matchToJS span (Seq ms) = concat `liftM` mapM (matchToJS span) ms
287matchToJS span (Seq ms) = concat `liftM` mapM (matchToJS span) ms
286clauseToJS span var (Clause name vars e) = do
289clauseToJS span var (Clause name vars e) = do
287− let vars' = map (\n -> var ++ "[" ++ show n ++ "]") [ 1 .. length vars ]
290+ let vars' = map (\n -> var ++ "._" ++ show n) [ 1 .. length vars ]
288 s <- matchToJS span $ matchSubst (zip vars vars') e
291 s <- matchToJS span $ matchSubst (zip vars vars') e
289 return $ concat [ "\ncase ", quoted name, ":", s ]
292 return $ concat [ "\ncase ", quoted name, ":", s ]
@@ -307,12 +310,12 @@ binop (o:p) e1 e2
307 "^" -> "Math.pow(" ++ e1 ++ "," ++ e2 ++ ")"
310 "^" -> "Math.pow(" ++ e1 ++ "," ++ e2 ++ ")"
308 "==" -> "eq(" ++ e1 ++ "," ++ e2 ++ ")"
311 "==" -> "eq(" ++ e1 ++ "," ++ e2 ++ ")"
309 "/=" -> "not(eq(" ++ e1 ++ "," ++ e2 ++ "))"
312 "/=" -> "not(eq(" ++ e1 ++ "," ++ e2 ++ "))"
310− "<" -> "(compare(" ++ e1 ++ ")(" ++ e2 ++ ")[0] === 'LT')"
313+ "<" -> "(compare(" ++ e1 ++ ")(" ++ e2 ++ ").ctor === 'LT')"
311− ">" -> "(compare(" ++ e1 ++ ")(" ++ e2 ++ ")[0] === 'GT')"
314+ ">" -> "(compare(" ++ e1 ++ ")(" ++ e2 ++ ").ctor === 'GT')"
312 "<=" -> "function() { var ord = compare(" ++ e1 ++ ")(" ++
315 "<=" -> "function() { var ord = compare(" ++ e1 ++ ")(" ++
313− e2 ++ ")[0]; return ord==='LT' || ord==='EQ'; }()"
316+ e2 ++ ").ctor; return ord==='LT' || ord==='EQ'; }()"
314 ">=" -> "function() { var ord = compare(" ++ e1 ++ ")(" ++
317 ">=" -> "function() { var ord = compare(" ++ e1 ++ ")(" ++
315− e2 ++ ")[0]; return ord==='GT' || ord==='EQ'; }()"
318+ e2 ++ ").ctor; return ord==='GT' || ord==='EQ'; }()"
316 "<~" -> "lift" ++ parens e1 ++ parens e2
319 "<~" -> "lift" ++ parens e1 ++ parens e2
317 "~" -> "lift2(function(f){return function(x){return f(x);};})" ++
320 "~" -> "lift2(function(f){return function(x){return f(x);};})" ++
318 parens e1 ++ parens e2
321 parens e1 ++ parens e2