1module AST.Variable where
4import qualified Data.Map as Map
5import qualified Data.Maybe as Maybe
6import Text.PrettyPrint as P
8import qualified AST.Helpers as Help
9import qualified AST.Module.Name as ModuleName
10import qualified Reporting.PrettyPrint as P
15newtype Raw = Raw String
16 deriving (Eq, Ord, Show)
21data TopLevel = TopLevelVar
22 { topHome :: ModuleName.Canonical
25 deriving (Eq, Ord, Show)
33 | Module ModuleName.Canonical
34 | TopLevel ModuleName.Canonical
36 deriving (Eq, Ord, Show)
39data Canonical = Canonical
43 deriving (Eq, Ord, Show)
46local :: String -> Canonical
51topLevel :: ModuleName.Canonical -> String -> Canonical
53 Canonical (TopLevel home) x
56builtin :: String -> Canonical
61fromModule :: ModuleName.Canonical -> String -> Canonical
63 Canonical (Module home) name
66inCore :: ModuleName.Raw -> String -> Canonical
68 Canonical (Module (ModuleName.inCore home)) name
71-- VARIABLE RECOGNIZERS
73isLocalHome :: Home -> Bool
89is :: ModuleName.Raw -> String -> Canonical -> Bool
91 var == inCore home name
94isJson :: Canonical -> Bool
96 is ["Json", "Encode"] "Value"
99isMaybe :: Canonical -> Bool
103isArray :: Canonical -> Bool
108isTask :: Canonical -> Bool
113isSignal :: Canonical -> Bool
115 is ["Signal"] "Signal"
118isText :: Canonical -> Bool
123isList :: Canonical -> Bool
125 v == Canonical BuiltIn "List"
128isTuple :: Canonical -> Bool
131 Canonical BuiltIn name -> Help.isTuple name
135isPrimitive :: Canonical -> Bool
138 Canonical BuiltIn name ->
139 name `elem` ["Int","Float","String","Bool"]
144isPrim :: String -> Canonical -> Bool
145isPrim prim (Canonical home name) =
154isLocal :: (String -> Bool) -> Canonical -> Bool
155isLocal check (Canonical home name) =
168class ToString a where
169 toString :: a -> String
172instance ToString Raw where
173 toString (Raw name) =
177instance ToString Canonical where
178 toString (Canonical home name) =
184 ModuleName.canonicalToString moduleName ++ "." ++ name
195-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
196data Listing a = Listing
200 deriving (Eq, Ord, Show)
203openListing :: Listing a
208closedListing :: Listing a
213listing :: [a] -> Listing a
218-- | A value that can be imported or exported
222 | Union !String !(Listing String)
223 deriving (Eq, Ord, Show)
226-- CATEGORIZING VALUES
228getValues :: [Value] -> [String]
230 Maybe.mapMaybe getValue values
233getValue :: Value -> Maybe String
236 Value name -> Just name
241getAliases :: [Value] -> [String]
243 Maybe.mapMaybe getAlias values
246getAlias :: Value -> Maybe String
250 Alias name -> Just name
254getUnions :: [Value] -> [(String, Listing String)]
256 Maybe.mapMaybe getUnion values
259getUnion :: Value -> Maybe (String, Listing String)
264 Union name ctors -> Just (name, ctors)
269instance P.Pretty Raw where
270 pretty _ _ (Raw name) =
272 then P.parens (P.text name)
276instance P.Pretty Canonical where
277 pretty dealiaser _ var =
281 P.text (maybe name id (Map.lookup name dealiaser))
284instance P.Pretty a => P.Pretty (Listing a) where
285 pretty dealiaser _ (Listing explicits open) =
286 let dots = [if open then P.text ".." else P.empty]
288 P.parens (P.commaCat (map (P.pretty dealiaser False) explicits ++ dots))
291instance P.Pretty Value where
292 pretty dealiaser _ portable =
301 P.text name <> P.pretty dealiaser False ctors
304-- BINARY SERIALIZATION
306instance Binary Canonical where
307 put (Canonical home name) =
310 putWord8 0 >> put name
313 putWord8 1 >> put path >> put name
316 putWord8 2 >> put path >> put name
319 putWord8 3 >> put name
324 0 -> Canonical BuiltIn <$> get
325 1 -> Canonical . Module <$> get <*> get
326 2 -> Canonical . TopLevel <$> get <*> get
327 3 -> Canonical Local <$> get
328 _ -> error "Unexpected tag when deserializing canonical variable"
331instance Binary Value where
335 putWord8 0 >> put name
338 putWord8 1 >> put name
341 putWord8 2 >> put name >> put ctors
348 2 -> Union <$> get <*> get
349 _ -> error "Error reading valid import/export information from serialized string"
352instance (Binary a) => Binary (Listing a) where
353 put (Listing explicits open) =
354 put explicits >> put open
356 get = Listing <$> get <*> get
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