1module AST.Variable where
3import Control.Applicative ((<$>), (<*>))
5import qualified Data.List as List
6import qualified Data.Maybe as Maybe
7import Text.PrettyPrint as P
8import qualified AST.Helpers as Help
14newtype Raw = Raw String
15 deriving (Eq,Ord,Show)
22 deriving (Eq,Ord,Show)
25data Canonical = Canonical
29 deriving (Eq,Ord,Show)
32local :: String -> Canonical
33local x = Canonical Local x
36builtin :: String -> Canonical
37builtin x = Canonical BuiltIn x
40fromModule :: [String] -> String -> Canonical
42 Canonical (Module home) name
45-- VARIABLE RECOGNIZERS
47is :: [String] -> String -> Canonical -> Bool
49 var == Canonical (Module home) name
52isJson :: Canonical -> Bool
54 is ["Json", "Encode"] "Value"
57isMaybe :: Canonical -> Bool
61isArray :: Canonical -> Bool
66isStream :: Canonical -> Bool
68 is ["Signal"] "Stream"
71isAddress :: Canonical -> Bool
73 is ["Stream"] "Address"
76isVarying :: Canonical -> Bool
78 is ["Signal"] "Varying"
81isPromise :: Canonical -> Bool
83 is ["Promise"] "Promise"
86isResult :: Canonical -> Bool
88 is ["Result"] "Result"
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 -> name `elem` ["Int","Float","String","Bool"]
110isPrim :: String -> Canonical -> Bool
113 Canonical BuiltIn name -> name == prim
117isText :: Canonical -> Bool
124class ToString a where
125 toString :: a -> String
128instance ToString Raw where
129 toString (Raw name) =
133instance ToString Canonical where
134 toString (Canonical home name) =
137 Module path -> List.intercalate "." (path ++ [name])
143-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
144data Listing a = Listing
147 } deriving (Eq,Ord,Show)
150openListing :: Listing a
155closedListing :: Listing a
160listing :: [a] -> Listing a
165-- | A value that can be imported or exported
169 | Union !String !(Listing String)
170 deriving (Eq,Ord,Show)
173-- CATEGORIZING VALUES
175getValues :: [Value] -> [String]
177 Maybe.mapMaybe getValue values
180getValue :: Value -> Maybe String
183 Value name -> Just name
188getAliases :: [Value] -> [String]
190 Maybe.mapMaybe getAlias values
193getAlias :: Value -> Maybe String
197 Alias name -> Just name
201getUnions :: [Value] -> [(String, Listing String)]
203 Maybe.mapMaybe getUnion values
206getUnion :: Value -> Maybe (String, Listing String)
211 Union name ctors -> Just (name, ctors)
216instance Pretty Raw where
221instance Pretty Canonical where
223 P.text (toString var)
226instance Pretty a => Pretty (Listing a) where
227 pretty (Listing explicits open) =
228 P.parens (commaCat (map pretty explicits ++ dots))
230 dots = [if open then P.text ".." else P.empty]
233instance Pretty Value where
236 Value name -> P.text name
237 Alias name -> P.text name
239 P.text name <> pretty (ctors { _explicits = map P.text (_explicits ctors) })
242-- BINARY SERIALIZATION
244instance Binary Canonical where
245 put (Canonical home name) =
247 BuiltIn -> putWord8 0 >> put name
248 Module path -> putWord8 1 >> put path >> put name
249 Local -> putWord8 2 >> put name
251 get = do tag <- getWord8
253 0 -> Canonical BuiltIn <$> get
254 1 -> Canonical . Module <$> get <*> get
255 2 -> Canonical Local <$> get
256 _ -> error "Unexpected tag when deserializing canonical variable"
259instance Binary Value where
262 Value name -> putWord8 0 >> put name
263 Alias name -> putWord8 1 >> put name
264 Union name ctors -> putWord8 2 >> put name >> put ctors
266 get = do tag <- getWord8
270 2 -> Union <$> get <*> get
271 _ -> error "Error reading valid import/export information from serialized string"
274instance (Binary a) => Binary (Listing a) where
275 put (Listing explicits open) =
276 put explicits >> put open
278 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