git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Better codegen for object binders
3a52374Phil Freeman
examples/passing/Objects.purs+3
@@ -23,5 +23,8 @@ module Main where
23 where
23 where
24 weirdObj :: { "!@#" :: Number }
24 weirdObj :: { "!@#" :: Number }
25 weirdObj = { "!@#": 1 }
25 weirdObj = { "!@#": 1 }
26+
27+ test5 = case { "***": 1 } of
28+ { "***" = n } -> n
26
29
27 main = Debug.Trace.trace "Done"
30 main = Debug.Trace.trace "Done"
src/Language/PureScript/CodeGen/JS.hs+9−6
@@ -116,9 +116,13 @@ var = JSVar . identToJs
116-- indexer is returned.
116-- indexer is returned.
117--
117--
118accessor :: Ident -> JS -> JS
118accessor :: Ident -> JS -> JS
119−accessor (Ident name) | nameIsJsReserved name = JSIndexer (JSStringLiteral name)
119+accessor (Ident prop) = accessorString prop
120accessor (Op op) = JSIndexer (JSStringLiteral op)
120accessor (Op op) = JSIndexer (JSStringLiteral op)
121−accessor ident = JSAccessor (identToJs ident)
121+accessor (Escaped prop) = JSAccessor prop
122+
123+accessorString :: String -> JS -> JS
124+accessorString prop | isIdent prop = JSAccessor prop
125+ | otherwise = JSIndexer (JSStringLiteral prop)
122
126
123-- |
127-- |
124-- Generate code in the simplified Javascript intermediate representation for a value or expression.
128-- Generate code in the simplified Javascript intermediate representation for a value or expression.
@@ -133,8 +137,7 @@ valueToJs opts m e (ObjectUpdate o ps) = extendObj (valueToJs opts m e o) (map (
133valueToJs _ m _ (Constructor name) = qualifiedToJS m (Ident . runProperName) name
137valueToJs _ m _ (Constructor name) = qualifiedToJS m (Ident . runProperName) name
134valueToJs opts m e (Case values binders) = bindersToJs opts m e binders (map (valueToJs opts m e) values)
138valueToJs opts m e (Case values binders) = bindersToJs opts m e binders (map (valueToJs opts m e) values)
135valueToJs opts m e (IfThenElse cond th el) = JSConditional (valueToJs opts m e cond) (valueToJs opts m e th) (valueToJs opts m e el)
139valueToJs opts m e (IfThenElse cond th el) = JSConditional (valueToJs opts m e cond) (valueToJs opts m e th) (valueToJs opts m e el)
136−valueToJs opts m e (Accessor prop val) | isIdent prop = JSAccessor prop (valueToJs opts m e val)
140+valueToJs opts m e (Accessor prop val) = accessorString prop (valueToJs opts m e val)
137− | otherwise = JSIndexer (JSStringLiteral prop) (valueToJs opts m e val)
138valueToJs opts m e (App val arg) = JSApp (valueToJs opts m e val) [valueToJs opts m e arg]
141valueToJs opts m e (App val arg) = JSApp (valueToJs opts m e val) [valueToJs opts m e arg]
139valueToJs opts m e (Let ds val) = JSApp (JSFunction Nothing [] (JSBlock (concat (mapMaybe (flip (declToJs opts m) e) ds) ++ [JSReturn $ valueToJs opts m e val]))) []
142valueToJs opts m e (Let ds val) = JSApp (JSFunction Nothing [] (JSBlock (concat (mapMaybe (flip (declToJs opts m) e) ds) ++ [JSReturn $ valueToJs opts m e val]))) []
140valueToJs opts m e (Abs (Left arg) val) = JSFunction Nothing [identToJs arg] (JSBlock [JSReturn (valueToJs opts m (bindName m arg e) val)])
143valueToJs opts m e (Abs (Left arg) val) = JSFunction Nothing [identToJs arg] (JSBlock [JSReturn (valueToJs opts m (bindName m arg e) val)])
@@ -201,7 +204,7 @@ runtimeTypeChecks arg ty =
201 let
204 let
202 (pairs, _) = rowToList row
205 (pairs, _) = rowToList row
203 in
206 in
204− typeCheck val "object" : concatMap (\(prop, ty') -> argumentCheck (JSAccessor prop val) ty') pairs
207+ typeCheck val "object" : concatMap (\(prop, ty') -> argumentCheck (accessorString prop val) ty') pairs
205 argumentCheck val (TypeApp (TypeApp t _) _) | t == tyFunction = [typeCheck val "function"]
208 argumentCheck val (TypeApp (TypeApp t _) _) | t == tyFunction = [typeCheck val "function"]
206 argumentCheck val (ForAll _ ty' _) = argumentCheck val ty'
209 argumentCheck val (ForAll _ ty' _) = argumentCheck val ty'
207 argumentCheck _ _ = []
210 argumentCheck _ _ = []
@@ -286,7 +289,7 @@ binderToJs m e varName done (ObjectBinder bs) = go done bs
286 propVar <- fresh
289 propVar <- fresh
287 done'' <- go done' bs'
290 done'' <- go done' bs'
288 js <- binderToJs m e propVar done'' binder
291 js <- binderToJs m e propVar done'' binder
289− return (JSVariableIntroduction propVar (Just (JSAccessor prop (JSVar varName))) : js)
292+ return (JSVariableIntroduction propVar (Just (accessorString prop (JSVar varName))) : js)
290binderToJs m e varName done (ArrayBinder bs) = do
293binderToJs m e varName done (ArrayBinder bs) = do
291 js <- go done 0 bs
294 js <- go done 0 bs
292 return [JSIfElse (JSBinary EqualTo (JSAccessor "length" (JSVar varName)) (JSNumericLiteral (Left (fromIntegral $ length bs)))) (JSBlock js) Nothing]
295 return [JSIfElse (JSBinary EqualTo (JSAccessor "length" (JSVar varName)) (JSNumericLiteral (Left (fromIntegral $ length bs)))) (JSBlock js) Nothing]
src/Language/PureScript/CodeGen/JS/AST.hs−2
@@ -19,8 +19,6 @@ module Language.PureScript.CodeGen.JS.AST where
19
19
20import Data.Data
20import Data.Data
21
21
22−
23−
24-- |
22-- |
25-- Built-in unary operators
23-- Built-in unary operators
26--
24--
src/Language/PureScript/Parser/Declarations.hs+1−1
@@ -372,7 +372,7 @@ parseNullBinder = C.lexeme (P.char '_') *> P.notFollowedBy C.identLetter *> retu
372
372
373parseIdentifierAndBinder :: P.Parsec String ParseState (String, Binder)
373parseIdentifierAndBinder :: P.Parsec String ParseState (String, Binder)
374parseIdentifierAndBinder = do
374parseIdentifierAndBinder = do
375− name <- C.lexeme C.identifier
375+ name <- C.lexeme (C.identifier <|> C.stringLiteral)
376 _ <- C.lexeme $ C.indented *> P.char '='
376 _ <- C.lexeme $ C.indented *> P.char '='
377 binder <- C.indented *> parseBinder
377 binder <- C.indented *> parseBinder
378 return (name, binder)
378 return (name, binder)