2module AST.Variable where
4import Control.Applicative ((<$>), (<*>))
6import qualified Data.List as List
7import Text.PrettyPrint as P
8import qualified AST.Helpers as Help
11newtype Raw = Raw String
12 deriving (Eq,Ord,Show)
18 deriving (Eq,Ord,Show)
20data Canonical = Canonical
23 } deriving (Eq,Ord,Show)
25local :: String -> Canonical
26local x = Canonical Local x
28builtin :: String -> Canonical
29builtin x = Canonical BuiltIn x
31-- To help with pattern matching on some common canonical variables:
32is :: [String] -> String -> Canonical -> Bool
34 var == Canonical (Module home) name
36isJson :: Canonical -> Bool
37isJson = is ["Json"] "Value"
39isMaybe :: Canonical -> Bool
40isMaybe = is ["Maybe"] "Maybe"
42isArray :: Canonical -> Bool
43isArray = is ["Array"] "Array"
45isSignal :: Canonical -> Bool
46isSignal = is ["Signal"] "Signal"
48isList :: Canonical -> Bool
50 v == Canonical BuiltIn "_List"
52isTuple :: Canonical -> Bool
55 Canonical BuiltIn name -> Help.isTuple name
58isPrimitive :: Canonical -> Bool
61 Canonical BuiltIn name -> name `elem` ["Int","Float","String","Bool"]
64isPrim :: String -> Canonical -> Bool
67 Canonical BuiltIn name -> name == prim
70isText :: Canonical -> Bool
71isText = is ["Text"] "Text"
74 toString :: a -> String
76instance ToString Raw where
79instance ToString Canonical where
80 toString (Canonical home name) =
83 Module path -> List.intercalate "." (path ++ [name])
86-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
87data Listing a = Listing
90 } deriving (Eq,Ord,Show)
92openListing :: Listing a
93openListing = Listing [] True
95-- | A value that can be imported or exported
99 | ADT !String !(Listing String)
100 deriving (Eq,Ord,Show)
102instance Pretty Raw where
103 pretty (Raw var) = variable var
105instance Pretty Canonical where
106 pretty var = P.text (toString var)
108instance Pretty a => Pretty (Listing a) where
109 pretty (Listing explicits open) =
110 P.parens (commaCat (map pretty explicits ++ dots))
112 dots = [if open then P.text ".." else P.empty]
114instance Pretty Value where
117 Value name -> P.text name
118 Alias name -> P.text name
120 P.text name <> pretty (ctors { _explicits = map P.text (_explicits ctors) })
122instance Binary Canonical where
123 put (Canonical home name) =
125 BuiltIn -> putWord8 0 >> put name
126 Module path -> putWord8 1 >> put path >> put name
127 Local -> putWord8 2 >> put name
129 get = do tag <- getWord8
131 0 -> Canonical BuiltIn <$> get
132 1 -> Canonical . Module <$> get <*> get
133 2 -> Canonical Local <$> get
134 _ -> error "Unexpected tag when deserializing canonical variable"
136instance Binary Value where
139 Value name -> putWord8 0 >> put name
140 Alias name -> putWord8 1 >> put name
141 ADT name ctors -> putWord8 2 >> put name >> put ctors
143 get = do tag <- getWord8
147 2 -> ADT <$> get <*> get
148 _ -> error "Error reading valid import/export information from serialized string"
150instance (Binary a) => Binary (Listing a) where
151 put (Listing explicits open) =
152 put explicits >> put open
154 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