on 2234b88 help sign in
217 lines 6.5 kB history raw
1module Generate.JavaScript.Ports (incoming, outgoing) where
3import qualified Data.List as List
4import Generate.JavaScript.Helpers
5import AST.Type as T
6import qualified AST.Variable as Var
7import Language.ECMAScript3.Syntax
10data JSType
11 = JSNumber
12 | JSBoolean
13 | JSString
14 | JSArray
15 | JSObject [String]
18typeToString :: JSType -> String
19typeToString tipe =
20 case tipe of
21 JSNumber -> "a number"
22 JSBoolean -> "a boolean (true or false)"
23 JSString -> "a string"
24 JSArray -> "an array"
25 JSObject fields ->
26 "an object with fields '" ++ List.intercalate "', '" fields ++ "'"
29_Array :: String -> Expression ()
30_Array functionName =
31 useLazy ["Elm","Native","Array"] functionName
34_List :: String -> Expression ()
35_List functionName =
36 useLazy ["Elm","Native","List"] functionName
39_Maybe :: String -> Expression ()
40_Maybe functionName =
41 useLazy ["Elm","Maybe"] functionName
44check :: Expression () -> JSType -> Expression () -> Expression ()
45check x jsType continue =
46 CondExpr () (jsFold OpLOr checks x) continue throw
47 where
48 jsFold op checks value =
49 foldl1 (InfixExpr () op) (map ($ value) checks)
51 throw =
52 obj ["_U","badPort"] `call` [ string (typeToString jsType), x ]
54 checks =
55 case jsType of
56 JSNumber -> [typeof "number"]
57 JSBoolean -> [typeof "boolean"]
58 JSString -> [typeof "string", instanceof "String"]
59 JSArray -> [instanceof "Array"]
60 JSObject fields -> [jsFold OpLAnd (typeof "object" : map member fields)]
63incoming :: CanonicalType -> Expression ()
64incoming tipe =
65 case tipe of
66 Aliased _ t -> incoming t
68 App (Type v) [t]
69 | Var.isSignal v -> obj ["_P","incomingSignal"] <| incoming t
71 _ -> ["v"] ==> inc tipe (ref "v")
74inc :: CanonicalType -> Expression () -> Expression ()
75inc tipe x =
76 case tipe of
77 Lambda _ _ -> error "functions should not be allowed through input ports"
78 Var _ -> error "type variables should not be allowed through input ports"
79 Aliased _ t ->
80 inc t x
82 Type (Var.Canonical Var.BuiltIn name)
83 | name == "Int" -> from JSNumber
84 | name == "Float" -> from JSNumber
85 | name == "Bool" -> from JSBoolean
86 | name == "String" -> from JSString
87 where
88 from checks = check x checks x
90 Type name
91 | Var.isJson name ->
92 x
94 | Var.isTuple name ->
95 incomingTuple [] x
97 | otherwise ->
98 error "bad type got to incoming port generation code"
100 App f args ->
101 case f : args of
102 Type name : [t]
103 | Var.isMaybe name ->
104 CondExpr ()
105 (equal x (NullLit ()))
106 (_Maybe "Nothing")
107 (_Maybe "Just" <| inc t x)
109 | Var.isList name ->
110 check x JSArray (_List "fromArray" <| array)
112 | Var.isArray name ->
113 check x JSArray (_Array "fromJSArray" <| array)
114 where
115 array = DotRef () x (var "map") <| incoming t
117 Type name : ts
118 | Var.isTuple name -> incomingTuple ts x
120 _ -> error "bad ADT got to incoming port generation code"
122 Record _ (Just _) ->
123 error "bad record got to incoming port generation code"
125 Record fields Nothing ->
126 check x (JSObject (map fst fields)) object
127 where
128 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
129 keys = map convert fields
130 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
133incomingTuple :: [CanonicalType] -> Expression () -> Expression ()
134incomingTuple types x =
135 check x JSArray (ObjectLit () fields)
136 where
137 fields = (prop "ctor", ctor) : zipWith convert [0..] types
139 ctor = string ("_Tuple" ++ show (length types))
141 convert n t =
142 ( prop ('_':show n)
143 , inc t (BracketRef () x (IntLit () n))
144 )
147outgoing :: CanonicalType -> Expression ()
148outgoing tipe =
149 case tipe of
150 Aliased _ t -> outgoing t
152 App (Type v) [t]
153 | Var.isSignal v -> obj ["_P","outgoingSignal"] <| outgoing t
155 _ -> ["v"] ==> out tipe (ref "v")
158out :: CanonicalType -> Expression () -> Expression ()
159out tipe x =
160 case tipe of
161 Aliased _ t -> out t x
163 Lambda _ _
164 | numArgs > 1 && numArgs < 10 ->
165 func (ref ('A':show numArgs) `call` (x:values))
166 | otherwise -> func (foldl (<|) x values)
167 where
168 ts = T.collectLambdas tipe
169 numArgs = length ts - 1
170 args = map (\n -> '_' : show n) [0..]
171 values = zipWith inc (init ts) (map ref args)
172 func body =
173 function (take numArgs args)
174 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
175 , ret (out (last ts) (ref "_r"))
176 ]
178 Var _ -> error "type variables should not be allowed through input ports"
180 Type (Var.Canonical Var.BuiltIn name)
181 | name `elem` ["Int","Float","Bool","String"] -> x
183 Type name
184 | Var.isJson name -> x
185 | Var.isTuple name -> ArrayLit () []
186 | otherwise -> error "bad type got to outgoing port generation code"
188 App f args ->
189 case f : args of
190 Type name : [t]
191 | Var.isMaybe name ->
192 CondExpr ()
193 (equal (DotRef () x (var "ctor")) (string "Nothing"))
194 (NullLit ())
195 (out t (DotRef () x (var "_0")))
197 | Var.isArray name ->
198 DotRef () (_Array "toJSArray" <| x) (var "map") <| outgoing t
200 | Var.isList name ->
201 DotRef () (_List "toArray" <| x) (var "map") <| outgoing t
203 Type name : ts
204 | Var.isTuple name ->
205 let convert n t = out t $ DotRef () x $ var ('_':show n)
206 in ArrayLit () $ zipWith convert [0..] ts
208 _ -> error "bad ADT got to outgoing port generation code"
210 Record _ (Just _) ->
211 error "bad record got to outgoing port generation code"
213 Record fields Nothing ->
214 ObjectLit () keys
215 where
216 keys = map convert fields
217 convert (f,t) = (PropId () (var f), out t (DotRef () x (var f)))