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
45toEffectName :: Int -> String
46toEffectName lineNumber =
47 "$perform$" ++ show lineNumber
50fromEffectName :: String -> Maybe String
52 case List.splitAt 9 name of
53 ("$perform$", number) -> Just number
57-- VARIABLE RECOGNIZERS
59is :: [String] -> String -> Canonical -> Bool
61 var == Canonical (Module home) name
64isJson :: Canonical -> Bool
66 is ["Json", "Encode"] "Value"
69isMaybe :: Canonical -> Bool
73isArray :: Canonical -> Bool
78isTask :: Canonical -> Bool
83isList :: Canonical -> Bool
85 v == Canonical BuiltIn "List"
88isTuple :: Canonical -> Bool
91 Canonical BuiltIn name -> Help.isTuple name
95isPrimitive :: Canonical -> Bool
98 Canonical BuiltIn name -> name `elem` ["Int","Float","String","Bool"]
102isPrim :: String -> Canonical -> Bool
105 Canonical BuiltIn name -> name == prim
109isText :: Canonical -> Bool
116class ToString a where
117 toString :: a -> String
120instance ToString Raw where
121 toString (Raw name) =
125instance ToString Canonical where
126 toString (Canonical home name) =
129 Module path -> List.intercalate "." (path ++ [name])
135-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
136data Listing a = Listing
139 } deriving (Eq,Ord,Show)
142openListing :: Listing a
147closedListing :: Listing a
152listing :: [a] -> Listing a
157-- | A value that can be imported or exported
161 | Union !String !(Listing String)
162 deriving (Eq,Ord,Show)
165-- CATEGORIZING VALUES
167getValues :: [Value] -> [String]
169 Maybe.mapMaybe getValue values
172getValue :: Value -> Maybe String
175 Value name -> Just name
180getAliases :: [Value] -> [String]
182 Maybe.mapMaybe getAlias values
185getAlias :: Value -> Maybe String
189 Alias name -> Just name
193getUnions :: [Value] -> [(String, Listing String)]
195 Maybe.mapMaybe getUnion values
198getUnion :: Value -> Maybe (String, Listing String)
203 Union name ctors -> Just (name, ctors)
208instance Pretty Raw where
213instance Pretty Canonical where
215 P.text (toString var)
218instance Pretty a => Pretty (Listing a) where
219 pretty (Listing explicits open) =
220 P.parens (commaCat (map pretty explicits ++ dots))
222 dots = [if open then P.text ".." else P.empty]
225instance Pretty Value where
228 Value name -> P.text name
229 Alias name -> P.text name
231 P.text name <> pretty (ctors { _explicits = map P.text (_explicits ctors) })
234-- BINARY SERIALIZATION
236instance Binary Canonical where
237 put (Canonical home name) =
239 BuiltIn -> putWord8 0 >> put name
240 Module path -> putWord8 1 >> put path >> put name
241 Local -> putWord8 2 >> put name
243 get = do tag <- getWord8
245 0 -> Canonical BuiltIn <$> get
246 1 -> Canonical . Module <$> get <*> get
247 2 -> Canonical Local <$> get
248 _ -> error "Unexpected tag when deserializing canonical variable"
251instance Binary Value where
254 Value name -> putWord8 0 >> put name
255 Alias name -> putWord8 1 >> put name
256 Union name ctors -> putWord8 2 >> put name >> put ctors
258 get = do tag <- getWord8
262 2 -> Union <$> get <*> get
263 _ -> error "Error reading valid import/export information from serialized string"
266instance (Binary a) => Binary (Listing a) where
267 put (Listing explicits open) =
268 put explicits >> put open
270 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