5import Data.Char (isDigit, isSymbol)
6import Data.List (intercalate)
8import qualified Text.Pandoc as Pandoc
10data Module = Module [String] Exports Imports [Statement]
12type Exports = [String]
14type Imports = [(String, ImportMethod)]
15data ImportMethod = As String | Importing [String] | Hiding [String]
16 deriving (Eq, Ord, Show)
19data Pattern = PData String [Pattern]
25type CExpr = Context Expr
34 | Insert CExpr String CExpr
35 | Modify CExpr [(String,CExpr)]
36 | Record [(String,[String],CExpr)]
37 | Binop String CExpr CExpr
40 | If CExpr CExpr CExpr
41 | MultiIf [(CExpr,CExpr)]
44 | Case CExpr [(Pattern,CExpr)]
46 | Markdown Pandoc.Pandoc
49data Def = FnDef String [String] CExpr
50 | OpDef String String String CExpr
53data Statement = Definition Def
54 | Datatype String [X] [(String,[Type])]
55 | TypeAlias String [X] Type
56 | TypeAnnotation String Type
57 | ImportEvent String CExpr String Type
58 | ExportEvent String String Type
61cons h t = epos h t (Data "Cons" [h,t])
62nil = C (Just "[]") NoSpan (Data "Nil" [])
64tuple es = Data ("Tuple" ++ show (length es)) es
66delist (C _ _ (Data "Cons" [h,t])) = h : delist t
70pcons h t = PData "Cons" [h,t]
72plist = foldr pcons pnil
73ptuple es = PData ("Tuple" ++ show (length es)) es
75brkt s = "{ " ++ s ++ " }"
76parensIf b s = if b then parens s else s
77isOp c = isSymbol c || elem c "+-/*=.$<>:&|^?%#@~!"
79instance Show Pattern where
82 PRecord fs -> brkt (intercalate ", " fs)
85 PData "Cons" [hd@(PData "Cons" _),tl] ->
86 parens (show hd) ++ " :: " ++ show tl
87 PData "Cons" [hd,tl] -> show hd ++ " : " ++ show tl
88 PData "Nil" [] -> "[]"
90 if take 5 name == "Tuple" && all isDigit (drop 5 name) then
91 parens . intercalate ", " $ map show ps
92 else parensIf (not (null ps)) $ unwords (name : map show ps)
94instance Show Expr where
96 let show' (C _ _ e) = parensIf (needsParens e) (show e) in
103 Range e1 e2 -> "[" ++ show e1 ++ ".." ++ show e2 ++ "]"
104 Access e x -> show' e ++ "." ++ x
105 Remove e x -> brkt (show e ++ " - " ++ x)
106 Insert (C _ _ (Remove e y)) x v ->
107 brkt (show e ++ " - " ++ y ++ " | " ++ x ++ " = " ++ show v)
108 Insert e x v -> brkt (show e ++ " | " ++ x ++ " = " ++ show v)
109 Modify e fs -> brkt (show e ++" | "++ intercalate ", " (map field fs))
110 where field (x,e) = x ++ " <- " ++ show e
111 Record r -> brkt (intercalate ", " (map fields r))
112 where fields (f,args,e) = f ++ concatMap (' ':) args ++ " = " ++ show e
113 Binop op e1 e2 -> show' e1 ++ " " ++ op ++ " " ++ show' e2
114 Lambda x e -> let (xs,e') = getLambdas (noContext $ Lambda x e) in
115 concat [ "\\", intercalate " " xs, " -> ", show e' ]
116 App e1 e2 -> show' e1 ++ " " ++ show' e2
117 If e1 e2 e3 -> concat [ "if ", show e1, " then ", show e2, " else ", show e3 ]
118 MultiIf (p:ps) -> concat [ "if | ", iff p, sep (map iff ps) ]
119 where iff (b,e) = show b ++ " -> " ++ show e
120 sep = concatMap ("\n | " ++)
121 Let defs e -> "let { "++intercalate " ; " (map show defs)++" } in "++show e
122 Var (c:cs) -> if isOp c then parens (c:cs) else c:cs
123 Case e pats -> "case "++ show e ++" of " ++ brkt (intercalate " ; " pats')
124 where pats' = map (\(p,e) -> show p ++ " -> " ++ show e) pats
126 | name == "Cons" -> ("["++) . (++"]") . intercalate "," . map show $
127 delist (noContext $ Data "Cons" es)
128 | name == "Nil" -> "[]"
129 | otherwise -> name ++ " " ++ intercalate " " (map show' es)
130 Markdown _ -> "[markdown| ... |]"
133instance Show Def where
136 FnDef v [] e -> v ++ " = " ++ show e
137 FnDef f args e -> f ++ " " ++ intercalate " " args ++ " = " ++ show e
138 OpDef op a1 a2 e -> intercalate " " [a1,op,a2] ++ " = " ++ show e
140getLambdas (C _ _ (Lambda x e)) = (x:xs,e')
141 where (xs,e') = getLambdas e
152 Data name (x:xs) -> name /= "Cons"
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