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
66isSignal :: Canonical -> Bool
68 is ["Signal"] "Signal"
71isPromise :: Canonical -> Bool
73 is ["Promise"] "Promise"
76isList :: Canonical -> Bool
78 v == Canonical BuiltIn "List"
81isTuple :: Canonical -> Bool
84 Canonical BuiltIn name -> Help.isTuple name
88isPrimitive :: Canonical -> Bool
91 Canonical BuiltIn name -> name `elem` ["Int","Float","String","Bool"]
95isPrim :: String -> Canonical -> Bool
98 Canonical BuiltIn name -> name == prim
102isText :: Canonical -> Bool
109class ToString a where
110 toString :: a -> String
113instance ToString Raw where
114 toString (Raw name) =
118instance ToString Canonical where
119 toString (Canonical home name) =
122 Module path -> List.intercalate "." (path ++ [name])
128-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
129data Listing a = Listing
132 } deriving (Eq,Ord,Show)
135openListing :: Listing a
140closedListing :: Listing a
145listing :: [a] -> Listing a
150-- | A value that can be imported or exported
154 | Union !String !(Listing String)
155 deriving (Eq,Ord,Show)
158-- CATEGORIZING VALUES
160getValues :: [Value] -> [String]
162 Maybe.mapMaybe getValue values
165getValue :: Value -> Maybe String
168 Value name -> Just name
173getAliases :: [Value] -> [String]
175 Maybe.mapMaybe getAlias values
178getAlias :: Value -> Maybe String
182 Alias name -> Just name
186getUnions :: [Value] -> [(String, Listing String)]
188 Maybe.mapMaybe getUnion values
191getUnion :: Value -> Maybe (String, Listing String)
196 Union name ctors -> Just (name, ctors)
201instance Pretty Raw where
206instance Pretty Canonical where
208 P.text (toString var)
211instance Pretty a => Pretty (Listing a) where
212 pretty (Listing explicits open) =
213 P.parens (commaCat (map pretty explicits ++ dots))
215 dots = [if open then P.text ".." else P.empty]
218instance Pretty Value where
221 Value name -> P.text name
222 Alias name -> P.text name
224 P.text name <> pretty (ctors { _explicits = map P.text (_explicits ctors) })
227-- BINARY SERIALIZATION
229instance Binary Canonical where
230 put (Canonical home name) =
232 BuiltIn -> putWord8 0 >> put name
233 Module path -> putWord8 1 >> put path >> put name
234 Local -> putWord8 2 >> put name
236 get = do tag <- getWord8
238 0 -> Canonical BuiltIn <$> get
239 1 -> Canonical . Module <$> get <*> get
240 2 -> Canonical Local <$> get
241 _ -> error "Unexpected tag when deserializing canonical variable"
244instance Binary Value where
247 Value name -> putWord8 0 >> put name
248 Alias name -> putWord8 1 >> put name
249 Union name ctors -> putWord8 2 >> put name >> put ctors
251 get = do tag <- getWord8
255 2 -> Union <$> get <*> get
256 _ -> error "Error reading valid import/export information from serialized string"
259instance (Binary a) => Binary (Listing a) where
260 put (Listing explicits open) =
261 put explicits >> put open
263 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