1{-# OPTIONS_GHC -Wall #-}
2module Generate.JavaScript.Variable
13import qualified Control.Monad.State as State
14import qualified Data.List as List
15import qualified Data.Set as Set
16import qualified Language.ECMAScript3.Syntax as JS
18import qualified AST.Helpers as Help
19import qualified AST.Module.Name as ModuleName
20import qualified AST.Variable as Var
21import qualified Elm.Package as Pkg
22import qualified Generate.JavaScript.Helpers as JS
29fresh :: State.State Int String
33 return ("_v" ++ show n)
40define :: Maybe ModuleName.Canonical -> String -> JS.Expression () -> [JS.Statement ()]
41define maybeHome name body =
42 if not (Help.isOp name) then
45 maybe unqualified qualified maybeHome name
47 [ JS.VarDeclStmt () [ JS.varDecl jsName body ] ]
52 error "can only define infix operators at the top level"
57 getOpsDictName (Var.TopLevel home)
60 JS.LBracket () (JS.ref opsDictName) (JS.StringLit () name)
62 [ JS.VarDeclStmt () [ JS.varDecl opsDictName (JS.refOrObject opsDictName) ]
63 , JS.ExprStmt () (JS.AssignExpr () JS.OpAssign lvalue body)
68-- INSTANTIATE VARIABLES
71canonical :: Var.Canonical -> JS.Expression ()
72canonical (Var.Canonical home name) =
73 if Help.isOp name then
74 JS.BracketRef () (JS.ref (getOpsDictName home)) (JS.StringLit () name)
79 JS.ref (unqualified name)
82 JS.ref (unqualified name)
84 Var.Module moduleName@(ModuleName.Canonical _ rawName) ->
85 if ModuleName.isNative rawName then
86 native moduleName name
89 JS.ref (qualified moduleName name)
91 Var.TopLevel moduleName ->
92 JS.ref (qualified moduleName name)
95unqualified :: String -> String
100qualified :: ModuleName.Canonical -> String -> String
101qualified moduleName name =
102 moduleToString moduleName ++ "$" ++ removePrimes name
105native :: ModuleName.Canonical -> String -> JS.Expression ()
106native moduleName name =
107 JS.obj [ moduleToString moduleName, name ]
110coreNative :: String -> String -> JS.Expression ()
111coreNative moduleName name =
112 native (ModuleName.inCore ["Native",moduleName]) name
115getOpsDictName :: Var.Home -> String
120 Var.Local -> error "infix operators should only be defined in top-level declarations"
121 Var.BuiltIn -> error "there should be no built-in infix operators"
122 Var.Module name -> name
123 Var.TopLevel name -> name
125 moduleToString moduleName ++ "_ops"
128moduleToString :: ModuleName.Canonical -> String
129moduleToString (ModuleName.Canonical (Pkg.Name user project) moduleName) =
132 map (swap '-' '_') user
135 map (swap '-' '_') project
138 List.intercalate "_" moduleName
140 '_' : safeUser ++ "$" ++ safeProject ++ "$" ++ safeModuleName
143swap :: Char -> Char -> Char -> Char
145 if c == from then to else c
152safe :: String -> String
155 if Set.member name jsReserveds then '$' : name else name
158removePrimes :: String -> String
160 map (swap '\'' '$') name
163jsReserveds :: Set.Set String
167 [ "null", "undefined", "Nan", "Infinity", "true", "false", "eval"
168 , "arguments", "int", "byte", "char", "goto", "long", "final", "float"
169 , "short", "double", "native", "throws", "boolean", "abstract", "volatile"
170 , "transient", "synchronized", "function", "break", "case", "catch"
171 , "continue", "debugger", "default", "delete", "do", "else", "finally"
172 , "for", "function", "if", "in", "instanceof", "new", "return", "switch"
173 , "this", "throw", "try", "typeof", "var", "void", "while", "with", "class"
174 , "const", "enum", "export", "extends", "import", "super", "implements"
175 , "interface", "let", "package", "private", "protected", "public"
177 -- reserved by the Elm runtime system
178 , "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9"
179 , "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list