on master help sign in
Properly propagate port types
commit75c1c8d346063a3f2192e4a4cd13e03f80f5c3d2 authorEvan Czaplicki <info@elm-lang.org> dateFri 20 Mar 2015, 19:41 (12 years ago) parentc4b4026
6 files changed+32−38
src/AST/Expression/General.hs+2−2 src/AST/Type.hs+13−24 src/Generate/JavaScript.hs+1−1 src/Transform/Canonicalize.hs+12−7 src/Transform/Canonicalize/Port.hs+3−3 src/Transform/Declaration.hs+1−1
src/AST/Expression/General.hs+2−2
@@ -10,7 +10,7 @@ module AST.Expression.General where
1010
1111import AST.PrettyPrint
1212import Text.PrettyPrint as P
13−import AST.Type (PortType)
13+import AST.Type (PortDirection)
1414
1515import qualified AST.Annotation as Annotation
1616import qualified AST.Helpers as Help
@@ -58,7 +58,7 @@ data Expr' ann def var
5858 | Modify (Expr ann def var) [(String, Expr ann def var)]
5959 | Record [(String, Expr ann def var)]
6060 -- for type checking and code gen only
61− | Port String (PortType var)
61+ | Port String (PortDirection var)
6262 | Perform (Expr ann def var)
6363 | GLShader String String Literal.GLShaderTipe
6464 deriving (Show)
src/AST/Type.hs+13−24
@@ -1,6 +1,7 @@
11module AST.Type
2− ( Type(..), RawType, CanonicalType, PortType(..)
3− , portTypeToType, fieldMap, recordOf, listOf, tupleOf
2+ ( Type(..), RawType, CanonicalType
3+ , PortType(..), PortDirection(..)
4+ , fieldMap, recordOf, listOf, tupleOf
45 , deepDealias, dealias
56 , collectLambdas
67 , prettyParens
@@ -37,22 +38,18 @@ type CanonicalType =
3738 Type Var.Canonical
3839
3940
40−data PortType var
41− = Inbound (Type var)
42− | Internal (Type var)
43− | Outbound (Type var)
41+data PortType var = PortType
42+ { direction :: PortDirection var
43+ , tipe :: Type var
44+ }
4445 deriving (Show)
4546
4647
47−portTypeToType :: PortType Var.Canonical -> CanonicalType
48−portTypeToType portType =
49− let makeType name tipe =
50− App (Type (Var.fromModule ["Port"] name)) [tipe]
51− in
52− case portType of
53− Inbound tipe -> makeType "InboundPort" tipe
54− Internal tipe -> makeType "Port" tipe
55− Outbound tipe -> makeType "OutboundPort" tipe
48+data PortDirection var
49+ = Inbound (Type var)
50+ | Internal
51+ | Outbound (Type var)
52+ deriving (Show)
5653
5754
5855fieldMap :: [(String,a)] -> Map.Map String [a]
@@ -139,15 +136,7 @@ replace typeTable t =
139136
140137instance (Pretty var, Var.ToString var) => Pretty (PortType var) where
141138 pretty portType =
142− case portType of
143− Inbound tipe ->
144− P.text "InboundPort" <+> pretty tipe
145−
146− Internal tipe ->
147− P.text "Port" <+> pretty tipe
148−
149− Outbound tipe ->
150− P.text "OutboundPort" <+> pretty tipe
139+ pretty (tipe portType)
151140
152141
153142instance (Var.ToString var, Pretty var) => Pretty (Type var) where
src/Generate/JavaScript.hs+1−1
@@ -212,7 +212,7 @@ expression (A region expr) =
212212 T.Inbound tipe ->
213213 return (Port.inbound name tipe)
214214
215− T.Internal _ ->
215+ T.Internal ->
216216 return (Port.internal name)
217217
218218 T.Outbound tipe ->
src/Transform/Canonicalize.hs+12−7
@@ -206,8 +206,8 @@ declaration env decl =
206206 do Env.uses ["Native","Port"]
207207 Env.uses ["Native","JavaScript"]
208208 tipe' <- canonicalize Canonicalize.tipe "port" name env tipe
209− portType <- Port.check name tipe'
210− return $ D.Port name portType
209+ portDirection <- Port.check name tipe'
210+ return $ D.Port name (Type.PortType portDirection tipe')
211211
212212 D.Perform line expr ->
213213 do Env.uses ["Native","Task"]
@@ -304,12 +304,17 @@ expression env (A.A ann validExpr) =
304304 (,) <$> format (pattern env p)
305305 <*> expression (Env.addPattern p env) b
306306
307− Port name portType ->
307+ Port name direction ->
308308 Port name <$>
309− case portType of
310− Type.Inbound tipe -> Type.Inbound <$> tipe' env tipe
311− Type.Internal tipe -> Type.Internal <$> tipe' env tipe
312− Type.Outbound tipe -> Type.Outbound <$> tipe' env tipe
309+ case direction of
310+ Type.Inbound tipe ->
311+ Type.Inbound <$> tipe' env tipe
312+
313+ Type.Internal ->
314+ return Type.Internal
315+
316+ Type.Outbound tipe ->
317+ Type.Outbound <$> tipe' env tipe
313318
314319 Perform expr ->
315320 Perform <$> go expr
src/Transform/Canonicalize/Port.hs+3−3
@@ -20,7 +20,7 @@ throw err =
2020check
2121 :: String
2222 -> T.CanonicalType
23− -> Env.Canonicalizer [P.Doc] (T.PortType Var.Canonical)
23+ -> Env.Canonicalizer [P.Doc] (T.PortDirection Var.Canonical)
2424check name tipe =
2525 checkHelp name tipe tipe
2626
@@ -29,12 +29,12 @@ checkHelp
2929 :: String
3030 -> T.CanonicalType
3131 -> T.CanonicalType
32− -> Env.Canonicalizer [P.Doc] (T.PortType Var.Canonical)
32+ -> Env.Canonicalizer [P.Doc] (T.PortDirection Var.Canonical)
3333checkHelp name rootType tipe =
3434 case tipe of
3535 T.Aliased alias [(_,arg)] _
3636 | Var.is ["Port"] "Port" alias ->
37− return (T.Internal arg)
37+ return T.Internal
3838
3939 | Var.is ["Port"] "InboundPort" alias ->
4040 do validForeignType name In arg arg
src/Transform/Declaration.hs+1−1
@@ -117,7 +117,7 @@ toDefs moduleName decl =
117117 []
118118
119119 D.Port name tipe ->
120− [ definition name (A.none $ E.Port name tipe) (T.portTypeToType tipe) ]
120+ [ definition name (A.none $ E.Port name (T.direction tipe)) (T.tipe tipe) ]
121121
122122 D.Perform line expr ->
123123 let name = "$perform$" ++ show line