on 61b2cd6 help sign in
elk/compiler/SourceSyntax/Expression.hs
131 lines 4.7 kB history raw
1{-# LANGUAGE DeriveDataTypeable #-}
2module SourceSyntax.Expression where
4import Data.Data
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)
15data Expr t v
16 = Literal Literal.Literal
17 | Var String
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)
35data Def tipe var
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
43delist _ = []
45saveEnvName = "_save_the_environment!!!"
47dummyLet defs =
48 Location.none $ Let defs (Location.none $ Var saveEnvName)
50instance Pretty (Expr t v) where
51 pretty expr =
52 case expr of
53 Literal lit -> pretty lit
54 Var x -> variable x
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)
66 where
67 iff (b,e) = P.text "|" <+> P.hang (pretty b <+> P.text "->") 2 (pretty e)
68 Let defs e ->
69 P.sep [ P.hang (P.text "let") 4 (P.vcat (map pretty defs))
70 , P.text "in" <+> pretty e ]
71 Case e pats ->
72 P.hang pexpr 2 (P.vcat (map pretty' pats))
73 where
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)
83 Insert e x v ->
84 P.braces (pretty e <+> P.text "|" <+> variable x <+> P.text "=" <+> pretty v)
86 Modify e fs ->
87 P.braces $ P.hang (pretty e <+> P.text "|")
88 4
89 (commaSep $ map field fs)
90 where
91 field (x,e) = variable x <+> P.text "<-" <+> pretty e
93 Record fs ->
94 P.braces $ P.nest 2 (commaSep $ map field fs)
95 where
96 field (x,e) = variable x <+> P.text "=" <+> pretty e
98 Markdown _ _ -> P.text "[markdown| ... |]"
100instance Pretty (Def t v) where
101 pretty def =
102 case def of
103 TypeAnnotation name tipe ->
104 variable name <+> P.text ":" <+> pretty tipe
105 Def pattern expr ->
106 pretty pattern <+> P.text "=" <+> pretty expr
108collectApps lexpr@(Location.L _ expr) =
109 case expr of
110 App a b -> collectApps a ++ [b]
111 _ -> [lexpr]
113collectLambdas lexpr@(Location.L _ expr) =
114 case expr of
115 Lambda pattern body ->
116 let (ps, body') = collectLambdas body
117 in (pretty pattern : ps, body')
118 _ -> ([], lexpr)
120prettyParens (Location.L _ expr) = parensIf needed (pretty expr)
121 where
122 needed =
123 case expr of
124 Binop _ _ _ -> True
125 Lambda _ _ -> True
126 App _ _ -> True
127 MultiIf _ -> True
128 Let _ _ -> True
129 Case _ _ -> True
130 Data name (x:xs) -> name /= "::"
131 _ -> False