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
40topLevel :: [String] -> String -> Canonical
42 Canonical (TopLevel names) x
45builtin :: String -> Canonical
50fromModule :: [String] -> String -> Canonical
52 Canonical (Module home) name
55-- VARIABLE RECOGNIZERS
57is :: [String] -> String -> Canonical -> Bool
59 var == Canonical (Module home) name
62isJson :: Canonical -> Bool
64 is ["Json", "Encode"] "Value"
67isMaybe :: Canonical -> Bool
71isArray :: Canonical -> Bool
76isTask :: Canonical -> Bool
81isSignal :: Canonical -> Bool
83 is ["Signal"] "Signal"
86isList :: Canonical -> Bool
88 v == Canonical BuiltIn "List"
91isTuple :: Canonical -> Bool
94 Canonical BuiltIn name -> Help.isTuple name
98isPrimitive :: Canonical -> Bool
101 Canonical BuiltIn name ->
102 name `elem` ["Int","Float","String","Bool"]
107isPrim :: String -> Canonical -> Bool
110 Canonical BuiltIn name -> name == prim
114isText :: Canonical -> Bool
121class ToString a where
122 toString :: a -> String
125instance ToString Raw where
126 toString (Raw name) =
130instance ToString Canonical where
131 toString (Canonical home name) =
137 List.intercalate "." (path ++ [name])
148-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
149data Listing a = Listing
153 deriving (Eq, Ord, Show)
156openListing :: Listing a
161closedListing :: Listing a
166listing :: [a] -> Listing a
171-- | A value that can be imported or exported
175 | Union !String !(Listing String)
176 deriving (Eq, Ord, Show)
179-- CATEGORIZING VALUES
181getValues :: [Value] -> [String]
183 Maybe.mapMaybe getValue values
186getValue :: Value -> Maybe String
189 Value name -> Just name
194getAliases :: [Value] -> [String]
196 Maybe.mapMaybe getAlias values
199getAlias :: Value -> Maybe String
203 Alias name -> Just name
207getUnions :: [Value] -> [(String, Listing String)]
209 Maybe.mapMaybe getUnion values
212getUnion :: Value -> Maybe (String, Listing String)
217 Union name ctors -> Just (name, ctors)
222instance P.Pretty Raw where
223 pretty _ _ (Raw name) =
225 then P.parens (P.text name)
229instance P.Pretty Canonical where
230 pretty dealiaser _ var =
234 P.text (maybe name id (Map.lookup name dealiaser))
237instance P.Pretty a => P.Pretty (Listing a) where
238 pretty dealiaser _ (Listing explicits open) =
239 let dots = [if open then P.text ".." else P.empty]
241 P.parens (P.commaCat (map (P.pretty dealiaser False) explicits ++ dots))
244instance P.Pretty Value where
245 pretty dealiaser _ portable =
254 P.text name <> P.pretty dealiaser False ctors
257-- BINARY SERIALIZATION
259instance Binary Canonical where
260 put (Canonical home name) =
263 putWord8 0 >> put name
266 putWord8 1 >> put path >> put name
269 putWord8 2 >> put path >> put name
272 putWord8 3 >> put name
277 0 -> Canonical BuiltIn <$> get
278 1 -> Canonical . Module <$> get <*> get
279 2 -> Canonical . TopLevel <$> get <*> get
280 3 -> Canonical Local <$> get
281 _ -> error "Unexpected tag when deserializing canonical variable"
284instance Binary Value where
288 putWord8 0 >> put name
291 putWord8 1 >> put name
294 putWord8 2 >> put name >> put ctors
301 2 -> Union <$> get <*> get
302 _ -> error "Error reading valid import/export information from serialized string"
305instance (Binary a) => Binary (Listing a) where
306 put (Listing explicits open) =
307 put explicits >> put open
309 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