git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Better codegen for object binders
commit3a52374a2b77bdb702f655075cb86854126719f8 authorPhil Freeman <paf31@cantab.net> dateSat 5 Apr 2014, 15:30 (13 years ago) parent6691b8a
4 files changed+13−9
examples/passing/Objects.purs+3 src/Language/PureScript/CodeGen/JS.hs+9−6 src/Language/PureScript/CodeGen/JS/AST.hs−2 src/Language/PureScript/Parser/Declarations.hs+1−1
examples/passing/Objects.purs+3
@@ -23,5 +23,8 @@ module Main where
2323 where
2424 weirdObj :: { "!@#" :: Number }
2525 weirdObj = { "!@#": 1 }
26+
27+ test5 = case { "***": 1 } of
28+ { "***" = n } -> n
2629
2730 main = Debug.Trace.trace "Done"
src/Language/PureScript/CodeGen/JS.hs+9−6
@@ -116,9 +116,13 @@ var = JSVar . identToJs
116116-- indexer is returned.
117117--
118118accessor :: Ident -> JS -> JS
119−accessor (Ident name) | nameIsJsReserved name = JSIndexer (JSStringLiteral name)
119+accessor (Ident prop) = accessorString prop
120120accessor (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)
122126
123127-- |
124128-- 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 (
133137valueToJs _ m _ (Constructor name) = qualifiedToJS m (Ident . runProperName) name
134138valueToJs opts m e (Case values binders) = bindersToJs opts m e binders (map (valueToJs opts m e) values)
135139valueToJs 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)
137− | otherwise = JSIndexer (JSStringLiteral prop) (valueToJs opts m e val)
140+valueToJs opts m e (Accessor prop val) = accessorString prop (valueToJs opts m e val)
138141valueToJs opts m e (App val arg) = JSApp (valueToJs opts m e val) [valueToJs opts m e arg]
139142valueToJs opts m e (Let ds val) = JSApp (JSFunction Nothing [] (JSBlock (concat (mapMaybe (flip (declToJs opts m) e) ds) ++ [JSReturn $ valueToJs opts m e val]))) []
140143valueToJs 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 =
201204 let
202205 (pairs, _) = rowToList row
203206 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
205208 argumentCheck val (TypeApp (TypeApp t _) _) | t == tyFunction = [typeCheck val "function"]
206209 argumentCheck val (ForAll _ ty' _) = argumentCheck val ty'
207210 argumentCheck _ _ = []
@@ -286,7 +289,7 @@ binderToJs m e varName done (ObjectBinder bs) = go done bs
286289 propVar <- fresh
287290 done'' <- go done' bs'
288291 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)
290293binderToJs m e varName done (ArrayBinder bs) = do
291294 js <- go done 0 bs
292295 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
1919
2020import Data.Data
2121
22−
23−
2422-- |
2523-- Built-in unary operators
2624--
src/Language/PureScript/Parser/Declarations.hs+1−1
@@ -372,7 +372,7 @@ parseNullBinder = C.lexeme (P.char '_') *> P.notFollowedBy C.identLetter *> retu
372372
373373parseIdentifierAndBinder :: P.Parsec String ParseState (String, Binder)
374374parseIdentifierAndBinder = do
375− name <- C.lexeme C.identifier
375+ name <- C.lexeme (C.identifier <|> C.stringLiteral)
376376 _ <- C.lexeme $ C.indented *> P.char '='
377377 binder <- C.indented *> parseBinder
378378 return (name, binder)