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"
66isList :: Canonical -> Bool
68 v == Canonical BuiltIn "List"
71isTuple :: Canonical -> Bool
74 Canonical BuiltIn name -> Help.isTuple name
78isPrimitive :: Canonical -> Bool
81 Canonical BuiltIn name -> name `elem` ["Int","Float","String","Bool"]
85isPrim :: String -> Canonical -> Bool
88 Canonical BuiltIn name -> name == prim
92isText :: Canonical -> Bool
100 toString :: a -> String
103instance ToString Raw where
107instance ToString Canonical where
108 toString (Canonical home name) =
111 Module path -> List.intercalate "." (path ++ [name])
117-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
118data Listing a = Listing
121 } deriving (Eq,Ord,Show)
124openListing :: Listing a
129-- | A value that can be imported or exported
133 | Union !String !(Listing String)
134 deriving (Eq,Ord,Show)
137-- CATEGORIZING VALUES
139getValues :: [Value] -> [String]
141 Maybe.mapMaybe getValue values
144getValue :: Value -> Maybe String
147 Value name -> Just name
152getAliases :: [Value] -> [String]
154 Maybe.mapMaybe getAlias values
157getAlias :: Value -> Maybe String
161 Alias name -> Just name
165getUnions :: [Value] -> [(String, Listing String)]
167 Maybe.mapMaybe getUnion values
170getUnion :: Value -> Maybe (String, Listing String)
175 Union name ctors -> Just (name, ctors)
180instance Pretty Raw where
181 pretty (Raw var) = variable var
184instance Pretty Canonical where
185 pretty var = P.text (toString var)
188instance Pretty a => Pretty (Listing a) where
189 pretty (Listing explicits open) =
190 P.parens (commaCat (map pretty explicits ++ dots))
192 dots = [if open then P.text ".." else P.empty]
195instance Pretty Value where
198 Value name -> P.text name
199 Alias name -> P.text name
201 P.text name <> pretty (ctors { _explicits = map P.text (_explicits ctors) })
204-- BINARY SERIALIZATION
206instance Binary Canonical where
207 put (Canonical home name) =
209 BuiltIn -> putWord8 0 >> put name
210 Module path -> putWord8 1 >> put path >> put name
211 Local -> putWord8 2 >> put name
213 get = do tag <- getWord8
215 0 -> Canonical BuiltIn <$> get
216 1 -> Canonical . Module <$> get <*> get
217 2 -> Canonical Local <$> get
218 _ -> error "Unexpected tag when deserializing canonical variable"
221instance Binary Value where
224 Value name -> putWord8 0 >> put name
225 Alias name -> putWord8 1 >> put name
226 Union name ctors -> putWord8 2 >> put name >> put ctors
228 get = do tag <- getWord8
232 2 -> Union <$> get <*> get
233 _ -> error "Error reading valid import/export information from serialized string"
236instance (Binary a) => Binary (Listing a) where
237 put (Listing explicits open) =
238 put explicits >> put open
240 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