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
40-- VARIABLE RECOGNIZERS
42is :: [String] -> String -> Canonical -> Bool
44 var == Canonical (Module home) name
47isJson :: Canonical -> Bool
49 is ["Json", "Encode"] "Value"
52isMaybe :: Canonical -> Bool
56isArray :: Canonical -> Bool
61isSignal :: Canonical -> Bool
63 is ["Signal"] "Signal"
66isPromise :: Canonical -> Bool
68 is ["Promise"] "Promise"
71isList :: Canonical -> Bool
73 v == Canonical BuiltIn "List"
76isTuple :: Canonical -> Bool
79 Canonical BuiltIn name -> Help.isTuple name
83isPrimitive :: Canonical -> Bool
86 Canonical BuiltIn name -> name `elem` ["Int","Float","String","Bool"]
90isPrim :: String -> Canonical -> Bool
93 Canonical BuiltIn name -> name == prim
97isText :: Canonical -> Bool
104class ToString a where
105 toString :: a -> String
108instance ToString Raw where
109 toString (Raw name) =
113instance ToString Canonical where
114 toString (Canonical home name) =
117 Module path -> List.intercalate "." (path ++ [name])
123-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
124data Listing a = Listing
127 } deriving (Eq,Ord,Show)
130openListing :: Listing a
135-- | A value that can be imported or exported
139 | Union !String !(Listing String)
140 deriving (Eq,Ord,Show)
143-- CATEGORIZING VALUES
145getValues :: [Value] -> [String]
147 Maybe.mapMaybe getValue values
150getValue :: Value -> Maybe String
153 Value name -> Just name
158getAliases :: [Value] -> [String]
160 Maybe.mapMaybe getAlias values
163getAlias :: Value -> Maybe String
167 Alias name -> Just name
171getUnions :: [Value] -> [(String, Listing String)]
173 Maybe.mapMaybe getUnion values
176getUnion :: Value -> Maybe (String, Listing String)
181 Union name ctors -> Just (name, ctors)
186instance Pretty Raw where
191instance Pretty Canonical where
193 P.text (toString var)
196instance Pretty a => Pretty (Listing a) where
197 pretty (Listing explicits open) =
198 P.parens (commaCat (map pretty explicits ++ dots))
200 dots = [if open then P.text ".." else P.empty]
203instance Pretty Value where
206 Value name -> P.text name
207 Alias name -> P.text name
209 P.text name <> pretty (ctors { _explicits = map P.text (_explicits ctors) })
212-- BINARY SERIALIZATION
214instance Binary Canonical where
215 put (Canonical home name) =
217 BuiltIn -> putWord8 0 >> put name
218 Module path -> putWord8 1 >> put path >> put name
219 Local -> putWord8 2 >> put name
221 get = do tag <- getWord8
223 0 -> Canonical BuiltIn <$> get
224 1 -> Canonical . Module <$> get <*> get
225 2 -> Canonical Local <$> get
226 _ -> error "Unexpected tag when deserializing canonical variable"
229instance Binary Value where
232 Value name -> putWord8 0 >> put name
233 Alias name -> putWord8 1 >> put name
234 Union name ctors -> putWord8 2 >> put name >> put ctors
236 get = do tag <- getWord8
240 2 -> Union <$> get <*> get
241 _ -> error "Error reading valid import/export information from serialized string"
244instance (Binary a) => Binary (Listing a) where
245 put (Listing explicits open) =
246 put explicits >> put open
248 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