on 317f80e help sign in
199 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")
59inc :: CanonicalType -> Expression () -> Expression ()
60inc tipe x =
61 case tipe of
62 Lambda _ _ -> error "functions should not be allowed through input ports"
63 Var _ -> error "type variables should not be allowed through input ports"
64 Aliased _ t ->
65 inc t x
67 Type (Var.Canonical Var.BuiltIn name)
68 | name == "Int" -> from JSNumber
69 | name == "Float" -> from JSNumber
70 | name == "Bool" -> from JSBoolean
71 | name == "String" -> from JSString
72 where
73 from checks = check x checks x
75 Type name
76 | Var.isJson name ->
77 x
79 | Var.isTuple name ->
80 incomingTuple [] x
82 | otherwise ->
83 error "bad type got to incoming port generation code"
85 App f args ->
86 case f : args of
87 Type name : [t]
88 | Var.isMaybe name ->
89 CondExpr ()
90 (equal x (NullLit ()))
91 (V.value ["Maybe"] "Nothing")
92 (V.value ["Maybe"] "Just" <| inc t x)
94 | Var.isList name ->
95 check x JSArray (obj ["_L","fromArray"] <| array)
97 | Var.isArray name ->
98 check x JSArray (obj ["_A","fromJSArray"] <| array)
99 where
100 array = DotRef () x (var "map") <| incoming t
102 Type name : ts
103 | Var.isTuple name -> incomingTuple ts x
105 _ -> error "bad ADT got to incoming port generation code"
107 Record _ (Just _) ->
108 error "bad record got to incoming port generation code"
110 Record fields Nothing ->
111 check x (JSObject (map fst fields)) object
112 where
113 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
114 keys = map convert fields
115 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
117incomingTuple :: [CanonicalType] -> Expression () -> Expression ()
118incomingTuple types x =
119 check x JSArray (ObjectLit () fields)
120 where
121 fields = (prop "ctor", ctor) : zipWith convert [0..] types
123 ctor = string ("_Tuple" ++ show (length types))
125 convert n t =
126 ( prop ('_':show n)
127 , inc t (BracketRef () x (IntLit () n))
128 )
130outgoing :: CanonicalType -> Expression ()
131outgoing tipe =
132 case tipe of
133 Aliased _ t -> outgoing t
135 App (Type v) [t]
136 | Var.isSignal v -> obj ["_P","outgoingSignal"] <| outgoing t
138 _ -> ["v"] ==> out tipe (ref "v")
140out :: CanonicalType -> Expression () -> Expression ()
141out tipe x =
142 case tipe of
143 Aliased _ t -> out t x
145 Lambda _ _
146 | numArgs > 1 && numArgs < 10 ->
147 func (ref ('A':show numArgs) `call` (x:values))
148 | otherwise -> func (foldl (<|) x values)
149 where
150 ts = T.collectLambdas tipe
151 numArgs = length ts - 1
152 args = map (\n -> '_' : show n) [0..]
153 values = zipWith inc (init ts) (map ref args)
154 func body =
155 function (take numArgs args)
156 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
157 , ret (out (last ts) (ref "_r"))
158 ]
160 Var _ -> error "type variables should not be allowed through input ports"
162 Type (Var.Canonical Var.BuiltIn name)
163 | name `elem` ["Int","Float","Bool","String"] -> x
165 Type name
166 | Var.isJson name -> V.value ["Native","Json"] "toJS" <| x
167 | Var.isTuple name -> ArrayLit () []
168 | otherwise -> error "bad type got to outgoing port generation code"
170 App f args ->
171 case f : args of
172 Type name : [t]
173 | Var.isMaybe name ->
174 CondExpr ()
175 (equal (DotRef () x (var "ctor")) (string "Nothing"))
176 (NullLit ())
177 (out t (DotRef () x (var "_0")))
179 | Var.isArray name ->
180 DotRef () (obj ["_A","toJSArray"] <| x) (var "map") <| outgoing t
182 | Var.isList name ->
183 DotRef () (obj ["_L","toArray"] <| x) (var "map") <| outgoing t
185 Type name : ts
186 | Var.isTuple name ->
187 let convert n t = out t $ DotRef () x $ var ('_':show n)
188 in ArrayLit () $ zipWith convert [0..] ts
190 _ -> error "bad ADT got to outgoing port generation code"
192 Record _ (Just _) ->
193 error "bad record got to outgoing port generation code"
195 Record fields Nothing ->
196 ObjectLit () keys
197 where
198 keys = map convert fields
199 convert (f,t) = (PropId () (var f), out t (DotRef () x (var f)))