on 8a2fc7c help sign in
37 lines 1.2 kB history raw
1{-# OPTIONS_GHC -W #-}
2module Generate.JavaScript.Helpers where
4import qualified SourceSyntax.Helpers as Help
5import Language.ECMAScript3.Syntax
7split :: String -> [String]
8split = go []
9 where
10 go vars str =
11 case break (=='.') str of
12 (x,_:rest) | Help.isOp x -> vars ++ [x ++ '.' : rest]
13 | otherwise -> go (vars ++ [x]) rest
14 (x,[]) -> vars ++ [x]
16var name = Id () name
17ref name = VarRef () (var name)
18prop name = PropId () (var name)
19f <| x = CallExpr () f [x]
20ret e = ReturnStmt () (Just e)
21args ==> e = FuncExpr () Nothing (map var args) [ ret e ]
22function args stmts = FuncExpr () Nothing (map var args) stmts
23call = CallExpr ()
24string = StringLit ()
26dotSep vars =
27 case vars of
28 x:xs -> foldl (DotRef ()) (ref x) (map var xs)
29 [] -> error "dotSep must be called on a non-empty list of variables"
31obj = dotSep . split
33equal a b = InfixExpr () OpStrictEq a b
34instanceof tipe x =
35 InfixExpr () OpLAnd (typeof "object" x) (InfixExpr () OpInstanceof x (ref tipe))
36typeof tipe x = equal (PrefixExpr () PrefixTypeof x) (string tipe)
37member field x = InfixExpr () OpIn (string field) x