git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Mixed function argument syntax
commit46189dd80fbffb3ccb0541e475e8b2d739436fea authorPhil Freeman <paf31@cantab.net> dateSun 10 Nov 2013, 12:02 (13 years ago) parentdfb07d2
3 files changed+25−14
examples/passing/Functions.ps+11 src/Language/PureScript/Parser/Values.hs+13−13 src/Language/PureScript/Pretty/JS.hs+1−1
examples/passing/Functions.psnew file+11
@@ -0,0 +1,11 @@
1+test1 = \ -> 0
2+
3+test2 = \() -> 0
4+
5+test3 = \a (b, c) d -> a + b + c + d
6+
7+test4 = \(a) -> a
8+
9+test5 = \(%%) -> 1 %% 2
10+
11+test6 = \((+++), (***)) -> 1 +++ 2 *** 3
src/Language/PureScript/Parser/Values.hs+13−13
@@ -59,20 +59,20 @@ parseIdentifierAndValue = (,) <$> (C.indented *> C.identifier <* C.indented <* C
5959parseAbs :: P.Parsec String ParseState Value
6060parseAbs = do
6161 C.lexeme $ P.char '\\'
62− uncurriedAbs <|> curriedAbs
62+ args <- P.many (C.indented *> (P.try singleArg <|> manyArgs))
63+ C.lexeme $ C.indented *> P.string "->"
64+ value <- parseValue
65+ return $ toFunction args value
6366 where
64− uncurriedAbs :: P.Parsec String ParseState Value
65− uncurriedAbs = do
66− args <- C.indented *> C.parens ((C.indented *> C.parseIdent) `P.sepBy` (C.indented *> C.comma))
67− C.lexeme $ C.indented *> P.string "->"
68− value <- parseValue
69− return $ Abs args value
70− curriedAbs :: P.Parsec String ParseState Value
71− curriedAbs = do
72− args <- P.many1 (C.indented *> C.parseIdent)
73− C.lexeme $ C.indented *> P.string "->"
74− value <- parseValue
75− return $ foldl (\ret arg -> Abs [arg] ret) value args
67+ manyArgs :: P.Parsec String ParseState (Value -> Value)
68+ manyArgs = do
69+ args <- C.parens ((C.indented *> C.parseIdent) `P.sepBy` (C.indented *> C.comma))
70+ return $ Abs args
71+ singleArg :: P.Parsec String ParseState (Value -> Value)
72+ singleArg = Abs . return <$> C.parseIdent
73+ toFunction :: [Value -> Value] -> Value -> Value
74+ toFunction [] value = Abs [] value
75+ toFunction args value = foldr (($)) value args
7676
7777parseApp :: P.Parsec String ParseState Value
7878parseApp = App <$> parseValue
src/Language/PureScript/Pretty/JS.hs+1−1
@@ -115,7 +115,7 @@ prettyPrintJS = fromMaybe (error "Incomplete pattern") . pattern matchValue
115115 , [ Wrap app $ \args val -> val ++ "(" ++ args ++ ")" ]
116116 , [ Wrap lam $ \(name, args) ret -> "function "
117117 ++ maybe "" identToJs name
118− ++ "(" ++ intercalate "," (map identToJs args) ++ ") "
118+ ++ "(" ++ intercalate ", " (map identToJs args) ++ ") "
119119 ++ ret ]
120120 , [ Wrap conditional $ \(th, el) cond -> cond ++ " ? " ++ prettyPrintJS th ++ " : " ++ prettyPrintJS el ]
121121 , [ binary LessThan "<" ]