2module AST.Variable where
5import Control.Applicative ((<$>), (<*>))
6import Text.PrettyPrint as P
7import qualified AST.Helpers as Help
10newtype Raw = Raw String
11 deriving (Eq,Ord,Show)
17 deriving (Eq,Ord,Show)
19data Canonical = Canonical
22 } deriving (Eq,Ord,Show)
24local :: String -> Canonical
25local x = Canonical Local x
27builtin :: String -> Canonical
28builtin x = Canonical BuiltIn x
30-- To help with pattern matching on some common canonical variables:
31is :: String -> String -> Canonical -> Bool
33 var == Canonical (Module home) name
35isJson :: Canonical -> Bool
36isJson = is "Json" "Value"
38isMaybe :: Canonical -> Bool
39isMaybe = is "Maybe" "Maybe"
41isArray :: Canonical -> Bool
42isArray = is "Array" "Array"
44isSignal :: Canonical -> Bool
45isSignal = is "Signal" "Signal"
47isList :: Canonical -> Bool
48isList v = v == Canonical BuiltIn "_List"
50isTuple :: Canonical -> Bool
53 Canonical BuiltIn name -> Help.isTuple name
56isPrimitive :: Canonical -> Bool
59 Canonical BuiltIn name -> name `elem` ["Int","Float","String","Bool"]
62isPrim :: String -> Canonical -> Bool
65 Canonical BuiltIn name -> name == prim
68isText :: Canonical -> Bool
69isText = is "Text" "Text"
72 toString :: a -> String
74instance ToString Raw where
77instance ToString Canonical where
78 toString (Canonical home name) =
81 Module path -> path ++ "." ++ name
84-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
85data Listing a = Listing
88 } deriving (Eq,Ord,Show)
90openListing :: Listing a
91openListing = Listing [] True
93-- | A value that can be imported or exported
97 | ADT !String !(Listing String)
98 deriving (Eq,Ord,Show)
100instance Pretty Raw where
101 pretty (Raw var) = variable var
103instance Pretty Canonical where
104 pretty var = P.text (toString var)
106instance Pretty a => Pretty (Listing a) where
107 pretty (Listing explicits open) =
108 P.parens (commaCat (map pretty explicits ++ dots))
110 dots = [if open then P.text ".." else P.empty]
112instance Pretty Value where
115 Value name -> P.text name
116 Alias name -> P.text name
118 P.text name <> pretty (ctors { _explicits = map P.text (_explicits ctors) })
120instance Binary Canonical where
121 put (Canonical home name) =
123 BuiltIn -> putWord8 0 >> put name
124 Module path -> putWord8 1 >> put path >> put name
125 Local -> putWord8 2 >> put name
127 get = do tag <- getWord8
129 0 -> Canonical BuiltIn <$> get
130 1 -> Canonical . Module <$> get <*> get
131 2 -> Canonical Local <$> get
132 _ -> error "Unexpected tag when deserializing canonical variable"
134instance Binary Value where
137 Value name -> putWord8 0 >> put name
138 Alias name -> putWord8 1 >> put name
139 ADT name ctors -> putWord8 2 >> put name >> put ctors
141 get = do tag <- getWord8
145 2 -> ADT <$> get <*> get
146 _ -> error "Error reading valid import/export information from serialized string"
148instance (Binary a) => Binary (Listing a) where
149 put (Listing explicits open) =
150 put explicits >> put open
152 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