on master help sign in
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".

commitb6872d4bacec5953cdcad04ec1b686d6a39f6d4d authorngunn <ngunn@computationalthinking.org.uk> dateTue 26 Mar 2013, 01:03 (14 years ago) parent3559c53
1 file changed+5−2
compiler/Gen/CompileToJS.hs+5−2
compiler/Gen/CompileToJS.hs+5−2
@@ -223,8 +223,11 @@ instance ToJS Expr where
223223 Let defs e -> jsLet defs e
224224 Data name es ->
225225 do fs <- mapM toJS' es
226− let fields = zipWith (\n e -> "_" ++ show n ++ ":" ++ e) [0..] fs
227− return (brackets ("ctor:" ++ show name ++ concatMap (", "++) fields))
226+ return $ case name of
227+ "Nil" -> jsNil
228+ "Cons" -> jsCons (head fs) ((head . tail) fs)
229+ _ -> brackets $ "ctor:" ++ show name ++ concatMap (", "++) fields
230+ where fields = zipWith (\n e -> "_" ++ show n ++ ":" ++ e) [0..] fs
228231
229232 Markdown doc -> return $ "text('" ++ pad ++ md ++ pad ++ "')"
230233 where pad = "<div style=\"height:0;width:0;\">&nbsp;</div>"