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
104 toString (Raw name) =
108instance ToString Canonical where
109 toString (Canonical home name) =
112 Module path -> List.intercalate "." (path ++ [name])
118-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
119data Listing a = Listing
122 } deriving (Eq,Ord,Show)
125openListing :: Listing a
130-- | A value that can be imported or exported
134 | Union !String !(Listing String)
135 deriving (Eq,Ord,Show)
138-- CATEGORIZING VALUES
140getValues :: [Value] -> [String]
142 Maybe.mapMaybe getValue values
145getValue :: Value -> Maybe String
148 Value name -> Just name
153getAliases :: [Value] -> [String]
155 Maybe.mapMaybe getAlias values
158getAlias :: Value -> Maybe String
162 Alias name -> Just name
166getUnions :: [Value] -> [(String, Listing String)]
168 Maybe.mapMaybe getUnion values
171getUnion :: Value -> Maybe (String, Listing String)
176 Union name ctors -> Just (name, ctors)
181instance Pretty Raw where
186instance Pretty Canonical where
188 P.text (toString var)
191instance Pretty a => Pretty (Listing a) where
192 pretty (Listing explicits open) =
193 P.parens (commaCat (map pretty explicits ++ dots))
195 dots = [if open then P.text ".." else P.empty]
198instance Pretty Value where
201 Value name -> P.text name
202 Alias name -> P.text name
204 P.text name <> pretty (ctors { _explicits = map P.text (_explicits ctors) })
207-- BINARY SERIALIZATION
209instance Binary Canonical where
210 put (Canonical home name) =
212 BuiltIn -> putWord8 0 >> put name
213 Module path -> putWord8 1 >> put path >> put name
214 Local -> putWord8 2 >> put name
216 get = do tag <- getWord8
218 0 -> Canonical BuiltIn <$> get
219 1 -> Canonical . Module <$> get <*> get
220 2 -> Canonical Local <$> get
221 _ -> error "Unexpected tag when deserializing canonical variable"
224instance Binary Value where
227 Value name -> putWord8 0 >> put name
228 Alias name -> putWord8 1 >> put name
229 Union name ctors -> putWord8 2 >> put name >> put ctors
231 get = do tag <- getWord8
235 2 -> Union <$> get <*> get
236 _ -> error "Error reading valid import/export information from serialized string"
239instance (Binary a) => Binary (Listing a) where
240 put (Listing explicits open) =
241 put explicits >> put open
243 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