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)
22 | Module ModuleName.Canonical
23 | TopLevel ModuleName.Canonical
25 deriving (Eq, Ord, Show)
28data Canonical = Canonical
32 deriving (Eq, Ord, Show)
35local :: String -> Canonical
40topLevel :: ModuleName.Canonical -> String -> Canonical
42 Canonical (TopLevel home) x
45builtin :: String -> Canonical
50fromModule :: ModuleName.Canonical -> String -> Canonical
52 Canonical (Module home) name
55inCore :: ModuleName.Raw -> String -> Canonical
57 Canonical (Module (ModuleName.inCore home)) name
60-- VARIABLE RECOGNIZERS
62is :: ModuleName.Raw -> String -> Canonical -> Bool
64 var == inCore home name
67isJson :: Canonical -> Bool
69 is ["Json", "Encode"] "Value"
72isMaybe :: Canonical -> Bool
76isArray :: Canonical -> Bool
81isTask :: Canonical -> Bool
86isSignal :: Canonical -> Bool
88 is ["Signal"] "Signal"
91isList :: Canonical -> Bool
93 v == Canonical BuiltIn "List"
96isTuple :: Canonical -> Bool
99 Canonical BuiltIn name -> Help.isTuple name
103isPrimitive :: Canonical -> Bool
106 Canonical BuiltIn name ->
107 name `elem` ["Int","Float","String","Bool"]
112isPrim :: String -> Canonical -> Bool
115 Canonical BuiltIn name -> name == prim
119isText :: Canonical -> Bool
126class ToString a where
127 toString :: a -> String
130instance ToString Raw where
131 toString (Raw name) =
135instance ToString Canonical where
136 toString (Canonical home name) =
142 ModuleName.canonicalToString moduleName ++ "." ++ name
153-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
154data Listing a = Listing
158 deriving (Eq, Ord, Show)
161openListing :: Listing a
166closedListing :: Listing a
171listing :: [a] -> Listing a
176-- | A value that can be imported or exported
180 | Union !String !(Listing String)
181 deriving (Eq, Ord, Show)
184-- CATEGORIZING VALUES
186getValues :: [Value] -> [String]
188 Maybe.mapMaybe getValue values
191getValue :: Value -> Maybe String
194 Value name -> Just name
199getAliases :: [Value] -> [String]
201 Maybe.mapMaybe getAlias values
204getAlias :: Value -> Maybe String
208 Alias name -> Just name
212getUnions :: [Value] -> [(String, Listing String)]
214 Maybe.mapMaybe getUnion values
217getUnion :: Value -> Maybe (String, Listing String)
222 Union name ctors -> Just (name, ctors)
227instance P.Pretty Raw where
228 pretty _ _ (Raw name) =
230 then P.parens (P.text name)
234instance P.Pretty Canonical where
235 pretty dealiaser _ var =
239 P.text (maybe name id (Map.lookup name dealiaser))
242instance P.Pretty a => P.Pretty (Listing a) where
243 pretty dealiaser _ (Listing explicits open) =
244 let dots = [if open then P.text ".." else P.empty]
246 P.parens (P.commaCat (map (P.pretty dealiaser False) explicits ++ dots))
249instance P.Pretty Value where
250 pretty dealiaser _ portable =
259 P.text name <> P.pretty dealiaser False ctors
262-- BINARY SERIALIZATION
264instance Binary Canonical where
265 put (Canonical home name) =
268 putWord8 0 >> put name
271 putWord8 1 >> put path >> put name
274 putWord8 2 >> put path >> put name
277 putWord8 3 >> put name
282 0 -> Canonical BuiltIn <$> get
283 1 -> Canonical . Module <$> get <*> get
284 2 -> Canonical . TopLevel <$> get <*> get
285 3 -> Canonical Local <$> get
286 _ -> error "Unexpected tag when deserializing canonical variable"
289instance Binary Value where
293 putWord8 0 >> put name
296 putWord8 1 >> put name
299 putWord8 2 >> put name >> put ctors
306 2 -> Union <$> get <*> get
307 _ -> error "Error reading valid import/export information from serialized string"
310instance (Binary a) => Binary (Listing a) where
311 put (Listing explicits open) =
312 put explicits >> put open
314 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