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