Skip to content

Commit

Permalink
Improve formatting of chained updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
athas committed Dec 11, 2024
1 parent bc24c74 commit b338bad
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/Futhark/Fmt/Printer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,29 @@ instance Format PrimValue where
BoolValue False -> "false"
FloatValue v -> prettyText v

updates ::
UncheckedExp ->
(UncheckedExp, [(Fmt, Fmt)])
updates (RecordUpdate src fs ve _ _) = second (++ [(fs', ve')]) $ updates src
where
fs' = sep "." $ fmt <$> fs
ve' = fmt ve
updates (Update src is ve _) = second (++ [(is', ve')]) $ updates src
where
is' = brackets $ sep ("," <> space) $ map fmt is
ve' = fmt ve
updates e = (e, [])

fmtUpdate :: UncheckedExp -> Fmt
fmtUpdate e =
-- Special case multiple chained Updates/RecordUpdates.
let (root, us) = updates e
loc = srclocOf e
in addComments loc . localLayout loc $
fmt root <+> align (sep line (map fmtWith us))
where
fmtWith (fs', v) = "with" <+> fs' <+> "=" <+> v

instance Format UncheckedExp where
fmt (Var name _ loc) = addComments loc $ fmtQualName name
fmt (Hole _ loc) = addComments loc "???"
Expand All @@ -246,16 +269,8 @@ instance Format UncheckedExp where
fmt (Project k e _ loc) = addComments loc $ fmt e <> "." <> fmt k
fmt (Negate e loc) = addComments loc $ "-" <> fmt e
fmt (Not e loc) = addComments loc $ "!" <> fmt e
fmt (Update src idxs ve loc) =
addComments loc $
fmt src <+> "with" <+> idxs' <+> stdNest ("=" </> fmt ve)
where
idxs' = brackets $ sep ("," <> space) $ map fmt idxs
fmt (RecordUpdate src fs ve _ loc) =
addComments loc $
fmt src <+> "with" <+> fs' <+> stdNest ("=" </> fmt ve)
where
fs' = sep "." $ fmt <$> fs
fmt e@Update {} = fmtUpdate e
fmt e@RecordUpdate {} = fmtUpdate e
fmt (Assert e1 e2 _ loc) =
addComments loc $ "assert" <+> fmt e1 <+> fmt e2
fmt (Lambda params body rettype _ loc) =
Expand Down

0 comments on commit b338bad

Please sign in to comment.