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)
25 deriving (Eq, Ord, Show)
28data Canonical = Canonical
32 deriving (Eq, Ord, Show)
35local :: String -> Canonical
36local x = Canonical Local x
39topLevel :: [String] -> String -> Canonical
40topLevel names x = Canonical (TopLevel names) x
43builtin :: String -> Canonical
44builtin x = Canonical BuiltIn x
47fromModule :: [String] -> String -> Canonical
49 Canonical (Module home) name
52-- VARIABLE RECOGNIZERS
54is :: [String] -> String -> Canonical -> Bool
56 var == Canonical (Module home) name
59isJson :: Canonical -> Bool
61 is ["Json", "Encode"] "Value"
64isMaybe :: Canonical -> Bool
68isArray :: Canonical -> Bool
73isTask :: Canonical -> Bool
78isSignal :: Canonical -> Bool
80 is ["Signal"] "Signal"
83isList :: Canonical -> Bool
85 v == Canonical BuiltIn "List"
88isTuple :: Canonical -> Bool
91 Canonical BuiltIn name -> Help.isTuple name
95isPrimitive :: Canonical -> Bool
98 Canonical BuiltIn name ->
99 name `elem` ["Int","Float","String","Bool"]
104isPrim :: String -> Canonical -> Bool
107 Canonical BuiltIn name -> name == prim
111isText :: Canonical -> Bool
118class ToString a where
119 toString :: a -> String
122instance ToString Raw where
123 toString (Raw name) =
127instance ToString Canonical where
128 toString (Canonical home name) =
131 Module path -> List.intercalate "." (path ++ [name])
138-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
139data Listing a = Listing
143 deriving (Eq, Ord, Show)
146openListing :: Listing a
151closedListing :: Listing a
156listing :: [a] -> Listing a
161-- | A value that can be imported or exported
165 | Union !String !(Listing String)
166 deriving (Eq, Ord, Show)
169-- CATEGORIZING VALUES
171getValues :: [Value] -> [String]
173 Maybe.mapMaybe getValue values
176getValue :: Value -> Maybe String
179 Value name -> Just name
184getAliases :: [Value] -> [String]
186 Maybe.mapMaybe getAlias values
189getAlias :: Value -> Maybe String
193 Alias name -> Just name
197getUnions :: [Value] -> [(String, Listing String)]
199 Maybe.mapMaybe getUnion values
202getUnion :: Value -> Maybe (String, Listing String)
207 Union name ctors -> Just (name, ctors)
212instance P.Pretty Raw where
213 pretty _ _ (Raw name) =
215 then P.parens (P.text name)
219instance P.Pretty Canonical where
220 pretty dealiaser _ var =
224 P.text (maybe name id (Map.lookup name dealiaser))
227instance P.Pretty a => P.Pretty (Listing a) where
228 pretty dealiaser _ (Listing explicits open) =
229 let dots = [if open then P.text ".." else P.empty]
231 P.parens (P.commaCat (map (P.pretty dealiaser False) explicits ++ dots))
234instance P.Pretty Value where
235 pretty dealiaser _ portable =
244 P.text name <> P.pretty dealiaser False ctors
247-- BINARY SERIALIZATION
249instance Binary Canonical where
250 put (Canonical home name) =
253 putWord8 0 >> put name
256 putWord8 1 >> put path >> put name
259 putWord8 2 >> put name
262 putWord8 3 >> put path >> put name
267 0 -> Canonical BuiltIn <$> get
268 1 -> Canonical . Module <$> get <*> get
269 2 -> Canonical Local <$> get
270 3 -> Canonical . TopLevel <$> get <*> get
271 _ -> error "Unexpected tag when deserializing canonical variable"
274instance Binary Value where
278 putWord8 0 >> put name
281 putWord8 1 >> put name
284 putWord8 2 >> put name >> put ctors
291 2 -> Union <$> get <*> get
292 _ -> error "Error reading valid import/export information from serialized string"
295instance (Binary a) => Binary (Listing a) where
296 put (Listing explicits open) =
297 put explicits >> put open
299 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