From d307afdb05102fa38044b31a97ab31d1a15f94ad Mon Sep 17 00:00:00 2001 From: Lukasz Czajka Date: Thu, 12 Sep 2024 10:01:43 +0200 Subject: [PATCH] fix quoting --- src/Juvix/Compiler/Tree/Pretty/Extra.hs | 48 +++++++++++++------------ src/Juvix/Prelude/Base/Foundation.hs | 12 +++++-- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/Juvix/Compiler/Tree/Pretty/Extra.hs b/src/Juvix/Compiler/Tree/Pretty/Extra.hs index cc7902f8c9..ac64fb4efe 100644 --- a/src/Juvix/Compiler/Tree/Pretty/Extra.hs +++ b/src/Juvix/Compiler/Tree/Pretty/Extra.hs @@ -1,6 +1,5 @@ module Juvix.Compiler.Tree.Pretty.Extra where -import Data.Text qualified as Text import Juvix.Data.CodeAnn import Juvix.Prelude @@ -17,28 +16,33 @@ variable :: Text -> Doc Ann variable a = annotate (AnnKind KNameLocal) (pretty a) quoteName :: Text -> Text -quoteName txt = - foldr - (uncurry Text.replace) - txt - [ ("$", "__dollar__"), - (":", "__colon__"), - ("@", "__at__"), - (".", "__dot__"), - (",", "__comma__"), - (";", "__semicolon__"), - ("arg", "__arg__"), - ("tmp", "__tmp__"), - ("sub", "__sub__"), - ("add", "__add__"), - ("mul", "__mul__"), - ("div", "__div__") - ] +quoteName = + quote1 . quote0 + where + quote0 :: Text -> Text + quote0 = + replaceSubtext + [ ("$", "__dollar__"), + (":", "__colon__"), + ("@", "__at__"), + (".", "__dot__"), + (",", "__comma__"), + (";", "__semicolon__") + ] + + quote1 :: Text -> Text + quote1 = + replaceText + [ ("arg", "__arg__"), + ("tmp", "__tmp__"), + ("sub", "__sub__"), + ("add", "__add__"), + ("mul", "__mul__"), + ("div", "__div__") + ] quoteFunName :: Text -> Text -quoteFunName txt = - foldr - (uncurry Text.replace) - txt +quoteFunName = + replaceText [ ("readLn", "__readLn__") ] diff --git a/src/Juvix/Prelude/Base/Foundation.hs b/src/Juvix/Prelude/Base/Foundation.hs index 3b49eccdc1..32eb62e85e 100644 --- a/src/Juvix/Prelude/Base/Foundation.hs +++ b/src/Juvix/Prelude/Base/Foundation.hs @@ -286,9 +286,6 @@ toUpperFirst :: String -> String toUpperFirst [] = [] toUpperFirst (x : xs) = Char.toUpper x : xs -uniqueName :: (Show a) => Text -> a -> Text -uniqueName txt sym = txt <> "_" <> show sym - -------------------------------------------------------------------------------- -- Text -------------------------------------------------------------------------------- @@ -313,6 +310,15 @@ isFirstLetter = \case h : _ -> isLetter h _ -> False +uniqueName :: (Show a) => Text -> a -> Text +uniqueName txt sym = txt <> "_" <> show sym + +replaceSubtext :: [(Text, Text)] -> Text -> Text +replaceSubtext texts txt = foldr (uncurry Text.replace) txt texts + +replaceText :: [(Text, Text)] -> Text -> Text +replaceText texts txt = fromMaybe txt (HashMap.lookup txt (HashMap.fromList texts)) + -------------------------------------------------------------------------------- -- Foldable --------------------------------------------------------------------------------