From b6872d4bacec5953cdcad04ec1b686d6a39f6d4d Mon Sep 17 00:00:00 2001 From: ngunn Date: Tue, 26 Mar 2013 01:03:12 +0000 Subject: [PATCH] Singleton Nil consequential change to compiler (minor) CompileToJS generates objects using "ctor:"... for all non-primitives. Added special handling for lists Might be better to have a separate tag instead of: Data "Nil" Test case: main_join = let f x xs = (x::xs) in asText $ zipWith f ['c','m','t'] ["hop","ill","ape"] before fails with "xs: undefined". --- compiler/Gen/CompileToJS.hs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/compiler/Gen/CompileToJS.hs b/compiler/Gen/CompileToJS.hs index b074944..554d6cc 100644 --- a/compiler/Gen/CompileToJS.hs +++ b/compiler/Gen/CompileToJS.hs @@ -223,8 +223,11 @@ instance ToJS Expr where Let defs e -> jsLet defs e Data name es -> do fs <- mapM toJS' es - let fields = zipWith (\n e -> "_" ++ show n ++ ":" ++ e) [0..] fs - return (brackets ("ctor:" ++ show name ++ concatMap (", "++) fields)) + return $ case name of + "Nil" -> jsNil + "Cons" -> jsCons (head fs) ((head . tail) fs) + _ -> brackets $ "ctor:" ++ show name ++ concatMap (", "++) fields + where fields = zipWith (\n e -> "_" ++ show n ++ ":" ++ e) [0..] fs Markdown doc -> return $ "text('" ++ pad ++ md ++ pad ++ "')" where pad = "
 
"