1module AST.Variable where
3import Control.Applicative ((<$>), (<*>))
5import qualified Data.List as List
6import qualified Data.Maybe as Maybe
7import Text.PrettyPrint as P
9import qualified AST.Helpers as Help
10import qualified Reporting.PrettyPrint as P
15newtype Raw = Raw String
16 deriving (Eq, Ord, Show)
23 deriving (Eq, Ord, Show)
26data Canonical = Canonical
30 deriving (Eq, Ord, Show)
33local :: String -> Canonical
34local x = Canonical Local x
37builtin :: String -> Canonical
38builtin x = Canonical BuiltIn x
41fromModule :: [String] -> String -> Canonical
43 Canonical (Module home) name
46-- VARIABLE RECOGNIZERS
48is :: [String] -> String -> Canonical -> Bool
50 var == Canonical (Module home) name
53isJson :: Canonical -> Bool
55 is ["Json", "Encode"] "Value"
58isMaybe :: Canonical -> Bool
62isArray :: Canonical -> Bool
67isTask :: Canonical -> Bool
72isSignal :: Canonical -> Bool
74 is ["Signal"] "Signal"
77isList :: Canonical -> Bool
79 v == Canonical BuiltIn "List"
82isTuple :: Canonical -> Bool
85 Canonical BuiltIn name -> Help.isTuple name
89isPrimitive :: Canonical -> Bool
92 Canonical BuiltIn name ->
93 name `elem` ["Int","Float","String","Bool"]
98isPrim :: String -> Canonical -> Bool
101 Canonical BuiltIn name -> name == prim
105isText :: Canonical -> Bool
112class ToString a where
113 toString :: a -> String
116instance ToString Raw where
117 toString (Raw name) =
121instance ToString Canonical where
122 toString (Canonical home name) =
125 Module path -> List.intercalate "." (path ++ [name])
131-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
132data Listing a = Listing
136 deriving (Eq, Ord, Show)
139openListing :: Listing a
144closedListing :: Listing a
149listing :: [a] -> Listing a
154-- | A value that can be imported or exported
158 | Union !String !(Listing String)
159 deriving (Eq, Ord, Show)
162-- CATEGORIZING VALUES
164getValues :: [Value] -> [String]
166 Maybe.mapMaybe getValue values
169getValue :: Value -> Maybe String
172 Value name -> Just name
177getAliases :: [Value] -> [String]
179 Maybe.mapMaybe getAlias values
182getAlias :: Value -> Maybe String
186 Alias name -> Just name
190getUnions :: [Value] -> [(String, Listing String)]
192 Maybe.mapMaybe getUnion values
195getUnion :: Value -> Maybe (String, Listing String)
200 Union name ctors -> Just (name, ctors)
205instance P.Pretty Raw where
206 pretty _ (Raw name) =
208 then P.parens (P.text name)
212instance P.Pretty Canonical where
214 P.text (toString var)
217instance P.Pretty a => P.Pretty (Listing a) where
218 pretty _ (Listing explicits open) =
219 let dots = [if open then P.text ".." else P.empty]
221 P.parens (P.commaCat (map (P.pretty False) explicits ++ dots))
224instance P.Pretty Value where
234 P.text name <> P.pretty False ctors
237-- BINARY SERIALIZATION
239instance Binary Canonical where
240 put (Canonical home name) =
243 putWord8 0 >> put name
246 putWord8 1 >> put path >> put name
249 putWord8 2 >> put name
254 0 -> Canonical BuiltIn <$> get
255 1 -> Canonical . Module <$> get <*> get
256 2 -> Canonical Local <$> get
257 _ -> error "Unexpected tag when deserializing canonical variable"
260instance Binary Value where
264 putWord8 0 >> put name
267 putWord8 1 >> put name
270 putWord8 2 >> put name >> put ctors
277 2 -> Union <$> get <*> get
278 _ -> error "Error reading valid import/export information from serialized string"
281instance (Binary a) => Binary (Listing a) where
282 put (Listing explicits open) =
283 put explicits >> put open
285 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