on f9f5d8e help sign in
97 lines 1.9 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 =
11 Id () name
14ref :: String -> Expression ()
15ref name =
16 VarRef () (var name)
19prop :: String -> Prop ()
20prop name =
21 PropId () (var name)
24string = StringLit ()
27obj :: [String] -> Expression ()
28obj vars =
29 case vars of
30 x:xs -> foldl (DotRef ()) (ref x) (map var xs)
31 [] -> error "dotSep must be called on a non-empty list of variables"
34-- Specific Utilities
36localModuleName :: String
37localModuleName =
38 "$moduleName"
41throw :: String -> Region -> Expression ()
42throw kind region =
43 let args =
44 [ ref localModuleName
45 , string (PP.renderPretty region)
46 ]
47 in
48 obj ["_U",kind] `call` args
51-- Function Calls
53(<|) :: Expression () -> Expression () -> Expression ()
54(<|) f x =
55 CallExpr () f [x]
58ret :: Expression () -> Statement ()
59ret e =
60 ReturnStmt () (Just e)
63(==>) :: [String] -> Expression () -> Expression ()
64(==>) args e =
65 FuncExpr () Nothing (map var args) [ ret e ]
68function :: [String] -> [Statement ()] -> Expression ()
69function args stmts =
70 FuncExpr () Nothing (map var args) stmts
73call :: Expression () -> [Expression ()] -> Expression ()
74call =
75 CallExpr ()
78-- Checks
80equal :: Expression () -> Expression () -> Expression ()
81equal a b =
82 InfixExpr () OpStrictEq a b
85instanceof :: String -> Expression () -> Expression ()
86instanceof tipe x =
87 InfixExpr () OpLAnd (typeof "object" x) (InfixExpr () OpInstanceof x (ref tipe))
90typeof :: String -> Expression () -> Expression ()
91typeof tipe x =
92 equal (PrefixExpr () PrefixTypeof x) (string tipe)
95member :: String -> Expression () -> Expression ()
96member field x =
97 InfixExpr () OpIn (string field) x