on cbaff88 help sign in
elk/compiler/SourceSyntax/Expression.hs
130 lines 4.6 kB history raw
1{-# LANGUAGE DeriveDataTypeable #-}
2module SourceSyntax.Expression where
4import Data.Data
5import Data.List (intercalate)
6import qualified Text.Pandoc as Pandoc
7import SourceSyntax.PrettyPrint
8import Text.PrettyPrint as P
9import qualified SourceSyntax.Helpers as Help
10import qualified SourceSyntax.Location as Location
11import qualified SourceSyntax.Pattern as Pattern
12import qualified SourceSyntax.Type as Type
13import qualified SourceSyntax.Literal as Literal
15type LExpr tipe var = Location.Located (Expr tipe var)
16data Expr t v
17 = Literal Literal.Literal
18 | Var String
19 | Range (LExpr t v) (LExpr t v)
20 | ExplicitList [LExpr t v]
21 | Binop String (LExpr t v) (LExpr t v)
22 | Lambda Pattern.Pattern (LExpr t v)
23 | App (LExpr t v) (LExpr t v)
24 | MultiIf [(LExpr t v,LExpr t v)]
25 | Let [Def t v] (LExpr t v)
26 | Case (LExpr t v) [(Pattern.Pattern, LExpr t v)]
27 | Data String [LExpr t v]
28 | Access (LExpr t v) String
29 | Remove (LExpr t v) String
30 | Insert (LExpr t v) String (LExpr t v)
31 | Modify (LExpr t v) [(String, LExpr t v)]
32 | Record [(String, LExpr t v)]
33 | Markdown Pandoc.Pandoc
34 deriving (Eq, Show, Data, Typeable)
36data Def tipe var
37 = Def Pattern.Pattern (LExpr tipe var)
38 | TypeAnnotation String Type.Type
39 deriving (Eq, Show, Data, Typeable)
41tuple es = Data ("_Tuple" ++ show (length es)) es
43delist (Location.L _ (Data "::" [h,t])) = h : delist t
44delist _ = []
46saveEnvName = "_save_the_environment!!!"
48dummyLet defs =
49 Location.none $ Let defs (Location.none $ Var saveEnvName)
51instance Pretty (Expr t v) where
52 pretty expr =
53 case expr of
54 Literal lit -> pretty lit
55 Var x -> variable x
56 Range e1 e2 -> P.brackets (pretty e1 <> P.text ".." <> pretty e2)
57 ExplicitList es -> P.brackets (commaCat (map pretty es))
58 Binop op e1 e2 -> P.sep [ prettyParens e1 <+> P.text op', prettyParens e2 ]
59 where op' = if Help.isOp op then op else "`" ++ op ++ "`"
60 Lambda p e -> let (ps,body) = collectLambdas (Location.none $ Lambda p e)
61 in P.text "\\" <> P.sep ps <+> P.text "->" <+> pretty body
62 App _ _ -> P.hang func 2 (P.sep args)
63 where func:args = map prettyParens (collectApps (Location.none expr))
64 MultiIf branches -> P.text "if" $$ nest 3 (vcat $ map iff branches)
65 where
66 iff (b,e) = P.text "|" <+> P.hang (pretty b <+> P.text "->") 2 (pretty e)
67 Let defs e ->
68 P.sep [ P.hang (P.text "let") 4 (P.vcat (map pretty defs))
69 , P.text "in" <+> pretty e ]
70 Case e pats ->
71 P.hang pexpr 2 (P.vcat (map pretty' pats))
72 where
73 pexpr = P.sep [ P.text "case" <+> pretty e, P.text "of" ]
74 pretty' (p,e) = pretty p <+> P.text "->" <+> pretty e
75 Data "::" [hd,tl] -> pretty hd <+> P.text "::" <+> pretty tl
76 Data "[]" [] -> P.text "[]"
77 Data name es -> P.hang (P.text name) 2 (P.sep (map prettyParens es))
78 Access e x -> prettyParens e <> P.text "." <> variable x
79 Remove e x -> P.braces (pretty e <+> P.text "-" <+> variable x)
80 Insert (Location.L _ (Remove e y)) x v ->
81 P.braces (pretty e <+> P.text "-" <+> variable y <+> P.text "|" <+> variable x <+> P.text "=" <+> pretty v)
82 Insert e x v ->
83 P.braces (pretty e <+> P.text "|" <+> variable x <+> P.text "=" <+> pretty v)
85 Modify e fs ->
86 P.braces $ P.hang (pretty e <+> P.text "|")
87 4
88 (commaSep $ map field fs)
89 where
90 field (x,e) = variable x <+> P.text "<-" <+> pretty e
92 Record fs ->
93 P.braces $ P.nest 2 (commaSep $ map field fs)
94 where
95 field (x,e) = variable x <+> P.text "=" <+> pretty e
97 Markdown _ -> P.text "[markdown| ... |]"
99instance Pretty (Def t v) where
100 pretty def =
101 case def of
102 TypeAnnotation name tipe ->
103 variable name <+> P.text ":" <+> pretty tipe
104 Def pattern expr ->
105 pretty pattern <+> P.text "=" <+> pretty expr
107collectApps lexpr@(Location.L _ expr) =
108 case expr of
109 App a b -> collectApps a ++ [b]
110 _ -> [lexpr]
112collectLambdas lexpr@(Location.L _ expr) =
113 case expr of
114 Lambda pattern body ->
115 let (ps, body') = collectLambdas body
116 in (pretty pattern : ps, body')
117 _ -> ([], lexpr)
119prettyParens (Location.L _ expr) = parensIf needed (pretty expr)
120 where
121 needed =
122 case expr of
123 Binop _ _ _ -> True
124 Lambda _ _ -> True
125 App _ _ -> True
126 MultiIf _ -> True
127 Let _ _ -> True
128 Case _ _ -> True
129 Data name (x:xs) -> name /= "::"
130 _ -> False