1module AST.Variable where
3import Control.Applicative ((<$>), (<*>))
5import qualified Data.List as List
6import qualified Data.Map as Map
7import qualified Data.Maybe as Maybe
8import Text.PrettyPrint as P
10import qualified AST.Helpers as Help
11import qualified Reporting.PrettyPrint as P
16newtype Raw = Raw String
17 deriving (Eq, Ord, Show)
22 { topVarModule :: [String]
23 , topVarName :: String
24 } deriving (Eq, Ord, Show)
32 deriving (Eq, Ord, Show)
35data Canonical = Canonical
39 deriving (Eq, Ord, Show)
42local :: String -> Canonical
47topLevel :: [String] -> String -> Canonical
49 Canonical (TopLevel names) x
52builtin :: String -> Canonical
57fromModule :: [String] -> String -> Canonical
59 Canonical (Module home) name
62-- VARIABLE RECOGNIZERS
64is :: [String] -> String -> Canonical -> Bool
66 var == Canonical (Module home) name
69isJson :: Canonical -> Bool
71 is ["Json", "Encode"] "Value"
74isMaybe :: Canonical -> Bool
78isArray :: Canonical -> Bool
83isTask :: Canonical -> Bool
88isSignal :: Canonical -> Bool
90 is ["Signal"] "Signal"
93isList :: Canonical -> Bool
95 v == Canonical BuiltIn "List"
98isTuple :: Canonical -> Bool
101 Canonical BuiltIn name -> Help.isTuple name
105isPrimitive :: Canonical -> Bool
108 Canonical BuiltIn name ->
109 name `elem` ["Int","Float","String","Bool"]
114isPrim :: String -> Canonical -> Bool
117 Canonical BuiltIn name -> name == prim
121isText :: Canonical -> Bool
128class ToString a where
129 toString :: a -> String
132instance ToString Raw where
133 toString (Raw name) =
137instance ToString Canonical where
138 toString (Canonical home name) =
144 List.intercalate "." (path ++ [name])
155-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
156data Listing a = Listing
160 deriving (Eq, Ord, Show)
163openListing :: Listing a
168closedListing :: Listing a
173listing :: [a] -> Listing a
178-- | A value that can be imported or exported
182 | Union !String !(Listing String)
183 deriving (Eq, Ord, Show)
186-- CATEGORIZING VALUES
188getValues :: [Value] -> [String]
190 Maybe.mapMaybe getValue values
193getValue :: Value -> Maybe String
196 Value name -> Just name
201getAliases :: [Value] -> [String]
203 Maybe.mapMaybe getAlias values
206getAlias :: Value -> Maybe String
210 Alias name -> Just name
214getUnions :: [Value] -> [(String, Listing String)]
216 Maybe.mapMaybe getUnion values
219getUnion :: Value -> Maybe (String, Listing String)
224 Union name ctors -> Just (name, ctors)
229instance P.Pretty Raw where
230 pretty _ _ (Raw name) =
232 then P.parens (P.text name)
236instance P.Pretty Canonical where
237 pretty dealiaser _ var =
241 P.text (maybe name id (Map.lookup name dealiaser))
244instance P.Pretty a => P.Pretty (Listing a) where
245 pretty dealiaser _ (Listing explicits open) =
246 let dots = [if open then P.text ".." else P.empty]
248 P.parens (P.commaCat (map (P.pretty dealiaser False) explicits ++ dots))
251instance P.Pretty Value where
252 pretty dealiaser _ portable =
261 P.text name <> P.pretty dealiaser False ctors
264-- BINARY SERIALIZATION
266instance Binary Canonical where
267 put (Canonical home name) =
270 putWord8 0 >> put name
273 putWord8 1 >> put path >> put name
276 putWord8 2 >> put path >> put name
279 putWord8 3 >> put name
284 0 -> Canonical BuiltIn <$> get
285 1 -> Canonical . Module <$> get <*> get
286 2 -> Canonical . TopLevel <$> get <*> get
287 3 -> Canonical Local <$> get
288 _ -> error "Unexpected tag when deserializing canonical variable"
291instance Binary Value where
295 putWord8 0 >> put name
298 putWord8 1 >> put name
301 putWord8 2 >> put name >> put ctors
308 2 -> Union <$> get <*> get
309 _ -> error "Error reading valid import/export information from serialized string"
312instance (Binary a) => Binary (Listing a) where
313 put (Listing explicits open) =
314 put explicits >> put open
316 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