1{-# LANGUAGE DeriveDataTypeable #-}
2module SourceSyntax.Expression where
5import Data.List (intercalate)
6import SourceSyntax.PrettyPrint
7import Text.PrettyPrint as P
8import qualified SourceSyntax.Helpers as Help
9import qualified SourceSyntax.Location as Location
10import qualified SourceSyntax.Pattern as Pattern
11import qualified SourceSyntax.Type as Type
12import qualified SourceSyntax.Literal as Literal
14type LExpr tipe var = Location.Located (Expr tipe var)
16 = Literal Literal.Literal
18 | Range (LExpr t v) (LExpr t v)
19 | ExplicitList [LExpr t v]
20 | Binop String (LExpr t v) (LExpr t v)
21 | Lambda Pattern.Pattern (LExpr t v)
22 | App (LExpr t v) (LExpr t v)
23 | MultiIf [(LExpr t v,LExpr t v)]
24 | Let [Def t v] (LExpr t v)
25 | Case (LExpr t v) [(Pattern.Pattern, LExpr t v)]
26 | Data String [LExpr t v]
27 | Access (LExpr t v) String
28 | Remove (LExpr t v) String
29 | Insert (LExpr t v) String (LExpr t v)
30 | Modify (LExpr t v) [(String, LExpr t v)]
31 | Record [(String, LExpr t v)]
32 | Markdown String [LExpr t v]
33 deriving (Eq, Show, Data, Typeable)
36 = Def Pattern.Pattern (LExpr tipe var)
37 | TypeAnnotation String Type.Type
38 deriving (Eq, Show, Data, Typeable)
40tuple es = Data ("_Tuple" ++ show (length es)) es
42delist (Location.L _ (Data "::" [h,t])) = h : delist t
45saveEnvName = "_save_the_environment!!!"
48 Location.none $ Let defs (Location.none $ Var saveEnvName)
50instance Pretty (Expr t v) where
53 Literal lit -> pretty lit
55 Range e1 e2 -> P.brackets (pretty e1 <> P.text ".." <> pretty e2)
56 ExplicitList es -> P.brackets (commaCat (map pretty es))
57 Binop "-" (Location.L _ (Literal (Literal.IntNum 0))) e ->
58 P.text "-" <> prettyParens e
59 Binop op e1 e2 -> P.sep [ prettyParens e1 <+> P.text op', prettyParens e2 ]
60 where op' = if Help.isOp op then op else "`" ++ op ++ "`"
61 Lambda p e -> let (ps,body) = collectLambdas (Location.none $ Lambda p e)
62 in P.text "\\" <> P.sep ps <+> P.text "->" <+> pretty body
63 App _ _ -> P.hang func 2 (P.sep args)
64 where func:args = map prettyParens (collectApps (Location.none expr))
65 MultiIf branches -> P.text "if" $$ nest 3 (vcat $ map iff branches)
67 iff (b,e) = P.text "|" <+> P.hang (pretty b <+> P.text "->") 2 (pretty e)
69 P.sep [ P.hang (P.text "let") 4 (P.vcat (map pretty defs))
70 , P.text "in" <+> pretty e ]
72 P.hang pexpr 2 (P.vcat (map pretty' pats))
74 pexpr = P.sep [ P.text "case" <+> pretty e, P.text "of" ]
75 pretty' (p,e) = pretty p <+> P.text "->" <+> pretty e
76 Data "::" [hd,tl] -> pretty hd <+> P.text "::" <+> pretty tl
77 Data "[]" [] -> P.text "[]"
78 Data name es -> P.hang (P.text name) 2 (P.sep (map prettyParens es))
79 Access e x -> prettyParens e <> P.text "." <> variable x
80 Remove e x -> P.braces (pretty e <+> P.text "-" <+> variable x)
81 Insert (Location.L _ (Remove e y)) x v ->
82 P.braces (pretty e <+> P.text "-" <+> variable y <+> P.text "|" <+> variable x <+> P.text "=" <+> pretty v)
84 P.braces (pretty e <+> P.text "|" <+> variable x <+> P.text "=" <+> pretty v)
87 P.braces $ P.hang (pretty e <+> P.text "|")
89 (commaSep $ map field fs)
91 field (x,e) = variable x <+> P.text "<-" <+> pretty e
94 P.braces $ P.nest 2 (commaSep $ map field fs)
96 field (x,e) = variable x <+> P.text "=" <+> pretty e
98 Markdown _ _ -> P.text "[markdown| ... |]"
100instance Pretty (Def t v) where
103 TypeAnnotation name tipe ->
104 variable name <+> P.text ":" <+> pretty tipe
106 pretty pattern <+> P.text "=" <+> pretty expr
108collectApps lexpr@(Location.L _ expr) =
110 App a b -> collectApps a ++ [b]
113collectLambdas lexpr@(Location.L _ expr) =
115 Lambda pattern body ->
116 let (ps, body') = collectLambdas body
117 in (pretty pattern : ps, body')
120prettyParens (Location.L _ expr) = parensIf needed (pretty expr)
130 Data name (x:xs) -> name /= "::"
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