git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Use indexer for JS reserved idents where possible
3a2e274Gary Burgess
docs/source/prelude.rst+3−3
@@ -52,7 +52,7 @@ Type Classes
52 (<$>) :: forall b. forall a. (a -> b) -> f a -> f b
52 (<$>) :: forall b. forall a. (a -> b) -> f a -> f b
53
53
54 class Monad m where
54 class Monad m where
55− $return :: forall a. a -> m a
55+ return :: forall a. a -> m a
56 (>>=) :: forall b. forall a. m a -> (a -> m b) -> m b
56 (>>=) :: forall b. forall a. m a -> (a -> m b) -> m b
57
57
58 class Num a where
58 class Num a where
@@ -119,8 +119,6 @@ Values
119
119
120::
120::
121
121
122− $const :: forall b. forall a. a -> b -> a
123−
124 (!!) :: forall a. [a] -> Prim.Number -> a
122 (!!) :: forall a. [a] -> Prim.Number -> a
125
123
126 (#) :: forall b. forall a. a -> (a -> b) -> b
124 (#) :: forall b. forall a. a -> (a -> b) -> b
@@ -135,6 +133,8 @@ Values
135
133
136 boolOr :: Prim.Boolean -> Prim.Boolean -> Prim.Boolean
134 boolOr :: Prim.Boolean -> Prim.Boolean -> Prim.Boolean
137
135
136+ const :: forall b. forall a. a -> b -> a
137+
138 flip :: forall c. forall b. forall a. (a -> b -> c) -> b -> a -> c
138 flip :: forall c. forall b. forall a. (a -> b -> c) -> b -> a -> c
139
139
140 numAdd :: Prim.Number -> Prim.Number -> Prim.Number
140 numAdd :: Prim.Number -> Prim.Number -> Prim.Number
examples/passing/JSReserved.purs+20−7
@@ -1,11 +1,11 @@
1−module Main where
1+module JSReserved where
2
2
3 import Prelude
3 import Prelude
4
4
5− yield = "test"
5+ yield = 0
6− member = "test"
6+ member = 1
7
7
8− fortest = {
8+ this = {
9 var function = 0;
9 var function = 0;
10 for (i <- 0 until 10) {
10 for (i <- 0 until 10) {
11 function = function + i;
11 function = function + i;
@@ -13,7 +13,7 @@ module Main where
13 return function;
13 return function;
14 }
14 }
15
15
16− whiletest = {
16+ catch = {
17 var for = 0;
17 var for = 0;
18 while (for > 0) {
18 while (for > 0) {
19 for = for - 1;
19 for = for - 1;
@@ -21,10 +21,23 @@ module Main where
21 return for;
21 return for;
22 }
22 }
23
23
24− vartest = {
24+ instanceof = {
25 var var = 1;
25 var var = 1;
26 var = var + 1;
26 var = var + 1;
27 return var;
27 return var;
28 }
28 }
29+
30+ public = \return -> return
31+
32+module Main where
33+
34+ import Prelude
35+ import Arrays
36+ import JSReserved
29
37
30− main = Trace.trace "Done"
38+ main = Trace.print [ yield
39+ , member
40+ , this
41+ , catch
42+ , instanceof
43+ , public 1 ]
examples/passing/Operators.purs+3
@@ -50,6 +50,8 @@ module Operators2 where
50 test11 = [1, 2, 3] ... [4, 5, 6]
50 test11 = [1, 2, 3] ... [4, 5, 6]
51
51
52 test12 (<%>) a b = a <%> b
52 test12 (<%>) a b = a <%> b
53+
54+ test13 = \(<%>) a b -> a <%> b
53
55
54module Main where
56module Main where
55
57
@@ -75,4 +77,5 @@ main =
75 , show test10
77 , show test10
76 , show test11
78 , show test11
77 , show (test12 k 1 2)
79 , show (test12 k 1 2)
80+ , show (test13 k 1 2)
78 ]
81 ]
libraries/prelude/README.md+3−3
@@ -44,7 +44,7 @@
44 (<$>) :: forall b. forall a. (a -> b) -> f a -> f b
44 (<$>) :: forall b. forall a. (a -> b) -> f a -> f b
45
45
46 class Monad m where
46 class Monad m where
47− $return :: forall a. a -> m a
47+ return :: forall a. a -> m a
48 (>>=) :: forall b. forall a. m a -> (a -> m b) -> m b
48 (>>=) :: forall b. forall a. m a -> (a -> m b) -> m b
49
49
50 class Num a where
50 class Num a where
@@ -107,8 +107,6 @@
107
107
108### Values
108### Values
109
109
110− $const :: forall b. forall a. a -> b -> a
111−
112 (!!) :: forall a. [a] -> Prim.Number -> a
110 (!!) :: forall a. [a] -> Prim.Number -> a
113
111
114 (#) :: forall b. forall a. a -> (a -> b) -> b
112 (#) :: forall b. forall a. a -> (a -> b) -> b
@@ -123,6 +121,8 @@
123
121
124 boolOr :: Prim.Boolean -> Prim.Boolean -> Prim.Boolean
122 boolOr :: Prim.Boolean -> Prim.Boolean -> Prim.Boolean
125
123
124+ const :: forall b. forall a. a -> b -> a
125+
126 flip :: forall c. forall b. forall a. (a -> b -> c) -> b -> a -> c
126 flip :: forall c. forall b. forall a. (a -> b -> c) -> b -> a -> c
127
127
128 numAdd :: Prim.Number -> Prim.Number -> Prim.Number
128 numAdd :: Prim.Number -> Prim.Number -> Prim.Number
src/Language/PureScript/CodeGen/Common.hs+74−2
@@ -21,15 +21,22 @@ import Language.PureScript.Names
21-- |
21-- |
22-- Convert an Ident into a valid Javascript identifier:
22-- Convert an Ident into a valid Javascript identifier:
23--
23--
24−-- * Alphanumeric characters are kept unmodified
24+-- * Alphanumeric characters are kept unmodified.
25--
25--
26+-- * Reserved javascript identifiers are prefixed with '$$'.
27+--
26−-- * Symbols are encoded as a dollar symbol ($) followed by their ordinal value
28+-- * Symbols are prefixed with '$' followed by a symbol name or their ordinal value.
27--
29--
28identToJs :: Ident -> String
30identToJs :: Ident -> String
31+identToJs (Ident name) | nameIsJsReserved name = "$$" ++ name
29identToJs (Ident name) = concatMap identCharToString name
32identToJs (Ident name) = concatMap identCharToString name
30identToJs (Op op) = concatMap identCharToString op
33identToJs (Op op) = concatMap identCharToString op
31identToJs (Escaped name) = name
34identToJs (Escaped name) = name
32
35
36+-- |
37+-- Attempts to find a human-readable name for a symbol, if none has been specified returns the
38+-- ordinal value.
39+--
33identCharToString :: Char -> String
40identCharToString :: Char -> String
34identCharToString c | isAlphaNum c = [c]
41identCharToString c | isAlphaNum c = [c]
35identCharToString '_' = "_"
42identCharToString '_' = "_"
@@ -54,3 +61,68 @@ identCharToString '\\' = "$bslash"
54identCharToString '?' = "$qmark"
61identCharToString '?' = "$qmark"
55identCharToString '@' = "$at"
62identCharToString '@' = "$at"
56identCharToString c = '$' : show (ord c)
63identCharToString c = '$' : show (ord c)
64+
65+-- |
66+-- Checks whether an identifier name is reserved in Javascript.
67+--
68+nameIsJsReserved :: String -> Bool
69+nameIsJsReserved name =
70+ elem name [ "abstract"
71+ , "boolean"
72+ , "break"
73+ , "byte"
74+ , "case"
75+ , "catch"
76+ , "char"
77+ , "class"
78+ , "const"
79+ , "continue"
80+ , "debugger"
81+ , "default"
82+ , "delete"
83+ , "do"
84+ , "double"
85+ , "else"
86+ , "enum"
87+ , "export"
88+ , "extends"
89+ , "final"
90+ , "finally"
91+ , "float"
92+ , "for"
93+ , "function"
94+ , "goto"
95+ , "if"
96+ , "implements"
97+ , "import"
98+ , "in"
99+ , "instanceof"
100+ , "int"
101+ , "interface"
102+ , "let"
103+ , "long"
104+ , "native"
105+ , "new"
106+ , "package"
107+ , "private"
108+ , "protected"
109+ , "public"
110+ , "return"
111+ , "short"
112+ , "static"
113+ , "super"
114+ , "switch"
115+ , "synchronized"
116+ , "this"
117+ , "throw"
118+ , "throws"
119+ , "transient"
120+ , "try"
121+ , "typeof"
122+ , "var"
123+ , "void"
124+ , "volatile"
125+ , "while"
126+ , "with"
127+ , "yield" ]
128+
src/Language/PureScript/CodeGen/JS.hs+14−11
@@ -41,7 +41,7 @@ import Language.PureScript.Options
41import Language.PureScript.CodeGen.JS.AST as AST
41import Language.PureScript.CodeGen.JS.AST as AST
42import Language.PureScript.Types
42import Language.PureScript.Types
43import Language.PureScript.CodeGen.Optimize
43import Language.PureScript.CodeGen.Optimize
44−import Language.PureScript.CodeGen.Common (identToJs)
44+import Language.PureScript.CodeGen.Common
45import Language.PureScript.TypeChecker.Monad (canonicalizeDataConstructor)
45import Language.PureScript.TypeChecker.Monad (canonicalizeDataConstructor)
46
46
47-- |
47-- |
@@ -87,14 +87,18 @@ declToJs _ _ _ _ = Nothing
87
87
88setProperty :: Ident -> JS -> ModuleName -> [JS]
88setProperty :: Ident -> JS -> ModuleName -> [JS]
89setProperty ident@(Op op) val (ModuleName (ProperName moduleName)) =
89setProperty ident@(Op op) val (ModuleName (ProperName moduleName)) =
90− [ JSAssignment (JSAccessor (identToJs ident) (JSVar moduleName)) val
90+ [ JSAssignment (accessor ident (JSVar moduleName)) val
91− , JSAssignment (JSIndexer (JSStringLiteral op) (JSVar moduleName)) (JSAccessor (identToJs ident) (JSVar moduleName)) ]
91+ , JSAssignment (JSIndexer (JSStringLiteral op) (JSVar moduleName)) (accessor ident (JSVar moduleName)) ]
92setProperty ident val (ModuleName (ProperName moduleName)) =
92setProperty ident val (ModuleName (ProperName moduleName)) =
93− [ JSAssignment (JSAccessor (identToJs ident) (JSVar moduleName)) val ]
93+ [ JSAssignment (accessor ident (JSVar moduleName)) val ]
94
94
95var :: Ident -> JS
95var :: Ident -> JS
96var = JSVar . identToJs
96var = JSVar . identToJs
97
97
98+accessor :: Ident -> JS -> JS
99+accessor (Ident name) | nameIsJsReserved name = JSIndexer (JSStringLiteral name)
100+accessor ident = JSAccessor (identToJs ident)
101+
98valueToJs :: Options -> ModuleName -> Environment -> Value -> JS
102valueToJs :: Options -> ModuleName -> Environment -> Value -> JS
99valueToJs _ _ _ (NumericLiteral n) = JSNumericLiteral n
103valueToJs _ _ _ (NumericLiteral n) = JSNumericLiteral n
100valueToJs _ _ _ (StringLiteral s) = JSStringLiteral s
104valueToJs _ _ _ (StringLiteral s) = JSStringLiteral s
@@ -104,9 +108,9 @@ valueToJs opts m e (ObjectLiteral ps) = JSObjectLiteral (map (second (valueToJs
104valueToJs opts m e (ObjectUpdate o ps) = JSApp (JSAccessor "extend" (JSVar "Object")) [ valueToJs opts m e o, JSObjectLiteral (map (second (valueToJs opts m e)) ps)]
108valueToJs opts m e (ObjectUpdate o ps) = JSApp (JSAccessor "extend" (JSVar "Object")) [ valueToJs opts m e o, JSObjectLiteral (map (second (valueToJs opts m e)) ps)]
105valueToJs _ m e (Constructor (Qualified Nothing name)) =
109valueToJs _ m e (Constructor (Qualified Nothing name)) =
106 case M.lookup (m, name) (dataConstructors e) of
110 case M.lookup (m, name) (dataConstructors e) of
107− Just (_, Alias aliasModule aliasIdent) -> qualifiedToJS identToJs (Qualified (Just aliasModule) aliasIdent)
111+ Just (_, Alias aliasModule aliasIdent) -> qualifiedToJS id (Qualified (Just aliasModule) aliasIdent)
108 _ -> JSVar . runProperName $ name
112 _ -> JSVar . runProperName $ name
109−valueToJs _ _ _ (Constructor name) = qualifiedToJS runProperName name
113+valueToJs _ _ _ (Constructor name) = qualifiedToJS (Ident . runProperName) name
110valueToJs opts m e (Block sts) = JSApp (JSFunction Nothing [] (JSBlock (map (statementToJs opts m e) sts))) []
114valueToJs opts m e (Block sts) = JSApp (JSFunction Nothing [] (JSBlock (map (statementToJs opts m e) sts))) []
111valueToJs opts m e (Case values binders) = bindersToJs opts m e binders (map (valueToJs opts m e) values)
115valueToJs opts m e (Case values binders) = bindersToJs opts m e binders (map (valueToJs opts m e) values)
112valueToJs opts m e (IfThenElse cond th el) = JSConditional (valueToJs opts m e cond) (valueToJs opts m e th) (valueToJs opts m e el)
116valueToJs opts m e (IfThenElse cond th el) = JSConditional (valueToJs opts m e cond) (valueToJs opts m e th) (valueToJs opts m e el)
@@ -156,17 +160,16 @@ varToJs m e qual@(Qualified _ ident) = go qual
156 Just (_, Alias aliasModule aliasIdent) -> go (Qualified (Just aliasModule) aliasIdent)
160 Just (_, Alias aliasModule aliasIdent) -> go (Qualified (Just aliasModule) aliasIdent)
157 _ -> case qual of
161 _ -> case qual of
158 Qualified Nothing _ -> var ident
162 Qualified Nothing _ -> var ident
159− Qualified (Just (ModuleName (ProperName mn))) (Op op) -> JSIndexer (JSStringLiteral op) (JSVar mn)
160− _ -> qualifiedToJS identToJs qual
163+ _ -> qualifiedToJS id qual
161 isExtern (Extern ForeignImport) = True
164 isExtern (Extern ForeignImport) = True
162 isExtern (Alias m' ident') = case M.lookup (m', ident') (names e) of
165 isExtern (Alias m' ident') = case M.lookup (m', ident') (names e) of
163 Just (_, ty') -> isExtern ty'
166 Just (_, ty') -> isExtern ty'
164 Nothing -> error "Undefined alias in varToJs"
167 Nothing -> error "Undefined alias in varToJs"
165 isExtern _ = False
168 isExtern _ = False
166
169
167−qualifiedToJS :: (a -> String) -> Qualified a -> JS
170+qualifiedToJS :: (a -> Ident) -> Qualified a -> JS
168−qualifiedToJS f (Qualified (Just (ModuleName (ProperName m))) a) = JSAccessor (f a) (JSVar m)
171+qualifiedToJS f (Qualified (Just (ModuleName (ProperName m))) a) = accessor (f a) (JSVar m)
169−qualifiedToJS f (Qualified Nothing a) = JSVar (f a)
172+qualifiedToJS f (Qualified Nothing a) = JSVar $ identToJs (f a)
170
173
171bindersToJs :: Options -> ModuleName -> Environment -> [([Binder], Maybe Guard, Value)] -> [JS] -> JS
174bindersToJs :: Options -> ModuleName -> Environment -> [([Binder], Maybe Guard, Value)] -> [JS] -> JS
172bindersToJs opts m e binders vals = runGen (map identToJs (unusedNames (binders, vals))) $ do
175bindersToJs opts m e binders vals = runGen (map identToJs (unusedNames (binders, vals))) $ do
src/Language/PureScript/Parser/Common.hs+1−69
@@ -52,69 +52,6 @@ reservedPsNames = [ "data"
52
52
53 ]
53 ]
54
54
55−-- |
56−-- A list of javascript reserved identifiers
57−--
58−reservedJsNames :: [String]
59−reservedJsNames = [ "abstract"
60− , "boolean"
61− , "break"
62− , "byte"
63− , "case"
64− , "catch"
65− , "char"
66− , "class"
67− , "const"
68− , "continue"
69− , "debugger"
70− , "default"
71− , "delete"
72− , "do"
73− , "double"
74− , "else"
75− , "enum"
76− , "export"
77− , "extends"
78− , "final"
79− , "finally"
80− , "float"
81− , "for"
82− , "function"
83− , "goto"
84− , "if"
85− , "implements"
86− , "import"
87− , "in"
88− , "instanceof"
89− , "int"
90− , "interface"
91− , "let"
92− , "long"
93− , "native"
94− , "new"
95− , "package"
96− , "private"
97− , "protected"
98− , "public"
99− , "return"
100− , "short"
101− , "static"
102− , "super"
103− , "switch"
104− , "synchronized"
105− , "this"
106− , "throw"
107− , "throws"
108− , "transient"
109− , "try"
110− , "typeof"
111− , "var"
112− , "void"
113− , "volatile"
114− , "while"
115− , "with"
116− , "yield" ]
117−
118-- |
55-- |
119-- A list of reserved identifiers for types
56-- A list of reserved identifiers for types
120--
57--
@@ -287,12 +224,7 @@ integerOrFloat = (Left <$> P.try (PT.natural tokenParser) <|>
287-- Parse an identifier or parenthesized operator
224-- Parse an identifier or parenthesized operator
288--
225--
289parseIdent :: P.Parsec String ParseState Ident
226parseIdent :: P.Parsec String ParseState Ident
290−parseIdent = parseIdent' <|> (Op <$> parens operator)
227+parseIdent = (Ident <$> identifier) <|> (Op <$> parens operator)
291− where
292− parseIdent' :: P.Parsec String ParseState Ident
293− parseIdent' = do
294− ident <- identifier
295− return $ if (ident `elem` reservedJsNames) then (Escaped $ "$" ++ ident) else (Ident ident)
296
228
297-- |
229-- |
298-- Parse a token inside square brackets
230-- Parse a token inside square brackets