Skip to content

Commit

Permalink
Monoid special case also works on inverted section
Browse files Browse the repository at this point in the history
  • Loading branch information
singpolyma committed Aug 23, 2012
1 parent 2721810 commit 40a3f67
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions mustache2hs.hs
Expand Up @@ -148,6 +148,14 @@ originalMustache = mconcat . map origOne
] ]
origOne _ = mempty origOne _ = mempty


monoidSpecialCase :: Text -> Record -> Builder
monoidSpecialCase name rec = Builder.fromText $ case lookup name (snd rec) of
Just MuBool ->
T.pack "(Any " `mappend` name `mappend` T.pack ")"
Just MuNum ->
T.pack "(Sum " `mappend` name `mappend` T.pack ")"
_ -> name

codeGenTree :: (Show a, Enum a) => FilePath -> Text -> String -> Records -> MuTree -> Word -> State a (Builder, [(FilePath, String)]) codeGenTree :: (Show a, Enum a) => FilePath -> Text -> String -> Records -> MuTree -> Word -> State a (Builder, [(FilePath, String)])
codeGenTree path fname rname recs tree level = do codeGenTree path fname rname recs tree level = do
let rec = recordMustExist $ lookup rname recs let rec = recordMustExist $ lookup rname recs
Expand Down Expand Up @@ -224,29 +232,23 @@ codeGen path (rname,rec) recs level (MuSection name stree)
Builder.fromString " escapeFunction) ", Builder.fromString " escapeFunction) ",
Builder.fromText name Builder.fromText name
], [helper], partials) ], [helper], partials)
Just MuBool -> _ -> do
doVar (T.pack "(Any " `mappend` name `mappend` T.pack ")") nm (helper, partials) <- codeGenTree path nm rname recs stree (level+1)
Just MuNum -> return (mconcat [
doVar (T.pack "(Sum " `mappend` name `mappend` T.pack ")") nm Builder.fromString "if mempty /= ",
_ -> doVar name nm monoidSpecialCase name rec,
where Builder.fromString " then ",
doVar name nm = do Builder.fromText nm,
(helper, partials) <- codeGenTree path nm rname recs stree (level+1) Builder.fromString " escapeFunction ctx else mempty"
return (mconcat [ ], [helper], partials)
Builder.fromString "if mempty /= ",
Builder.fromText name,
Builder.fromString " then ",
Builder.fromText nm,
Builder.fromString " escapeFunction ctx else mempty"
], [helper], partials)
codeGen path (rname,rec) recs level (MuSectionInv name stree) = do codeGen path (rname,rec) recs level (MuSectionInv name stree) = do
id <- get id <- get
modify succ modify succ
let nm = name `mappend` T.pack (show id) let nm = name `mappend` T.pack (show id)
(helper, partials) <- codeGenTree path nm rname recs stree (level+1) (helper, partials) <- codeGenTree path nm rname recs stree (level+1)
return (mconcat [ return (mconcat [
Builder.fromString "if mempty == ", Builder.fromString "if mempty == ",
Builder.fromText name, monoidSpecialCase name rec,
Builder.fromString " then ", Builder.fromString " then ",
Builder.fromText nm, Builder.fromText nm,
Builder.fromString " escapeFunction ctx else mempty" Builder.fromString " escapeFunction ctx else mempty"
Expand Down

0 comments on commit 40a3f67

Please sign in to comment.