on a7bb27d help sign in
130 lines 4.8 kB history raw
1{-# OPTIONS_GHC -W #-}
2module Generate.JavaScript.Ports (incoming, outgoing) where
4import Generate.JavaScript.Helpers
5import qualified SourceSyntax.Helpers as Help
6import SourceSyntax.Type as T
7import Language.ECMAScript3.Syntax
9data JSType = JSNumber | JSBoolean | JSString | JSArray | JSObject [String]
10 deriving Show
12check :: Expression () -> JSType -> Expression () -> Expression ()
13check x jsType continue =
14 CondExpr () (jsFold OpLOr checks x) continue throw
15 where
16 jsFold op checks value = foldl1 (InfixExpr () op) (map ($value) checks)
17 throw = obj "_E.raise" <| InfixExpr () OpAdd msg x
18 msg = string ("invalid input, expecting " ++ show jsType ++ " but got ")
19 checks = case jsType of
20 JSNumber -> [typeof "number"]
21 JSBoolean -> [typeof "boolean"]
22 JSString -> [typeof "string", instanceof "String"]
23 JSArray -> [instanceof "Array"]
24 JSObject fields -> [jsFold OpLAnd (typeof "object" : map member fields)]
26incoming :: Type -> Expression ()
27incoming tipe =
28 case tipe of
29 Data "Signal.Signal" [t] ->
30 obj "Native.Ports.incomingSignal" <| incoming t
31 _ -> ["v"] ==> inc tipe (ref "v")
33inc :: Type -> Expression () -> Expression ()
34inc tipe x =
35 case tipe of
36 Lambda _ _ -> error "functions should not be allowed through input ports"
37 Var _ -> error "type variables should not be allowed through input ports"
38 Data ctor []
39 | ctor == "Int" -> from JSNumber
40 | ctor == "Float" -> from JSNumber
41 | ctor == "Bool" -> from JSBoolean
42 | ctor == "String" -> from JSString
43 where
44 from checks = check x checks (obj ("_J.to" ++ ctor) <| x)
46 Data ctor [t]
47 | ctor == "Maybe.Maybe" ->
48 CondExpr () (equal x (NullLit ()))
49 (obj "Maybe.Nothing")
50 (obj "Maybe.Just" <| inc t x)
52 | ctor == "_List" ->
53 check x JSArray (obj "_J.toList" <| array)
54 where
55 array = DotRef () x (var "map") <| incoming t
57 Data ctor ts
58 | Help.isTuple ctor -> check x JSArray tuple
59 where
60 tuple = ObjectLit () $ (prop "ctor", string ctor) : values
61 values = zipWith convert [0..] ts
62 convert n t = ( prop ('_':show n)
63 , inc t (BracketRef () x (IntLit () n)))
65 Data _ _ ->
66 error "bad ADT got to port generation code"
68 Record _ (Just _) ->
69 error "bad record got to port generation code"
71 Record fields Nothing ->
72 check x (JSObject (map fst fields)) object
73 where
74 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
75 keys = map convert fields
76 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
78outgoing tipe =
79 case tipe of
80 Data "Signal.Signal" [t] ->
81 obj "Native.Ports.outgoingSignal" <| outgoing t
82 _ -> ["v"] ==> out tipe (ref "v")
84out :: Type -> Expression () -> Expression ()
85out tipe x =
86 case tipe of
87 Lambda _ _
88 | numArgs > 1 && numArgs < 10 ->
89 func (ref ('A':show numArgs) `call` (x:values))
90 | otherwise -> func (foldl (<|) x values)
91 where
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"))
99 ]
101 Var _ -> error "type variables should not be allowed through input ports"
102 Data ctor []
103 | ctor `elem` ["Int","Float","Bool","String"] ->
104 obj ("_J.from" ++ ctor) <| x
106 Data ctor [t]
107 | ctor == "Maybe.Maybe" ->
108 CondExpr () (equal (DotRef () x (var "ctor")) (string "Nothing"))
109 (NullLit ())
110 (out t (DotRef () x (var "_0")))
112 | ctor == "_List" ->
113 DotRef () (obj "_J.fromList" <| x) (var "map") <| outgoing t
115 Data ctor ts
116 | Help.isTuple ctor ->
117 let convert n t = out t $ DotRef () x $ var ('_':show n)
118 in ArrayLit () $ zipWith convert [0..] ts
120 Data _ _ ->
121 error "bad ADT got to port generation code"
123 Record _ (Just _) ->
124 error "bad record got to port generation code"
126 Record fields Nothing ->
127 ObjectLit () keys
128 where
129 keys = map convert fields
130 convert (f,t) = (PropId () (var f), out t (DotRef () x (var f)))