on 837a525 help sign in
elk/src/Generate/JavaScript/Variable.hs
55 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
11swap :: Char -> Char -> Char -> Char
12swap from to c = if c == from then to else c
14canonical :: Var.Canonical -> JS.Expression ()
15canonical (Var.Canonical home name) =
16 case Help.isOp name of
17 True -> JS.BracketRef () (JS.obj (home' ++ ["_op"])) (JS.string name)
18 False -> JS.obj (home' ++ [ varName name ])
19 where
20 home' =
21 case home of
22 Var.Local -> []
23 Var.BuiltIn -> []
24 Var.Module path -> [ moduleName path ]
26moduleName :: Module.Name -> String
27moduleName name =
28 '$' : List.intercalate "$" name
30varName :: String -> String
31varName x = map (swap '\'' '$') x'
32 where
33 x' = if Set.member x jsReserveds then '$' : x else x
35value :: Module.Name -> String -> JS.Expression ()
36value home name =
37 canonical (Var.Canonical (Var.Module home) name)
39jsReserveds :: Set.Set String
40jsReserveds = Set.fromList
41 [ "null", "undefined", "Nan", "Infinity", "true", "false", "eval"
42 , "arguments", "int", "byte", "char", "goto", "long", "final", "float"
43 , "short", "double", "native", "throws", "boolean", "abstract", "volatile"
44 , "transient", "synchronized", "function", "break", "case", "catch"
45 , "continue", "debugger", "default", "delete", "do", "else", "finally"
46 , "for", "function", "if", "in", "instanceof", "new", "return", "switch"
47 , "this", "throw", "try", "typeof", "var", "void", "while", "with", "class"
48 , "const", "enum", "export", "extends", "import", "super", "implements"
49 , "interface", "let", "package", "private", "protected", "public"
50 , "static", "yield"
51 -- reserved by the Elm runtime system
52 , "Elm", "ElmRuntime"
53 , "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9"
54 , "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9"
55 ]