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
64isJavaScript :: Canonical -> Bool
66 is ["JavaScript", "Encode"] "Value"
69isMaybe :: Canonical -> Bool
73isArray :: Canonical -> Bool
78isTask :: Canonical -> Bool
83isStream :: Canonical -> Bool
85 is ["SignalTypes"] "Stream"
88isList :: Canonical -> Bool
90 v == Canonical BuiltIn "List"
93isTuple :: Canonical -> Bool
96 Canonical BuiltIn name -> Help.isTuple name
100isPrimitive :: Canonical -> Bool
103 Canonical BuiltIn name -> name `elem` ["Int","Float","String","Bool"]
107isPrim :: String -> Canonical -> Bool
110 Canonical BuiltIn name -> name == prim
114isText :: Canonical -> Bool
121class ToString a where
122 toString :: a -> String
125instance ToString Raw where
126 toString (Raw name) =
130instance ToString Canonical where
131 toString (Canonical home name) =
134 Module path -> List.intercalate "." (path ++ [name])
140-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
141data Listing a = Listing
144 } deriving (Eq,Ord,Show)
147openListing :: Listing a
152closedListing :: Listing a
157listing :: [a] -> Listing a
162-- | A value that can be imported or exported
166 | Union !String !(Listing String)
167 deriving (Eq,Ord,Show)
170-- CATEGORIZING VALUES
172getValues :: [Value] -> [String]
174 Maybe.mapMaybe getValue values
177getValue :: Value -> Maybe String
180 Value name -> Just name
185getAliases :: [Value] -> [String]
187 Maybe.mapMaybe getAlias values
190getAlias :: Value -> Maybe String
194 Alias name -> Just name
198getUnions :: [Value] -> [(String, Listing String)]
200 Maybe.mapMaybe getUnion values
203getUnion :: Value -> Maybe (String, Listing String)
208 Union name ctors -> Just (name, ctors)
213instance Pretty Raw where
218instance Pretty Canonical where
220 P.text (toString var)
223instance Pretty a => Pretty (Listing a) where
224 pretty (Listing explicits open) =
225 P.parens (commaCat (map pretty explicits ++ dots))
227 dots = [if open then P.text ".." else P.empty]
230instance Pretty Value where
233 Value name -> P.text name
234 Alias name -> P.text name
236 P.text name <> pretty (ctors { _explicits = map P.text (_explicits ctors) })
239-- BINARY SERIALIZATION
241instance Binary Canonical where
242 put (Canonical home name) =
244 BuiltIn -> putWord8 0 >> put name
245 Module path -> putWord8 1 >> put path >> put name
246 Local -> putWord8 2 >> put name
248 get = do tag <- getWord8
250 0 -> Canonical BuiltIn <$> get
251 1 -> Canonical . Module <$> get <*> get
252 2 -> Canonical Local <$> get
253 _ -> error "Unexpected tag when deserializing canonical variable"
256instance Binary Value where
259 Value name -> putWord8 0 >> put name
260 Alias name -> putWord8 1 >> put name
261 Union name ctors -> putWord8 2 >> put name >> put ctors
263 get = do tag <- getWord8
267 2 -> Union <$> get <*> get
268 _ -> error "Error reading valid import/export information from serialized string"
271instance (Binary a) => Binary (Listing a) where
272 put (Listing explicits open) =
273 put explicits >> put open
275 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