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"
71isVarying :: Canonical -> Bool
73 is ["Signal"] "Varying"
76isPromise :: Canonical -> Bool
78 is ["Promise"] "Promise"
81isList :: Canonical -> Bool
83 v == Canonical BuiltIn "List"
86isTuple :: Canonical -> Bool
89 Canonical BuiltIn name -> Help.isTuple name
93isPrimitive :: Canonical -> Bool
96 Canonical BuiltIn name -> name `elem` ["Int","Float","String","Bool"]
100isPrim :: String -> Canonical -> Bool
103 Canonical BuiltIn name -> name == prim
107isText :: Canonical -> Bool
114class ToString a where
115 toString :: a -> String
118instance ToString Raw where
119 toString (Raw name) =
123instance ToString Canonical where
124 toString (Canonical home name) =
127 Module path -> List.intercalate "." (path ++ [name])
133-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
134data 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 Pretty Raw where
211instance Pretty Canonical where
213 P.text (toString var)
216instance Pretty a => Pretty (Listing a) where
217 pretty (Listing explicits open) =
218 P.parens (commaCat (map pretty explicits ++ dots))
220 dots = [if open then P.text ".." else P.empty]
223instance Pretty Value where
226 Value name -> P.text name
227 Alias name -> P.text name
229 P.text name <> pretty (ctors { _explicits = map P.text (_explicits ctors) })
232-- BINARY SERIALIZATION
234instance Binary Canonical where
235 put (Canonical home name) =
237 BuiltIn -> putWord8 0 >> put name
238 Module path -> putWord8 1 >> put path >> put name
239 Local -> putWord8 2 >> put name
241 get = do tag <- getWord8
243 0 -> Canonical BuiltIn <$> get
244 1 -> Canonical . Module <$> get <*> get
245 2 -> Canonical Local <$> get
246 _ -> error "Unexpected tag when deserializing canonical variable"
249instance Binary Value where
252 Value name -> putWord8 0 >> put name
253 Alias name -> putWord8 1 >> put name
254 Union name ctors -> putWord8 2 >> put name >> put ctors
256 get = do tag <- getWord8
260 2 -> Union <$> get <*> get
261 _ -> error "Error reading valid import/export information from serialized string"
264instance (Binary a) => Binary (Listing a) where
265 put (Listing explicits open) =
266 put explicits >> put open
268 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