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 { topVarModule :: ModuleName.Canonical
23 , topVarName :: String
24 } deriving (Eq, Ord, Show)
29 | Module ModuleName.Canonical
30 | TopLevel ModuleName.Canonical
32 deriving (Eq, Ord, Show)
35data Canonical = Canonical
39 deriving (Eq, Ord, Show)
42local :: String -> Canonical
47topLevel :: ModuleName.Canonical -> String -> Canonical
49 Canonical (TopLevel home) x
52builtin :: String -> Canonical
57fromModule :: ModuleName.Canonical -> String -> Canonical
59 Canonical (Module home) name
62inCore :: ModuleName.Raw -> String -> Canonical
64 Canonical (Module (ModuleName.inCore home)) name
67-- VARIABLE RECOGNIZERS
69is :: ModuleName.Raw -> String -> Canonical -> Bool
71 var == inCore home name
74isJson :: Canonical -> Bool
76 is ["Json", "Encode"] "Value"
79isMaybe :: Canonical -> Bool
83isArray :: Canonical -> Bool
88isTask :: Canonical -> Bool
93isSignal :: Canonical -> Bool
95 is ["Signal"] "Signal"
98isList :: Canonical -> Bool
100 v == Canonical BuiltIn "List"
103isTuple :: Canonical -> Bool
106 Canonical BuiltIn name -> Help.isTuple name
110isPrimitive :: Canonical -> Bool
113 Canonical BuiltIn name ->
114 name `elem` ["Int","Float","String","Bool"]
119isPrim :: String -> Canonical -> Bool
122 Canonical BuiltIn name -> name == prim
126isText :: Canonical -> Bool
133class ToString a where
134 toString :: a -> String
137instance ToString Raw where
138 toString (Raw name) =
142instance ToString Canonical where
143 toString (Canonical home name) =
149 ModuleName.canonicalToString moduleName ++ "." ++ name
160-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
161data Listing a = Listing
165 deriving (Eq, Ord, Show)
168openListing :: Listing a
173closedListing :: Listing a
178listing :: [a] -> Listing a
183-- | A value that can be imported or exported
187 | Union !String !(Listing String)
188 deriving (Eq, Ord, Show)
191-- CATEGORIZING VALUES
193getValues :: [Value] -> [String]
195 Maybe.mapMaybe getValue values
198getValue :: Value -> Maybe String
201 Value name -> Just name
206getAliases :: [Value] -> [String]
208 Maybe.mapMaybe getAlias values
211getAlias :: Value -> Maybe String
215 Alias name -> Just name
219getUnions :: [Value] -> [(String, Listing String)]
221 Maybe.mapMaybe getUnion values
224getUnion :: Value -> Maybe (String, Listing String)
229 Union name ctors -> Just (name, ctors)
234instance P.Pretty Raw where
235 pretty _ _ (Raw name) =
237 then P.parens (P.text name)
241instance P.Pretty Canonical where
242 pretty dealiaser _ var =
246 P.text (maybe name id (Map.lookup name dealiaser))
249instance P.Pretty a => P.Pretty (Listing a) where
250 pretty dealiaser _ (Listing explicits open) =
251 let dots = [if open then P.text ".." else P.empty]
253 P.parens (P.commaCat (map (P.pretty dealiaser False) explicits ++ dots))
256instance P.Pretty Value where
257 pretty dealiaser _ portable =
266 P.text name <> P.pretty dealiaser False ctors
269-- BINARY SERIALIZATION
271instance Binary Canonical where
272 put (Canonical home name) =
275 putWord8 0 >> put name
278 putWord8 1 >> put path >> put name
281 putWord8 2 >> put path >> put name
284 putWord8 3 >> put name
289 0 -> Canonical BuiltIn <$> get
290 1 -> Canonical . Module <$> get <*> get
291 2 -> Canonical . TopLevel <$> get <*> get
292 3 -> Canonical Local <$> get
293 _ -> error "Unexpected tag when deserializing canonical variable"
296instance Binary Value where
300 putWord8 0 >> put name
303 putWord8 1 >> put name
306 putWord8 2 >> put name >> put ctors
313 2 -> Union <$> get <*> get
314 _ -> error "Error reading valid import/export information from serialized string"
317instance (Binary a) => Binary (Listing a) where
318 put (Listing explicits open) =
319 put explicits >> put open
321 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