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