@@ -3,7 +3,7 @@ module Generate.JavaScript.Ports (incoming, outgoing) where
4import Generate.JavaScript.Helpers
4import Generate.JavaScript.Helpers
5import qualified SourceSyntax.Helpers as Help
5import qualified SourceSyntax.Helpers as Help
6−import SourceSyntax.Type
6+import SourceSyntax.Type as T
7import Language.ECMAScript3.Syntax
7import Language.ECMAScript3.Syntax
9data JSType = JSNumber | JSBoolean | JSString | JSArray | JSObject [String]
9data JSType = JSNumber | JSBoolean | JSString | JSArray | JSObject [String]
@@ -28,10 +28,10 @@ incoming tipe =
28 case tipe of
28 case tipe of
29 Data "Signal.Signal" [t] ->
29 Data "Signal.Signal" [t] ->
30 obj "Native.Ports.incomingSignal" <| incoming t
30 obj "Native.Ports.incomingSignal" <| incoming t
31− _ -> ["v"] ==> go tipe (ref "v")
31+ _ -> ["v"] ==> inc tipe (ref "v")
33− go :: Type -> Expression () -> Expression ()
33+inc :: Type -> Expression () -> Expression ()
34− go tipe x =
34+inc tipe x =
35 case tipe of
35 case tipe of
36 Lambda _ _ -> error "functions should not be allowed through input ports"
36 Lambda _ _ -> error "functions should not be allowed through input ports"
37 Var _ -> error "type variables should not be allowed through input ports"
37 Var _ -> error "type variables should not be allowed through input ports"
@@ -51,7 +51,7 @@ incoming tipe =
51 | ctor == "Maybe.Maybe" ->
51 | ctor == "Maybe.Maybe" ->
52 CondExpr () (equal x (NullLit ()))
52 CondExpr () (equal x (NullLit ()))
53 (obj "Maybe.Nothing")
53 (obj "Maybe.Nothing")
54− (obj "Maybe.Just" <| go t x)
54+ (obj "Maybe.Just" <| inc t x)
56 | ctor == "_List" ->
56 | ctor == "_List" ->
57 check x JSArray (obj "_J.toList" <| array)
57 check x JSArray (obj "_J.toList" <| array)
@@ -63,7 +63,7 @@ incoming tipe =
63 tuple = ObjectLit () $ (PropId () (var "ctor"), string ctor) : values
63 tuple = ObjectLit () $ (PropId () (var "ctor"), string ctor) : values
64 values = zipWith convert [0..] ts
64 values = zipWith convert [0..] ts
65 convert n t = ( PropId () $ var ('_':show n)
65 convert n t = ( PropId () $ var ('_':show n)
66− , go t (BracketRef () x (IntLit () n)))
66+ , inc t (BracketRef () x (IntLit () n)))
68 Data _ _ -> error "bad ADT got to port generation code"
68 Data _ _ -> error "bad ADT got to port generation code"
@@ -73,18 +73,31 @@ incoming tipe =
74 object = ObjectLit () $ (PropId () (var "_"), ObjectLit () []) : keys
74 object = ObjectLit () $ (PropId () (var "_"), ObjectLit () []) : keys
75 keys = map convert fields
75 keys = map convert fields
76− convert (f,t) = (PropId () (var f), go t (DotRef () x (var f)))
76+ convert (f,t) = (PropId () (var f), inc t (DotRef () x (var f)))
78outgoing tipe =
78outgoing tipe =
79 case tipe of
79 case tipe of
80 Data "Signal.Signal" [t] ->
80 Data "Signal.Signal" [t] ->
81 obj "Native.Ports.outgoingSignal" <| outgoing t
81 obj "Native.Ports.outgoingSignal" <| outgoing t
82− _ -> ["v"] ==> go tipe (ref "v")
82+ _ -> ["v"] ==> out tipe (ref "v")
84− go :: Type -> Expression () -> Expression ()
84+out :: Type -> Expression () -> Expression ()
85− go tipe x =
85+out tipe x =
86− case tipe of
86+ case tipe of
87− Lambda _ _ -> error "unimplemented"
87+ Lambda _ _
88+ | numArgs > 1 && numArgs < 10 ->
89+ func (ref ('A':show numArgs) `call` (x:values))
90+ | otherwise -> func (foldl (<|) x values)
92+ ts = T.collectLambdas tipe
93+ numArgs = length ts - 1
94+ args = map (\n -> '_' : show n) [0..]
95+ values = zipWith inc (init ts) (map ref args)
96+ func body = function (take numArgs args)
97+ [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
98+ , ret (out (last ts) (ref "_r"))
88 Var _ -> error "type variables should not be allowed through input ports"
101 Var _ -> error "type variables should not be allowed through input ports"
89 Data ctor []
102 Data ctor []
90 | ctor `elem` ["Int","Float","Bool","String"] -> obj ("_J.from" ++ ctor) <| x
103 | ctor `elem` ["Int","Float","Bool","String"] -> obj ("_J.from" ++ ctor) <| x
@@ -104,7 +117,7 @@ outgoing tipe =
104 Data ctor ts | Help.isTuple ctor ->
117 Data ctor ts | Help.isTuple ctor ->
105 ArrayLit () $ zipWith convert [0..] ts
118 ArrayLit () $ zipWith convert [0..] ts
107− convert n t = go t $ DotRef () x $ var ('_':show n)
120+ convert n t = out t $ DotRef () x $ var ('_':show n)
109 Data _ _ -> error "bad ADT got to port generation code"
122 Data _ _ -> error "bad ADT got to port generation code"
@@ -114,4 +127,4 @@ outgoing tipe =
114 ObjectLit () keys
127 ObjectLit () keys
116 keys = map convert fields
129 keys = map convert fields
117− convert (f,t) = (PropId () (var f), go t (DotRef () x (var f)))
130+ convert (f,t) = (PropId () (var f), out t (DotRef () x (var f)))