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"
118isList :: Canonical -> Bool
120 v == Canonical BuiltIn "List"
123isTuple :: Canonical -> Bool
126 Canonical BuiltIn name -> Help.isTuple name
130isPrimitive :: Canonical -> Bool
133 Canonical BuiltIn name ->
134 name `elem` ["Int","Float","String","Bool"]
139isPrim :: String -> Canonical -> Bool
142 Canonical BuiltIn name -> name == prim
146isText :: Canonical -> Bool
153class ToString a where
154 toString :: a -> String
157instance ToString Raw where
158 toString (Raw name) =
162instance ToString Canonical where
163 toString (Canonical home name) =
169 ModuleName.canonicalToString moduleName ++ "." ++ name
180-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
181data Listing a = Listing
185 deriving (Eq, Ord, Show)
188openListing :: Listing a
193closedListing :: Listing a
198listing :: [a] -> Listing a
203-- | A value that can be imported or exported
207 | Union !String !(Listing String)
208 deriving (Eq, Ord, Show)
211-- CATEGORIZING VALUES
213getValues :: [Value] -> [String]
215 Maybe.mapMaybe getValue values
218getValue :: Value -> Maybe String
221 Value name -> Just name
226getAliases :: [Value] -> [String]
228 Maybe.mapMaybe getAlias values
231getAlias :: Value -> Maybe String
235 Alias name -> Just name
239getUnions :: [Value] -> [(String, Listing String)]
241 Maybe.mapMaybe getUnion values
244getUnion :: Value -> Maybe (String, Listing String)
249 Union name ctors -> Just (name, ctors)
254instance P.Pretty Raw where
255 pretty _ _ (Raw name) =
257 then P.parens (P.text name)
261instance P.Pretty Canonical where
262 pretty dealiaser _ var =
266 P.text (maybe name id (Map.lookup name dealiaser))
269instance P.Pretty a => P.Pretty (Listing a) where
270 pretty dealiaser _ (Listing explicits open) =
271 let dots = [if open then P.text ".." else P.empty]
273 P.parens (P.commaCat (map (P.pretty dealiaser False) explicits ++ dots))
276instance P.Pretty Value where
277 pretty dealiaser _ portable =
286 P.text name <> P.pretty dealiaser False ctors
289-- BINARY SERIALIZATION
291instance Binary Canonical where
292 put (Canonical home name) =
295 putWord8 0 >> put name
298 putWord8 1 >> put path >> put name
301 putWord8 2 >> put path >> put name
304 putWord8 3 >> put name
309 0 -> Canonical BuiltIn <$> get
310 1 -> Canonical . Module <$> get <*> get
311 2 -> Canonical . TopLevel <$> get <*> get
312 3 -> Canonical Local <$> get
313 _ -> error "Unexpected tag when deserializing canonical variable"
316instance Binary Value where
320 putWord8 0 >> put name
323 putWord8 1 >> put name
326 putWord8 2 >> put name >> put ctors
333 2 -> Union <$> get <*> get
334 _ -> error "Error reading valid import/export information from serialized string"
337instance (Binary a) => Binary (Listing a) where
338 put (Listing explicits open) =
339 put explicits >> put open
341 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