@@ -1,13 +1,14 @@
1+{-# OPTIONS_GHC -Wall #-}
12module SourceSyntax.Expression where
23
3−import Data.List (intercalate)
44import SourceSyntax.PrettyPrint
55import Text.PrettyPrint as P
66import qualified SourceSyntax.Helpers as Help
77import qualified SourceSyntax.Location as Location
88import qualified SourceSyntax.Pattern as Pattern
9−import qualified SourceSyntax.Type as Type
9+import qualified SourceSyntax.Type as SrcType
1010import qualified SourceSyntax.Literal as Literal
11+import qualified Type.Type as Type
1112
1213{-| This is a located expression. -}
1314type LExpr' def = Location.Located (Expr' def)
@@ -45,7 +46,9 @@ data Expr' def
4546 | Modify (LExpr' def) [(String, LExpr' def)]
4647 | Record [(String, LExpr' def)]
4748 | 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)
4952
5053type ParseExpr = Expr' ParseDef
5154type LParseExpr = LExpr' ParseDef
@@ -57,23 +60,26 @@ separately, and other checks will make sure they can be combined.
5760-}
5861data ParseDef
5962 = Def Pattern.Pattern LParseExpr
60− | TypeAnnotation String Type.Type
61− deriving (Eq, Show)
63+ | TypeAnnotation String SrcType.Type
64+ deriving (Show)
6265
6366{-| After checking that type annotations and definitions are all
6467valid, they can be combined.
6568-}
66−data Def
67− = Definition Pattern.Pattern (LExpr' Def) (Maybe Type.Type)
68− deriving (Eq, Show)
69+data Def = Definition Pattern.Pattern (LExpr' Def) (Maybe SrcType.Type)
70+ deriving (Show)
6971
72+tuple :: [LExpr' def] -> Expr' def
7073tuple es = Data ("_Tuple" ++ show (length es)) es
7174
75+delist :: LExpr' def -> [LExpr' def]
7276delist (Location.L _ (Data "::" [h,t])) = h : delist t
7377delist _ = []
7478
79+saveEnvName :: String
7580saveEnvName = "_save_the_environment!!!"
7681
82+dummyLet :: Pretty def => [def] -> LExpr' def
7783dummyLet defs =
7884 Location.none $ Let defs (Location.none $ Var saveEnvName)
7985
@@ -105,7 +111,7 @@ instance Pretty def => Pretty (Expr' def) where
105111 P.hang pexpr 2 (P.vcat (map pretty' pats))
106112 where
107113 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
109115 Data "::" [hd,tl] -> pretty hd <+> P.text "::" <+> pretty tl
110116 Data "[]" [] -> P.text "[]"
111117 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
121127 4
122128 (commaSep $ map field fs)
123129 where
124− field (x,e) = variable x <+> P.text "<-" <+> pretty e
130+ field (k,v) = variable k <+> P.text "<-" <+> pretty v
125131
126132 Record fs ->
127133 P.braces $ P.nest 2 (commaSep $ map field fs)
@@ -130,6 +136,10 @@ instance Pretty def => Pretty (Expr' def) where
130136
131137 Markdown _ _ _ -> P.text "[markdown| ... |]"
132138
139+ PortIn _ _ _ handler -> pretty handler
140+
141+ PortOut _ _ signal -> pretty signal
142+
133143instance Pretty ParseDef where
134144 pretty def =
135145 case def of
@@ -147,11 +157,13 @@ instance Pretty Def where
147157 Nothing -> P.empty
148158 Just tipe -> pretty pattern <+> P.colon <+> pretty tipe
149159
160+collectApps :: LExpr' def -> [LExpr' def]
150161collectApps lexpr@(Location.L _ expr) =
151162 case expr of
152163 App a b -> collectApps a ++ [b]
153164 _ -> [lexpr]
154165
166+collectLambdas :: LExpr' def -> ([Doc], LExpr' def)
155167collectLambdas lexpr@(Location.L _ expr) =
156168 case expr of
157169 Lambda pattern body ->
@@ -159,6 +171,7 @@ collectLambdas lexpr@(Location.L _ expr) =
159171 in (pretty pattern : ps, body')
160172 _ -> ([], lexpr)
161173
174+prettyParens :: (Pretty def) => LExpr' def -> Doc
162175prettyParens (Location.L _ expr) = parensIf needed (pretty expr)
163176 where
164177 needed =
@@ -169,5 +182,5 @@ prettyParens (Location.L _ expr) = parensIf needed (pretty expr)
169182 MultiIf _ -> True
170183 Let _ _ -> True
171184 Case _ _ -> True
172− Data name (x:xs) -> name /= "::"
185+ Data name (_:_) -> name /= "::"
173186 _ -> False