on 0bc67dd help sign in
65 lines 1.8 kB history raw
1module Generate.JavaScript.Helpers where
3import AST.Annotation (Region)
4import qualified AST.PrettyPrint as PP
5import Language.ECMAScript3.Syntax
7-- Creating Variables
9var :: String -> Id ()
10var name = Id () name
12ref :: String -> Expression ()
13ref name = VarRef () (var name)
15prop :: String -> Prop ()
16prop name = PropId () (var name)
18string = StringLit ()
20obj :: [String] -> Expression ()
21obj vars =
22 case vars of
23 x:xs -> foldl (DotRef ()) (ref x) (map var xs)
24 [] -> error "dotSep must be called on a non-empty list of variables"
26-- Specific Utilities
28localModuleName :: String
29localModuleName = "$moduleName"
31throw :: String -> Region -> Expression ()
32throw kind region =
33 obj ["_U",kind] `call` [ ref localModuleName, string (PP.renderPretty region) ]
35-- Function Calls
37(<|) :: Expression () -> Expression () -> Expression ()
38(<|) f x = CallExpr () f [x]
40ret :: Expression () -> Statement ()
41ret e = ReturnStmt () (Just e)
43(==>) :: [String] -> Expression () -> Expression ()
44(==>) args e = FuncExpr () Nothing (map var args) [ ret e ]
46function :: [String] -> [Statement ()] -> Expression ()
47function args stmts = FuncExpr () Nothing (map var args) stmts
49call :: Expression () -> [Expression ()] -> Expression ()
50call = CallExpr ()
52-- Checks
54equal :: Expression () -> Expression () -> Expression ()
55equal a b = InfixExpr () OpStrictEq a b
57instanceof :: String -> Expression () -> Expression ()
58instanceof tipe x =
59 InfixExpr () OpLAnd (typeof "object" x) (InfixExpr () OpInstanceof x (ref tipe))
61typeof :: String -> Expression () -> Expression ()
62typeof tipe x = equal (PrefixExpr () PrefixTypeof x) (string tipe)
64member :: String -> Expression () -> Expression ()
65member field x = InfixExpr () OpIn (string field) x