Skip to content

Commit

Permalink
fix quoting
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Sep 12, 2024
1 parent 4607b00 commit 58e2af8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
48 changes: 26 additions & 22 deletions src/Juvix/Compiler/Tree/Pretty/Extra.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Juvix.Compiler.Tree.Pretty.Extra where

import Data.Text qualified as Text
import Juvix.Data.CodeAnn
import Juvix.Prelude

Expand All @@ -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__")
]
12 changes: 9 additions & 3 deletions src/Juvix/Prelude/Base/Foundation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------------------------------------------------------------------
Expand All @@ -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
--------------------------------------------------------------------------------
Expand Down

0 comments on commit 58e2af8

Please sign in to comment.