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