1module AST.Variable where
3import Control.Applicative ((<$>), (<*>))
5import qualified Data.Map as Map
6import qualified Data.Maybe as Maybe
7import Text.PrettyPrint as P
9import qualified AST.Helpers as Help
10import qualified AST.Module.Name as ModuleName
11import qualified Reporting.PrettyPrint as P
16newtype Raw = Raw String
17 deriving (Eq, Ord, Show)
22data TopLevel = TopLevelVar
23 { topHome :: ModuleName.Canonical
26 deriving (Eq, Ord, Show)
34 | Module ModuleName.Canonical
35 | TopLevel ModuleName.Canonical
37 deriving (Eq, Ord, Show)
40data Canonical = Canonical
44 deriving (Eq, Ord, Show)
47local :: String -> Canonical
52topLevel :: ModuleName.Canonical -> String -> Canonical
54 Canonical (TopLevel home) x
57builtin :: String -> Canonical
62fromModule :: ModuleName.Canonical -> String -> Canonical
64 Canonical (Module home) name
67inCore :: ModuleName.Raw -> String -> Canonical
69 Canonical (Module (ModuleName.inCore home)) name
72-- VARIABLE RECOGNIZERS
74isLocalHome :: Home -> Bool
90is :: ModuleName.Raw -> String -> Canonical -> Bool
92 var == inCore home name
95isJson :: Canonical -> Bool
97 is ["Json", "Encode"] "Value"
100isMaybe :: Canonical -> Bool
104isArray :: Canonical -> Bool
109isTask :: Canonical -> Bool
114isSignal :: Canonical -> Bool
116 is ["Signal"] "Signal"
119isList :: Canonical -> Bool
121 v == Canonical BuiltIn "List"
124isTuple :: Canonical -> Bool
127 Canonical BuiltIn name -> Help.isTuple name
131isPrimitive :: Canonical -> Bool
134 Canonical BuiltIn name ->
135 name `elem` ["Int","Float","String","Bool"]
140isPrim :: String -> Canonical -> Bool
143 Canonical BuiltIn name -> name == prim
147isText :: Canonical -> Bool
154class ToString a where
155 toString :: a -> String
158instance ToString Raw where
159 toString (Raw name) =
163instance ToString Canonical where
164 toString (Canonical home name) =
170 ModuleName.canonicalToString moduleName ++ "." ++ name
181-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
182data Listing a = Listing
186 deriving (Eq, Ord, Show)
189openListing :: Listing a
194closedListing :: Listing a
199listing :: [a] -> Listing a
204-- | A value that can be imported or exported
208 | Union !String !(Listing String)
209 deriving (Eq, Ord, Show)
212-- CATEGORIZING VALUES
214getValues :: [Value] -> [String]
216 Maybe.mapMaybe getValue values
219getValue :: Value -> Maybe String
222 Value name -> Just name
227getAliases :: [Value] -> [String]
229 Maybe.mapMaybe getAlias values
232getAlias :: Value -> Maybe String
236 Alias name -> Just name
240getUnions :: [Value] -> [(String, Listing String)]
242 Maybe.mapMaybe getUnion values
245getUnion :: Value -> Maybe (String, Listing String)
250 Union name ctors -> Just (name, ctors)
255instance P.Pretty Raw where
256 pretty _ _ (Raw name) =
258 then P.parens (P.text name)
262instance P.Pretty Canonical where
263 pretty dealiaser _ var =
267 P.text (maybe name id (Map.lookup name dealiaser))
270instance P.Pretty a => P.Pretty (Listing a) where
271 pretty dealiaser _ (Listing explicits open) =
272 let dots = [if open then P.text ".." else P.empty]
274 P.parens (P.commaCat (map (P.pretty dealiaser False) explicits ++ dots))
277instance P.Pretty Value where
278 pretty dealiaser _ portable =
287 P.text name <> P.pretty dealiaser False ctors
290-- BINARY SERIALIZATION
292instance Binary Canonical where
293 put (Canonical home name) =
296 putWord8 0 >> put name
299 putWord8 1 >> put path >> put name
302 putWord8 2 >> put path >> put name
305 putWord8 3 >> put name
310 0 -> Canonical BuiltIn <$> get
311 1 -> Canonical . Module <$> get <*> get
312 2 -> Canonical . TopLevel <$> get <*> get
313 3 -> Canonical Local <$> get
314 _ -> error "Unexpected tag when deserializing canonical variable"
317instance Binary Value where
321 putWord8 0 >> put name
324 putWord8 1 >> put name
327 putWord8 2 >> put name >> put ctors
334 2 -> Union <$> get <*> get
335 _ -> error "Error reading valid import/export information from serialized string"
338instance (Binary a) => Binary (Listing a) where
339 put (Listing explicits open) =
340 put explicits >> put open
342 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