on d42941c help sign in
elk/src/Generate/JavaScript/Variable.hs
62 lines 2.1 kB history raw
1module Generate.JavaScript.Variable where
3import qualified AST.Helpers as Help
4import qualified AST.Module as Module
5import qualified AST.Variable as Var
6import qualified Data.List as List
7import qualified Data.Set as Set
8import qualified Generate.JavaScript.Helpers as JS
9import qualified Language.ECMAScript3.Syntax as JS
12swap :: Char -> Char -> Char -> Char
13swap from to c =
14 if c == from then to else c
17canonical :: Var.Canonical -> JS.Expression ()
18canonical (Var.Canonical home name) =
19 case Help.isOp name of
20 True -> JS.BracketRef () (JS.obj (home' ++ ["_op"])) (JS.string name)
21 False -> JS.obj (home' ++ [ varName name ])
22 where
23 home' =
24 case home of
25 Var.Local -> []
26 Var.BuiltIn -> []
27 Var.Module path -> [ moduleName path ]
30moduleName :: Module.Name -> String
31moduleName name =
32 '$' : List.intercalate "$" name
35varName :: String -> String
36varName x = map (swap '\'' '$') x'
37 where
38 x' = if Set.member x jsReserveds then '$' : x else x
41value :: Module.Name -> String -> JS.Expression ()
42value home name =
43 canonical (Var.Canonical (Var.Module home) name)
46jsReserveds :: Set.Set String
47jsReserveds = Set.fromList
48 [ "null", "undefined", "Nan", "Infinity", "true", "false", "eval"
49 , "arguments", "int", "byte", "char", "goto", "long", "final", "float"
50 , "short", "double", "native", "throws", "boolean", "abstract", "volatile"
51 , "transient", "synchronized", "function", "break", "case", "catch"
52 , "continue", "debugger", "default", "delete", "do", "else", "finally"
53 , "for", "function", "if", "in", "instanceof", "new", "return", "switch"
54 , "this", "throw", "try", "typeof", "var", "void", "while", "with", "class"
55 , "const", "enum", "export", "extends", "import", "super", "implements"
56 , "interface", "let", "package", "private", "protected", "public"
57 , "static", "yield"
58 -- reserved by the Elm runtime system
59 , "Elm", "ElmRuntime"
60 , "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9"
61 , "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9"
62 ]