on b63c613 help sign in
160 lines 5.9 kB history raw
1{-# OPTIONS_GHC -W #-}
2module Generate.JavaScript.Ports (incoming, outgoing) where
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
10data JSType = JSNumber | JSBoolean | JSString | JSArray | JSObject [String]
11 deriving Show
13check :: Expression () -> JSType -> Expression () -> Expression ()
14check x jsType continue =
15 CondExpr () (jsFold OpLOr checks x) continue throw
16 where
17 jsFold op checks value = foldl1 (InfixExpr () op) (map ($value) checks)
18 throw = obj ["_E","raise"] <| InfixExpr () OpAdd msg x
19 msg = string ("invalid input, expecting " ++ show jsType ++ " but got ")
20 checks = case jsType of
21 JSNumber -> [typeof "number"]
22 JSBoolean -> [typeof "boolean"]
23 JSString -> [typeof "string", instanceof "String"]
24 JSArray -> [(obj ["_U","isJSArray"] <|)]
25 JSObject fields -> [jsFold OpLAnd (typeof "object" : map member fields)]
27incoming :: CanonicalType -> Expression ()
28incoming tipe =
29 case tipe of
30 Aliased _ t -> incoming t
32 App (Type v) [t]
33 | Var.isSignal v -> V.value "Native.Ports" "incomingSignal" <| incoming t
35 _ -> ["v"] ==> inc tipe (ref "v")
37inc :: CanonicalType -> Expression () -> Expression ()
38inc tipe x =
39 case tipe of
40 Lambda _ _ -> error "functions should not be allowed through input ports"
41 Var _ -> error "type variables should not be allowed through input ports"
42 Aliased _ t ->
43 inc t x
45 Type (Var.Canonical Var.BuiltIn name)
46 | name == "Int" -> from JSNumber
47 | name == "Float" -> from JSNumber
48 | name == "Bool" -> from JSBoolean
49 | name == "String" -> from JSString
50 where
51 from checks = check x checks x
53 Type name
54 | Var.isJson name -> V.value "Native.Json" "fromJS" <| x
55 | otherwise -> error "bad type got to incoming port generation code"
57 App f args ->
58 case f : args of
59 Type name : [t]
60 | Var.isMaybe name -> CondExpr () (equal x (NullLit ()))
61 (V.value "Maybe" "Nothing")
62 (V.value "Maybe" "Just" <| inc t x)
64 | Var.isList name -> check x JSArray (obj ["_L","fromArray"] <| array)
66 | Var.isArray name -> check x JSArray (obj ["_A","fromJSArray"] <| array)
67 where
68 array = DotRef () x (var "map") <| incoming t
70 Type name : ts
71 | Var.isTuple name -> check x JSArray tuple
72 where
73 tuple = ObjectLit () $ (prop "ctor", ctor) : values
75 ctor = string ("_Tuple" ++ show (length ts))
76 values = zipWith convert [0..] ts
78 convert n t = ( prop ('_':show n)
79 , inc t (BracketRef () x (IntLit () n))
80 )
82 _ -> error "bad ADT got to incoming port generation code"
84 Record _ (Just _) ->
85 error "bad record got to incoming port generation code"
87 Record fields Nothing ->
88 check x (JSObject (map fst fields)) object
89 where
90 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
91 keys = map convert fields
92 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
94outgoing :: CanonicalType -> Expression ()
95outgoing tipe =
96 case tipe of
97 Aliased _ t -> outgoing t
99 App (Type v) [t]
100 | Var.isSignal v -> V.value "Native.Ports" "outgoingSignal" <| outgoing t
102 _ -> ["v"] ==> out tipe (ref "v")
104out :: CanonicalType -> Expression () -> Expression ()
105out tipe x =
106 case tipe of
107 Aliased _ t -> out t x
109 Lambda _ _
110 | numArgs > 1 && numArgs < 10 ->
111 func (ref ('A':show numArgs) `call` (x:values))
112 | otherwise -> func (foldl (<|) x values)
113 where
114 ts = T.collectLambdas tipe
115 numArgs = length ts - 1
116 args = map (\n -> '_' : show n) [0..]
117 values = zipWith inc (init ts) (map ref args)
118 func body = function (take numArgs args)
119 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
120 , ret (out (last ts) (ref "_r"))
121 ]
123 Var _ -> error "type variables should not be allowed through input ports"
125 Type (Var.Canonical Var.BuiltIn name)
126 | name `elem` ["Int","Float","Bool","String"] -> x
128 Type name
129 | Var.isJson name -> V.value "Native.Json" "toJS" <| x
130 | otherwise -> error "bad type got to outgoing port generation code"
132 App f args ->
133 case f : args of
134 Type name : [t]
135 | Var.isMaybe name ->
136 CondExpr () (equal (DotRef () x (var "ctor")) (string "Nothing"))
137 (NullLit ())
138 (out t (DotRef () x (var "_0")))
140 | Var.isArray name ->
141 DotRef () (obj ["_A","toJSArray"] <| x) (var "map") <| outgoing t
143 | Var.isList name ->
144 DotRef () (obj ["_L","toArray"] <| x) (var "map") <| outgoing t
146 Type name : ts
147 | Var.isTuple name ->
148 let convert n t = out t $ DotRef () x $ var ('_':show n)
149 in ArrayLit () $ zipWith convert [0..] ts
151 _ -> error "bad ADT got to outgoing port generation code"
153 Record _ (Just _) ->
154 error "bad record got to outgoing port generation code"
156 Record fields Nothing ->
157 ObjectLit () keys
158 where
159 keys = map convert fields
160 convert (f,t) = (PropId () (var f), out t (DotRef () x (var f)))