on 48928dc help sign in
203 lines 6.3 kB history raw
1module Generate.JavaScript.Ports (incoming, outgoing) where
3import qualified Data.List as List
4import Generate.JavaScript.Helpers
5import qualified Generate.JavaScript.Variable as V
6import AST.Type as T
7import qualified AST.Variable as Var
8import Language.ECMAScript3.Syntax
11data JSType
12 = JSNumber
13 | JSBoolean
14 | JSString
15 | JSArray
16 | JSObject [String]
19typeToString :: JSType -> String
20typeToString tipe =
21 case tipe of
22 JSNumber -> "a number"
23 JSBoolean -> "a boolean (true or false)"
24 JSString -> "a string"
25 JSArray -> "an array"
26 JSObject fields ->
27 "an object with fields '" ++ List.intercalate "', '" fields ++ "'"
30check :: Expression () -> JSType -> Expression () -> Expression ()
31check x jsType continue =
32 CondExpr () (jsFold OpLOr checks x) continue throw
33 where
34 jsFold op checks value =
35 foldl1 (InfixExpr () op) (map ($ value) checks)
37 throw =
38 obj ["_U","badPort"] `call` [ string (typeToString jsType), x ]
40 checks =
41 case jsType of
42 JSNumber -> [typeof "number"]
43 JSBoolean -> [typeof "boolean"]
44 JSString -> [typeof "string", instanceof "String"]
45 JSArray -> [instanceof "Array"]
46 JSObject fields -> [jsFold OpLAnd (typeof "object" : map member fields)]
49incoming :: CanonicalType -> Expression ()
50incoming tipe =
51 case tipe of
52 Aliased _ t -> incoming t
54 App (Type v) [t]
55 | Var.isSignal v -> obj ["_P","incomingSignal"] <| incoming t
57 _ -> ["v"] ==> inc tipe (ref "v")
60inc :: CanonicalType -> Expression () -> Expression ()
61inc tipe x =
62 case tipe of
63 Lambda _ _ -> error "functions should not be allowed through input ports"
64 Var _ -> error "type variables should not be allowed through input ports"
65 Aliased _ t ->
66 inc t x
68 Type (Var.Canonical Var.BuiltIn name)
69 | name == "Int" -> from JSNumber
70 | name == "Float" -> from JSNumber
71 | name == "Bool" -> from JSBoolean
72 | name == "String" -> from JSString
73 where
74 from checks = check x checks x
76 Type name
77 | Var.isJson name ->
78 x
80 | Var.isTuple name ->
81 incomingTuple [] x
83 | otherwise ->
84 error "bad type got to incoming port generation code"
86 App f args ->
87 case f : args of
88 Type name : [t]
89 | Var.isMaybe name ->
90 CondExpr ()
91 (equal x (NullLit ()))
92 (V.value ["Maybe"] "Nothing")
93 (V.value ["Maybe"] "Just" <| inc t x)
95 | Var.isList name ->
96 check x JSArray (obj ["_L","fromArray"] <| array)
98 | Var.isArray name ->
99 check x JSArray (obj ["_A","fromJSArray"] <| array)
100 where
101 array = DotRef () x (var "map") <| incoming t
103 Type name : ts
104 | Var.isTuple name -> incomingTuple ts x
106 _ -> error "bad ADT got to incoming port generation code"
108 Record _ (Just _) ->
109 error "bad record got to incoming port generation code"
111 Record fields Nothing ->
112 check x (JSObject (map fst fields)) object
113 where
114 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
115 keys = map convert fields
116 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
119incomingTuple :: [CanonicalType] -> Expression () -> Expression ()
120incomingTuple types x =
121 check x JSArray (ObjectLit () fields)
122 where
123 fields = (prop "ctor", ctor) : zipWith convert [0..] types
125 ctor = string ("_Tuple" ++ show (length types))
127 convert n t =
128 ( prop ('_':show n)
129 , inc t (BracketRef () x (IntLit () n))
130 )
133outgoing :: CanonicalType -> Expression ()
134outgoing tipe =
135 case tipe of
136 Aliased _ t -> outgoing t
138 App (Type v) [t]
139 | Var.isSignal v -> obj ["_P","outgoingSignal"] <| outgoing t
141 _ -> ["v"] ==> out tipe (ref "v")
144out :: CanonicalType -> Expression () -> Expression ()
145out tipe x =
146 case tipe of
147 Aliased _ t -> out t x
149 Lambda _ _
150 | numArgs > 1 && numArgs < 10 ->
151 func (ref ('A':show numArgs) `call` (x:values))
152 | otherwise -> func (foldl (<|) x values)
153 where
154 ts = T.collectLambdas tipe
155 numArgs = length ts - 1
156 args = map (\n -> '_' : show n) [0..]
157 values = zipWith inc (init ts) (map ref args)
158 func body =
159 function (take numArgs args)
160 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
161 , ret (out (last ts) (ref "_r"))
162 ]
164 Var _ -> error "type variables should not be allowed through input ports"
166 Type (Var.Canonical Var.BuiltIn name)
167 | name `elem` ["Int","Float","Bool","String"] -> x
169 Type name
170 | Var.isJson name -> x
171 | Var.isTuple name -> ArrayLit () []
172 | otherwise -> error "bad type got to outgoing port generation code"
174 App f args ->
175 case f : args of
176 Type name : [t]
177 | Var.isMaybe name ->
178 CondExpr ()
179 (equal (DotRef () x (var "ctor")) (string "Nothing"))
180 (NullLit ())
181 (out t (DotRef () x (var "_0")))
183 | Var.isArray name ->
184 DotRef () (obj ["_A","toJSArray"] <| x) (var "map") <| outgoing t
186 | Var.isList name ->
187 DotRef () (obj ["_L","toArray"] <| x) (var "map") <| outgoing t
189 Type name : ts
190 | Var.isTuple name ->
191 let convert n t = out t $ DotRef () x $ var ('_':show n)
192 in ArrayLit () $ zipWith convert [0..] ts
194 _ -> error "bad ADT got to outgoing port generation code"
196 Record _ (Just _) ->
197 error "bad record got to outgoing port generation code"
199 Record fields Nothing ->
200 ObjectLit () keys
201 where
202 keys = map convert fields
203 convert (f,t) = (PropId () (var f), out t (DotRef () x (var f)))