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)
24 deriving (Eq, Ord, Show)
27data Canonical = Canonical
31 deriving (Eq, Ord, Show)
34local :: String -> Canonical
35local x = Canonical Local x
38builtin :: String -> Canonical
39builtin x = Canonical BuiltIn x
42fromModule :: [String] -> String -> Canonical
44 Canonical (Module home) name
47-- VARIABLE RECOGNIZERS
49is :: [String] -> String -> Canonical -> Bool
51 var == Canonical (Module home) name
54isJson :: Canonical -> Bool
56 is ["Json", "Encode"] "Value"
59isMaybe :: Canonical -> Bool
63isArray :: Canonical -> Bool
68isTask :: Canonical -> Bool
73isSignal :: Canonical -> Bool
75 is ["Signal"] "Signal"
78isList :: Canonical -> Bool
80 v == Canonical BuiltIn "List"
83isTuple :: Canonical -> Bool
86 Canonical BuiltIn name -> Help.isTuple name
90isPrimitive :: Canonical -> Bool
93 Canonical BuiltIn name ->
94 name `elem` ["Int","Float","String","Bool"]
99isPrim :: String -> Canonical -> Bool
102 Canonical BuiltIn name -> name == prim
106isText :: Canonical -> Bool
113class ToString a where
114 toString :: a -> String
117instance ToString Raw where
118 toString (Raw name) =
122instance ToString Canonical where
123 toString (Canonical home name) =
126 Module path -> List.intercalate "." (path ++ [name])
132-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
133data Listing a = Listing
137 deriving (Eq, Ord, Show)
140openListing :: Listing a
145closedListing :: Listing a
150listing :: [a] -> Listing a
155-- | A value that can be imported or exported
159 | Union !String !(Listing String)
160 deriving (Eq, Ord, Show)
163-- CATEGORIZING VALUES
165getValues :: [Value] -> [String]
167 Maybe.mapMaybe getValue values
170getValue :: Value -> Maybe String
173 Value name -> Just name
178getAliases :: [Value] -> [String]
180 Maybe.mapMaybe getAlias values
183getAlias :: Value -> Maybe String
187 Alias name -> Just name
191getUnions :: [Value] -> [(String, Listing String)]
193 Maybe.mapMaybe getUnion values
196getUnion :: Value -> Maybe (String, Listing String)
201 Union name ctors -> Just (name, ctors)
206instance P.Pretty Raw where
207 pretty _ _ (Raw name) =
209 then P.parens (P.text name)
213instance P.Pretty Canonical where
214 pretty dealiaser _ var =
218 P.text (maybe name id (Map.lookup name dealiaser))
221instance P.Pretty a => P.Pretty (Listing a) where
222 pretty dealiaser _ (Listing explicits open) =
223 let dots = [if open then P.text ".." else P.empty]
225 P.parens (P.commaCat (map (P.pretty dealiaser False) explicits ++ dots))
228instance P.Pretty Value where
229 pretty dealiaser _ portable =
238 P.text name <> P.pretty dealiaser False ctors
241-- BINARY SERIALIZATION
243instance Binary Canonical where
244 put (Canonical home name) =
247 putWord8 0 >> put name
250 putWord8 1 >> put path >> put name
253 putWord8 2 >> put name
258 0 -> Canonical BuiltIn <$> get
259 1 -> Canonical . Module <$> get <*> get
260 2 -> Canonical Local <$> get
261 _ -> error "Unexpected tag when deserializing canonical variable"
264instance Binary Value where
268 putWord8 0 >> put name
271 putWord8 1 >> put name
274 putWord8 2 >> put name >> put ctors
281 2 -> Union <$> get <*> get
282 _ -> error "Error reading valid import/export information from serialized string"
285instance (Binary a) => Binary (Listing a) where
286 put (Listing explicits open) =
287 put explicits >> put open
289 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