git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Object getters
commit4813b5f3dd6e844e2ec79e27d7a18501d231167f authorGary Burgess <gary.burgess@gmail.com> dateSat 7 Feb 2015, 11:33 (12 years ago) parent36fed27
4 files changed+20
examples/passing/ObjectGetter.purs+9 src/Language/PureScript/AST/Declarations.hs+5 src/Language/PureScript/Parser/Declarations.hs+4 src/Language/PureScript/Sugar/ObjectWildcards.hs+2
examples/passing/ObjectGetter.pursnew file+9
@@ -0,0 +1,9 @@
1+module Main where
2+
3+getX = _.x
4+
5+point = { x: 1, y: 0 }
6+
7+main = do
8+ Debug.Trace.print $ getX point
9+ Debug.Trace.trace $ _.value { value: "Done!" }
src/Language/PureScript/AST/Declarations.hs+5
@@ -325,6 +325,11 @@ data Expr
325325 --
326326 | ObjectConstructor [(String, Maybe Expr)]
327327 -- |
328+ -- An object property getter (e.g. `_.x`). This will be removed during
329+ -- desugaring and expanded into a lambda that reads a property from an object.
330+ --
331+ | ObjectGetter String
332+ -- |
328333 -- An record property accessor expression
329334 --
330335 | Accessor String Expr
src/Language/PureScript/Parser/Declarations.hs+4
@@ -345,6 +345,7 @@ parseValueAtom = P.choice
345345 , P.try parseBooleanLiteral
346346 , parseArrayLiteral
347347 , P.try parseObjectLiteral
348+ , parseObjectGetter
348349 , parseAbs
349350 , P.try parseConstructor
350351 , P.try parseVar
@@ -383,6 +384,9 @@ parseDoNotationElement = P.choice
383384 , parseDoNotationLet
384385 , P.try (DoNotationValue <$> parseValue) ]
385386
387+parseObjectGetter :: TokenParser Expr
388+parseObjectGetter = ObjectGetter <$> (underscore *> C.indented *> dot *> C.indented *> (lname <|> stringLiteral))
389+
386390-- |
387391-- Parse a value
388392--
src/Language/PureScript/Sugar/ObjectWildcards.hs+2
@@ -37,6 +37,8 @@ desugarObjectConstructors (Module mn ds exts) = Module mn (map desugarDecl ds) e
3737 in if null args
3838 then ObjectLiteral $ second fromJust `map` props
3939 else foldr (Abs . Left . Ident . fst) (ObjectLiteral (mkProp `map` ps)) args
40+ desugarExpr (ObjectGetter prop) =
41+ Abs (Left (Ident "obj")) (Accessor prop (Var (Qualified Nothing (Ident "obj"))))
4042 desugarExpr e = e
4143
4244 mkProp :: (String, Maybe Expr) -> (String, Expr)