on c8eeedf help sign in
166 lines 6.0 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 | Var.isTuple name -> incomingTuple [] x
56 | otherwise -> error "bad type got to incoming port generation code"
58 App f args ->
59 case f : args of
60 Type name : [t]
61 | Var.isMaybe name -> CondExpr () (equal x (NullLit ()))
62 (V.value "Maybe" "Nothing")
63 (V.value "Maybe" "Just" <| inc t x)
65 | Var.isList name -> check x JSArray (obj ["_L","fromArray"] <| array)
67 | Var.isArray name -> check x JSArray (obj ["_A","fromJSArray"] <| array)
68 where
69 array = DotRef () x (var "map") <| incoming t
71 Type name : ts
72 | Var.isTuple name -> incomingTuple ts x
74 _ -> error "bad ADT got to incoming port generation code"
76 Record _ (Just _) ->
77 error "bad record got to incoming port generation code"
79 Record fields Nothing ->
80 check x (JSObject (map fst fields)) object
81 where
82 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
83 keys = map convert fields
84 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
86incomingTuple :: [CanonicalType] -> Expression () -> Expression ()
87incomingTuple types x =
88 check x JSArray (ObjectLit () fields)
89 where
90 fields = (prop "ctor", ctor) : zipWith convert [0..] types
92 ctor = string ("_Tuple" ++ show (length types))
94 convert n t =
95 ( prop ('_':show n)
96 , inc t (BracketRef () x (IntLit () n))
97 )
99outgoing :: CanonicalType -> Expression ()
100outgoing tipe =
101 case tipe of
102 Aliased _ t -> outgoing t
104 App (Type v) [t]
105 | Var.isSignal v -> V.value "Native.Ports" "outgoingSignal" <| outgoing t
107 _ -> ["v"] ==> out tipe (ref "v")
109out :: CanonicalType -> Expression () -> Expression ()
110out tipe x =
111 case tipe of
112 Aliased _ t -> out t x
114 Lambda _ _
115 | numArgs > 1 && numArgs < 10 ->
116 func (ref ('A':show numArgs) `call` (x:values))
117 | otherwise -> func (foldl (<|) x values)
118 where
119 ts = T.collectLambdas tipe
120 numArgs = length ts - 1
121 args = map (\n -> '_' : show n) [0..]
122 values = zipWith inc (init ts) (map ref args)
123 func body = function (take numArgs args)
124 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
125 , ret (out (last ts) (ref "_r"))
126 ]
128 Var _ -> error "type variables should not be allowed through input ports"
130 Type (Var.Canonical Var.BuiltIn name)
131 | name `elem` ["Int","Float","Bool","String"] -> x
133 Type name
134 | Var.isJson name -> V.value "Native.Json" "toJS" <| x
135 | Var.isTuple name -> ArrayLit () []
136 | otherwise -> error "bad type got to outgoing port generation code"
138 App f args ->
139 case f : args of
140 Type name : [t]
141 | Var.isMaybe name ->
142 CondExpr () (equal (DotRef () x (var "ctor")) (string "Nothing"))
143 (NullLit ())
144 (out t (DotRef () x (var "_0")))
146 | Var.isArray name ->
147 DotRef () (obj ["_A","toJSArray"] <| x) (var "map") <| outgoing t
149 | Var.isList name ->
150 DotRef () (obj ["_L","toArray"] <| x) (var "map") <| outgoing t
152 Type name : ts
153 | Var.isTuple name ->
154 let convert n t = out t $ DotRef () x $ var ('_':show n)
155 in ArrayLit () $ zipWith convert [0..] ts
157 _ -> error "bad ADT got to outgoing port generation code"
159 Record _ (Just _) ->
160 error "bad record got to outgoing port generation code"
162 Record fields Nothing ->
163 ObjectLit () keys
164 where
165 keys = map convert fields
166 convert (f,t) = (PropId () (var f), out t (DotRef () x (var f)))