on c28b9df help sign in
194 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 -> V.value ["Native","Json"] "fromJS" <| x
77 | Var.isTuple name -> incomingTuple [] x
78 | otherwise -> error "bad type got to incoming port generation code"
80 App f args ->
81 case f : args of
82 Type name : [t]
83 | Var.isMaybe name ->
84 CondExpr ()
85 (equal x (NullLit ()))
86 (V.value ["Maybe"] "Nothing")
87 (V.value ["Maybe"] "Just" <| inc t x)
89 | Var.isList name ->
90 check x JSArray (obj ["_L","fromArray"] <| array)
92 | Var.isArray name ->
93 check x JSArray (obj ["_A","fromJSArray"] <| array)
94 where
95 array = DotRef () x (var "map") <| incoming t
97 Type name : ts
98 | Var.isTuple name -> incomingTuple ts x
100 _ -> error "bad ADT got to incoming port generation code"
102 Record _ (Just _) ->
103 error "bad record got to incoming port generation code"
105 Record fields Nothing ->
106 check x (JSObject (map fst fields)) object
107 where
108 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
109 keys = map convert fields
110 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
112incomingTuple :: [CanonicalType] -> Expression () -> Expression ()
113incomingTuple types x =
114 check x JSArray (ObjectLit () fields)
115 where
116 fields = (prop "ctor", ctor) : zipWith convert [0..] types
118 ctor = string ("_Tuple" ++ show (length types))
120 convert n t =
121 ( prop ('_':show n)
122 , inc t (BracketRef () x (IntLit () n))
123 )
125outgoing :: CanonicalType -> Expression ()
126outgoing tipe =
127 case tipe of
128 Aliased _ t -> outgoing t
130 App (Type v) [t]
131 | Var.isSignal v -> obj ["_P","outgoingSignal"] <| outgoing t
133 _ -> ["v"] ==> out tipe (ref "v")
135out :: CanonicalType -> Expression () -> Expression ()
136out tipe x =
137 case tipe of
138 Aliased _ t -> out t x
140 Lambda _ _
141 | numArgs > 1 && numArgs < 10 ->
142 func (ref ('A':show numArgs) `call` (x:values))
143 | otherwise -> func (foldl (<|) x values)
144 where
145 ts = T.collectLambdas tipe
146 numArgs = length ts - 1
147 args = map (\n -> '_' : show n) [0..]
148 values = zipWith inc (init ts) (map ref args)
149 func body =
150 function (take numArgs args)
151 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
152 , ret (out (last ts) (ref "_r"))
153 ]
155 Var _ -> error "type variables should not be allowed through input ports"
157 Type (Var.Canonical Var.BuiltIn name)
158 | name `elem` ["Int","Float","Bool","String"] -> x
160 Type name
161 | Var.isJson name -> V.value ["Native","Json"] "toJS" <| x
162 | Var.isTuple name -> ArrayLit () []
163 | otherwise -> error "bad type got to outgoing port generation code"
165 App f args ->
166 case f : args of
167 Type name : [t]
168 | Var.isMaybe name ->
169 CondExpr ()
170 (equal (DotRef () x (var "ctor")) (string "Nothing"))
171 (NullLit ())
172 (out t (DotRef () x (var "_0")))
174 | Var.isArray name ->
175 DotRef () (obj ["_A","toJSArray"] <| x) (var "map") <| outgoing t
177 | Var.isList name ->
178 DotRef () (obj ["_L","toArray"] <| x) (var "map") <| outgoing t
180 Type name : ts
181 | Var.isTuple name ->
182 let convert n t = out t $ DotRef () x $ var ('_':show n)
183 in ArrayLit () $ zipWith convert [0..] ts
185 _ -> error "bad ADT got to outgoing port generation code"
187 Record _ (Just _) ->
188 error "bad record got to outgoing port generation code"
190 Record fields Nothing ->
191 ObjectLit () keys
192 where
193 keys = map convert fields
194 convert (f,t) = (PropId () (var f), out t (DotRef () x (var f)))