@@ -1,13 +1,14 @@
1+{-# OPTIONS_GHC -Wall #-}
1module SourceSyntax.Expression where
2module SourceSyntax.Expression where
3−import Data.List (intercalate)
4import SourceSyntax.PrettyPrint
4import SourceSyntax.PrettyPrint
5import Text.PrettyPrint as P
5import Text.PrettyPrint as P
6import qualified SourceSyntax.Helpers as Help
6import qualified SourceSyntax.Helpers as Help
7import qualified SourceSyntax.Location as Location
7import qualified SourceSyntax.Location as Location
8import qualified SourceSyntax.Pattern as Pattern
8import qualified SourceSyntax.Pattern as Pattern
9−import qualified SourceSyntax.Type as Type
9+import qualified SourceSyntax.Type as SrcType
10import qualified SourceSyntax.Literal as Literal
10import qualified SourceSyntax.Literal as Literal
11+import qualified Type.Type as Type
12{-| This is a located expression. -}
13{-| This is a located expression. -}
13type LExpr' def = Location.Located (Expr' def)
14type LExpr' def = Location.Located (Expr' def)
@@ -45,7 +46,9 @@ data Expr' def
45 | Modify (LExpr' def) [(String, LExpr' def)]
46 | Modify (LExpr' def) [(String, LExpr' def)]
46 | Record [(String, LExpr' def)]
47 | Record [(String, LExpr' def)]
47 | Markdown String String [LExpr' def]
48 | Markdown String String [LExpr' def]
48− deriving (Eq)
49+ -- for type checking and code gen only
50+ | PortIn String SrcType.Type Type.Type (LExpr' def)
51+ | PortOut String SrcType.Type (LExpr' def)
50type ParseExpr = Expr' ParseDef
53type ParseExpr = Expr' ParseDef
51type LParseExpr = LExpr' ParseDef
54type LParseExpr = LExpr' ParseDef
@@ -57,23 +60,26 @@ separately, and other checks will make sure they can be combined.
58data ParseDef
61data ParseDef
59 = Def Pattern.Pattern LParseExpr
62 = Def Pattern.Pattern LParseExpr
60− | TypeAnnotation String Type.Type
63+ | TypeAnnotation String SrcType.Type
61− deriving (Eq, Show)
64+ deriving (Show)
63{-| After checking that type annotations and definitions are all
66{-| After checking that type annotations and definitions are all
64valid, they can be combined.
67valid, they can be combined.
67− = Definition Pattern.Pattern (LExpr' Def) (Maybe Type.Type)
69+data Def = Definition Pattern.Pattern (LExpr' Def) (Maybe SrcType.Type)
68− deriving (Eq, Show)
70+ deriving (Show)
72+tuple :: [LExpr' def] -> Expr' def
70tuple es = Data ("_Tuple" ++ show (length es)) es
73tuple es = Data ("_Tuple" ++ show (length es)) es
75+delist :: LExpr' def -> [LExpr' def]
72delist (Location.L _ (Data "::" [h,t])) = h : delist t
76delist (Location.L _ (Data "::" [h,t])) = h : delist t
73delist _ = []
77delist _ = []
75saveEnvName = "_save_the_environment!!!"
80saveEnvName = "_save_the_environment!!!"
82+dummyLet :: Pretty def => [def] -> LExpr' def
77dummyLet defs =
83dummyLet defs =
78 Location.none $ Let defs (Location.none $ Var saveEnvName)
84 Location.none $ Let defs (Location.none $ Var saveEnvName)
@@ -105,7 +111,7 @@ instance Pretty def => Pretty (Expr' def) where
105 P.hang pexpr 2 (P.vcat (map pretty' pats))
111 P.hang pexpr 2 (P.vcat (map pretty' pats))
107 pexpr = P.sep [ P.text "case" <+> pretty e, P.text "of" ]
113 pexpr = P.sep [ P.text "case" <+> pretty e, P.text "of" ]
108− pretty' (p,e) = pretty p <+> P.text "->" <+> pretty e
114+ pretty' (p,b) = pretty p <+> P.text "->" <+> pretty b
109 Data "::" [hd,tl] -> pretty hd <+> P.text "::" <+> pretty tl
115 Data "::" [hd,tl] -> pretty hd <+> P.text "::" <+> pretty tl
110 Data "[]" [] -> P.text "[]"
116 Data "[]" [] -> P.text "[]"
111 Data name es -> P.hang (P.text name) 2 (P.sep (map prettyParens es))
117 Data name es -> P.hang (P.text name) 2 (P.sep (map prettyParens es))
@@ -121,7 +127,7 @@ instance Pretty def => Pretty (Expr' def) where
122 (commaSep $ map field fs)
128 (commaSep $ map field fs)
124− field (x,e) = variable x <+> P.text "<-" <+> pretty e
130+ field (k,v) = variable k <+> P.text "<-" <+> pretty v
126 Record fs ->
132 Record fs ->
127 P.braces $ P.nest 2 (commaSep $ map field fs)
133 P.braces $ P.nest 2 (commaSep $ map field fs)
@@ -130,6 +136,10 @@ instance Pretty def => Pretty (Expr' def) where
131 Markdown _ _ _ -> P.text "[markdown| ... |]"
137 Markdown _ _ _ -> P.text "[markdown| ... |]"
139+ PortIn _ _ _ handler -> pretty handler
141+ PortOut _ _ signal -> pretty signal
133instance Pretty ParseDef where
143instance Pretty ParseDef where
134 pretty def =
144 pretty def =
135 case def of
145 case def of
@@ -147,11 +157,13 @@ instance Pretty Def where
147 Nothing -> P.empty
157 Nothing -> P.empty
148 Just tipe -> pretty pattern <+> P.colon <+> pretty tipe
158 Just tipe -> pretty pattern <+> P.colon <+> pretty tipe
160+collectApps :: LExpr' def -> [LExpr' def]
150collectApps lexpr@(Location.L _ expr) =
161collectApps lexpr@(Location.L _ expr) =
151 case expr of
162 case expr of
152 App a b -> collectApps a ++ [b]
163 App a b -> collectApps a ++ [b]
153 _ -> [lexpr]
164 _ -> [lexpr]
166+collectLambdas :: LExpr' def -> ([Doc], LExpr' def)
155collectLambdas lexpr@(Location.L _ expr) =
167collectLambdas lexpr@(Location.L _ expr) =
156 case expr of
168 case expr of
157 Lambda pattern body ->
169 Lambda pattern body ->
@@ -159,6 +171,7 @@ collectLambdas lexpr@(Location.L _ expr) =
159 in (pretty pattern : ps, body')
171 in (pretty pattern : ps, body')
160 _ -> ([], lexpr)
172 _ -> ([], lexpr)
174+prettyParens :: (Pretty def) => LExpr' def -> Doc
162prettyParens (Location.L _ expr) = parensIf needed (pretty expr)
175prettyParens (Location.L _ expr) = parensIf needed (pretty expr)
@@ -169,5 +182,5 @@ prettyParens (Location.L _ expr) = parensIf needed (pretty expr)
169 MultiIf _ -> True
182 MultiIf _ -> True
170 Let _ _ -> True
183 Let _ _ -> True
171 Case _ _ -> True
184 Case _ _ -> True
172− Data name (x:xs) -> name /= "::"
185+ Data name (_:_) -> name /= "::"
173 _ -> False
186 _ -> False