1-- | Common code generation utility functions
2module CodeGen.IL.Common where
6import Control.Monad.Supply.Class (MonadSupply, freshName)
8import Data.Maybe (maybe)
9import Data.Monoid ((<>))
10import Data.Text (Text)
11import qualified Data.Text as T
13import Language.PureScript.Crash
14import Language.PureScript.Names
16import qualified Language.PureScript.Constants as C
18moduleNameToIL :: ModuleName -> Text
19moduleNameToIL (ModuleName pns) =
20 let name = T.intercalate "_" (runProperName `map` pns)
21 in moduleNameToIL' name
23moduleNameToIL' :: Text -> Text
25 if moduleIsReserved name then (name <> "_") else name
27-- | Convert an 'Ident' into a valid IL identifier:
29-- * Alphanumeric characters are kept unmodified.
31-- * Reserved IL identifiers are wrapped with '_'.
33identToIL :: Ident -> Text
34identToIL UnusedIdent = unusedName
35identToIL (Ident "$__unused") = unusedName
36identToIL (Ident name) | name == C.undefined = undefinedName
37identToIL (Ident name) = properToIL name
38identToIL (GenIdent _ _) = internalError "GenIdent in identToIL"
40moduleIdentToIL :: Ident -> Text
41moduleIdentToIL UnusedIdent = unusedName
42moduleIdentToIL (Ident name) | name == C.undefined = undefinedName
43moduleIdentToIL (Ident name) = moduleProperToIL name
44moduleIdentToIL (GenIdent _ _) = internalError "GenIdent in identToIL"
47undefinedName = "Undefined"
52properToIL :: Text -> Text
54 | nameIsILReserved name || nameIsILBuiltIn name || prefixIsReserved name = name <> "ˉ"
55 | otherwise = T.concatMap identCharToText name
57-- | Attempts to find a human-readable name for a symbol, if none has been specified returns the
59identCharToText :: Char -> Text
60identCharToText c | isAlphaNum c = T.singleton c
61identCharToText '_' = "_"
62identCharToText '\'' = "ʹ"
63identCharToText c = "ᵤ" <> T.pack (show (ord c))
65moduleProperToIL :: Text -> Text
66moduleProperToIL = T.concatMap identCharToText
68-- | Checks whether an identifier name is reserved in IL.
69nameIsILReserved :: Text -> Bool
70nameIsILReserved name =
71 name `elem` ilAnyReserved
73-- | Checks whether a name matches a built-in value in IL.
74nameIsILBuiltIn :: Text -> Bool
80ilAnyReserved :: [Text]
116moduleIsReserved :: Text -> Bool
117moduleIsReserved "Main" = False
118moduleIsReserved name = not $ T.isInfixOf "_" name
120prefixIsReserved :: Text -> Bool
121prefixIsReserved name =
122 any (`T.isPrefixOf` name)
150withPrefix :: Text -> Text
151withPrefix s = "PS__" <> s
157dictType = "map[string]Any"
175arrayLengthFn = "Length"
180freshName' :: MonadSupply m => m Text
183 return $ T.replace "$" "ᵗ" name
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list