on c8eeedf help sign in
66 lines 1.8 kB history raw
1{-# OPTIONS_GHC -W #-}
2module Generate.JavaScript.Helpers where
4import AST.Annotation (Region)
5import qualified AST.PrettyPrint as PP
6import Language.ECMAScript3.Syntax
8-- Creating Variables
10var :: String -> Id ()
11var name = Id () name
13ref :: String -> Expression ()
14ref name = VarRef () (var name)
16prop :: String -> Prop ()
17prop name = PropId () (var name)
19string = StringLit ()
21obj :: [String] -> Expression ()
22obj vars =
23 case vars of
24 x:xs -> foldl (DotRef ()) (ref x) (map var xs)
25 [] -> error "dotSep must be called on a non-empty list of variables"
27-- Specific Utilities
29localModuleName :: String
30localModuleName = "$moduleName"
32throw :: String -> Region -> Expression ()
33throw kind region =
34 obj ["_E",kind] `call` [ ref localModuleName, string (PP.renderPretty region) ]
36-- Function Calls
38(<|) :: Expression () -> Expression () -> Expression ()
39(<|) f x = CallExpr () f [x]
41ret :: Expression () -> Statement ()
42ret e = ReturnStmt () (Just e)
44(==>) :: [String] -> Expression () -> Expression ()
45(==>) args e = FuncExpr () Nothing (map var args) [ ret e ]
47function :: [String] -> [Statement ()] -> Expression ()
48function args stmts = FuncExpr () Nothing (map var args) stmts
50call :: Expression () -> [Expression ()] -> Expression ()
51call = CallExpr ()
53-- Checks
55equal :: Expression () -> Expression () -> Expression ()
56equal a b = InfixExpr () OpStrictEq a b
58instanceof :: String -> Expression () -> Expression ()
59instanceof tipe x =
60 InfixExpr () OpLAnd (typeof "object" x) (InfixExpr () OpInstanceof x (ref tipe))
62typeof :: String -> Expression () -> Expression ()
63typeof tipe x = equal (PrefixExpr () PrefixTypeof x) (string tipe)
65member :: String -> Expression () -> Expression ()
66member field x = InfixExpr () OpIn (string field) x