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)
22data TopLevel = TopLevelVar
23 { topHome :: ModuleName.Canonical
26 deriving (Eq, Ord, Show)
34 | Module ModuleName.Canonical
35 | TopLevel ModuleName.Canonical
37 deriving (Eq, Ord, Show)
40data Canonical = Canonical
44 deriving (Eq, Ord, Show)
47local :: String -> Canonical
52topLevel :: ModuleName.Canonical -> String -> Canonical
54 Canonical (TopLevel home) x
57builtin :: String -> Canonical
62fromModule :: ModuleName.Canonical -> String -> Canonical
64 Canonical (Module home) name
67inCore :: ModuleName.Raw -> String -> Canonical
69 Canonical (Module (ModuleName.inCore home)) name
72-- VARIABLE RECOGNIZERS
74is :: ModuleName.Raw -> String -> Canonical -> Bool
76 var == inCore home name
79isJson :: Canonical -> Bool
81 is ["Json", "Encode"] "Value"
84isMaybe :: Canonical -> Bool
88isArray :: Canonical -> Bool
93isTask :: Canonical -> Bool
98isSignal :: Canonical -> Bool
100 is ["Signal"] "Signal"
103isList :: Canonical -> Bool
105 v == Canonical BuiltIn "List"
108isTuple :: Canonical -> Bool
111 Canonical BuiltIn name -> Help.isTuple name
115isPrimitive :: Canonical -> Bool
118 Canonical BuiltIn name ->
119 name `elem` ["Int","Float","String","Bool"]
124isPrim :: String -> Canonical -> Bool
127 Canonical BuiltIn name -> name == prim
131isText :: Canonical -> Bool
138class ToString a where
139 toString :: a -> String
142instance ToString Raw where
143 toString (Raw name) =
147instance ToString Canonical where
148 toString (Canonical home name) =
154 ModuleName.canonicalToString moduleName ++ "." ++ name
165-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
166data Listing a = Listing
170 deriving (Eq, Ord, Show)
173openListing :: Listing a
178closedListing :: Listing a
183listing :: [a] -> Listing a
188-- | A value that can be imported or exported
192 | Union !String !(Listing String)
193 deriving (Eq, Ord, Show)
196-- CATEGORIZING VALUES
198getValues :: [Value] -> [String]
200 Maybe.mapMaybe getValue values
203getValue :: Value -> Maybe String
206 Value name -> Just name
211getAliases :: [Value] -> [String]
213 Maybe.mapMaybe getAlias values
216getAlias :: Value -> Maybe String
220 Alias name -> Just name
224getUnions :: [Value] -> [(String, Listing String)]
226 Maybe.mapMaybe getUnion values
229getUnion :: Value -> Maybe (String, Listing String)
234 Union name ctors -> Just (name, ctors)
239instance P.Pretty Raw where
240 pretty _ _ (Raw name) =
242 then P.parens (P.text name)
246instance P.Pretty Canonical where
247 pretty dealiaser _ var =
251 P.text (maybe name id (Map.lookup name dealiaser))
254instance P.Pretty a => P.Pretty (Listing a) where
255 pretty dealiaser _ (Listing explicits open) =
256 let dots = [if open then P.text ".." else P.empty]
258 P.parens (P.commaCat (map (P.pretty dealiaser False) explicits ++ dots))
261instance P.Pretty Value where
262 pretty dealiaser _ portable =
271 P.text name <> P.pretty dealiaser False ctors
274-- BINARY SERIALIZATION
276instance Binary Canonical where
277 put (Canonical home name) =
280 putWord8 0 >> put name
283 putWord8 1 >> put path >> put name
286 putWord8 2 >> put path >> put name
289 putWord8 3 >> put name
294 0 -> Canonical BuiltIn <$> get
295 1 -> Canonical . Module <$> get <*> get
296 2 -> Canonical . TopLevel <$> get <*> get
297 3 -> Canonical Local <$> get
298 _ -> error "Unexpected tag when deserializing canonical variable"
301instance Binary Value where
305 putWord8 0 >> put name
308 putWord8 1 >> put name
311 putWord8 2 >> put name >> put ctors
318 2 -> Union <$> get <*> get
319 _ -> error "Error reading valid import/export information from serialized string"
322instance (Binary a) => Binary (Listing a) where
323 put (Listing explicits open) =
324 put explicits >> put open
326 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