definition:
|
generatorWrite :: FunctionGenerator
generatorWrite typedecl = return $ zipWith (forCons (rwBaseModuleName rwNaming, "writeRW")) [0..] tcs
where
tcs = typeCons typedecl
--- Exactly one constructor with exactly zero arguments
trivialCons = case tcs of
[CCons _ _ []] -> True
_ -> False
handlePat | trivialCons = anonPattern
| otherwise = CPVar (0, "h")
forCons wfn i (CCons name _ tes)
= CRule lhs (CSimpleRhs rhs [])
where
rhs | trivialCons = CApply (CSymbol $ pre "return") (CVar (length tes + 1, "strs"))
| null tes = applyF (pre ">>") [writeCons, CApply (CSymbol $ pre "return") (CVar (length tes + 1, "strs"))]
| length tcs == 1 = monad
| otherwise = applyF (pre ">>") [writeCons, monad]
writeCons | length tcs <= length coding = applyF ("System.IO","hPutChar") [CVar (0,"h"), cChar (coding !! i)]
| otherwise = applyF ("System.IO","hPutStr") [CVar (0,"h"), string2ac (codingI i (length tcs))]
monad = combineWithL (pre ">>=") (map (\index -> applyF wfn (CVar (0, "params") : (args index))) (fromIndex0 tes))
args index = appendIf (index == 0) [CVar (0, "h"), CVar (index + 1, varName index ++ "'")] (CVar (length tes + 1, "strs"))
lhs = [paramsPat, handlePat,
CPComb name (map (\index -> CPVar (index + 1, varName index ++ "'"))
(fromIndex0 tes)),
CPVar (length tes + 1, "strs")]
where
paramsPat | null tes = anonPattern
| otherwise = CPVar (0, "params")
|