on cecf6bf help sign in
60 lines 1.6 kB history raw
1{-|
2Module : IRTS.JavaScript.Name
3Description : The JavaScript name mangler.
4Copyright :
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.List
24import qualified Data.Map.Strict as Map
25import Data.Text (Text)
26import qualified Data.Text as T
27import Idris.Core.TT
28import IRTS.JavaScript.AST
30jsEscape :: String -> String
31jsEscape = concatMap jschar
32 where
33 jschar x
34 | isAlpha x || isDigit x = [x]
35 | x == '.' = "__"
36 | otherwise = "_" ++ show (fromEnum x) ++ "_"
38jsName :: Name -> Text
39jsName (MN i u) = T.concat ["$_", T.pack (show i), "_", T.pack $ jsEscape $ T.unpack u]
40jsName n = T.pack $ jsEscape $ showCG n
42jsNameGenerated :: Int -> Text
43jsNameGenerated v = T.pack $ "$cg$" ++ show v
45data Partial = Partial Name Int Int deriving (Eq, Ord)
47jsNamePartial :: Partial -> Text
48jsNamePartial (Partial n i j) = T.concat ["$partial_", T.pack $ show i, "_", T.pack $ show j, "$" , jsName n]
50jsTailCallOptimName :: Text -> Text
51jsTailCallOptimName x = T.concat ["$tco$", x]
54data HiddenClass = HiddenClass Name Int Int deriving (Eq, Ord)
56jsNameHiddenClass :: HiddenClass -> Text
57jsNameHiddenClass (HiddenClass n id arity) = T.concat ["$HC_", T.pack $ show arity, "_", T.pack $ show id,"$", jsName n]
59dataPartName :: Int -> Text
60dataPartName i = T.pack $ "$" ++ show i