on master help sign in
57 lines 1.5 kB history raw
1{-|
2Module : IRTS.JavaScript.Name
3Description : The JavaScript name mangler.
5License : BSD3
6Maintainer : The Idris Community.
7-}
9{-# LANGUAGE OverloadedStrings, PatternGuards #-}
11module IRTS.JavaScript.Name
12 ( jsName
13 , jsNameGenerated
14 , Partial(..)
15 , jsNamePartial
16 , jsTailCallOptimName
17 , HiddenClass(..)
18 , jsNameHiddenClass
19 , dataPartName
20 ) where
22import Data.Char
23import Data.Text (Text)
24import qualified Data.Text as T
25import Idris.Core.TT
27jsEscape :: String -> String
28jsEscape = concatMap jschar
29 where
30 jschar x
31 | isAlpha x || isDigit x = [x]
32 | x == '.' = "__"
33 | otherwise = "_" ++ show (fromEnum x) ++ "_"
35jsName :: Name -> Text
36jsName (MN i u) = T.concat ["$_", T.pack (show i), "_", T.pack $ jsEscape $ T.unpack u]
37jsName n = T.pack $ jsEscape $ showCG n
39jsNameGenerated :: Int -> Text
40jsNameGenerated v = T.pack $ "$cg$" ++ show v
42data Partial = Partial Name Int Int deriving (Eq, Ord)
44jsNamePartial :: Partial -> Text
45jsNamePartial (Partial n i j) = T.concat ["$partial_", T.pack $ show i, "_", T.pack $ show j, "$" , jsName n]
47jsTailCallOptimName :: Text -> Text
48jsTailCallOptimName x = T.concat ["$tco$", x]
51data HiddenClass = HiddenClass Name Int Int deriving (Eq, Ord)
53jsNameHiddenClass :: HiddenClass -> Text
54jsNameHiddenClass (HiddenClass n id arity) = T.concat ["$HC_", T.pack $ show arity, "_", T.pack $ show id,"$", jsName n]
56dataPartName :: Int -> Text
57dataPartName i = T.pack $ "$" ++ show i