git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #318 from garyb/where-clauses
a4aeab0Phil Freeman
examples/passing/Where.pursnew file+49
@@ -0,0 +1,49 @@
1+module Main where
2+
3+import Prelude
4+import Control.Monad.Eff
5+import Control.Monad.ST
6+
7+test1 x = y
8+ where
9+ y :: Number
10+ y = x + 1
11+
12+test2 x y = x' + y'
13+ where
14+ x' = x + 1
15+ y' = y + 1
16+
17+
18+test3 = f 1 2 3
19+ where f x y z = x + y + z
20+
21+
22+test4 = f (+) [1, 2]
23+ where f x [y, z] = x y z
24+
25+
26+test5 = g 10
27+ where
28+ f x | x > 0 = g (x / 2) + 1
29+ f x = 0
30+ g x = f (x - 1) + 1
31+
32+test6 = if f true then f 1 else f 2
33+ where f :: forall a. a -> a
34+ f x = x
35+
36+test7 :: Number -> Number
37+test7 x = go x
38+ where
39+ go y | (x - 0.1 < y * y) && (y * y < x + 0.1) = y
40+ go y = go $ (y + x / y) / 2
41+
42+main = do
43+ Debug.Trace.print (test1 1)
44+ Debug.Trace.print (test2 1 2)
45+ Debug.Trace.print test3
46+ Debug.Trace.print test4
47+ Debug.Trace.print test5
48+ Debug.Trace.print test6
49+ Debug.Trace.print (test7 100)
src/Language/PureScript/Parser/Declarations.hs+11−6
@@ -60,12 +60,17 @@ parseTypeSynonymDeclaration =
60 <*> (lexeme (indented *> P.char '=') *> parsePolyType)
60 <*> (lexeme (indented *> P.char '=') *> parsePolyType)
61
61
62parseValueDeclaration :: P.Parsec String ParseState Declaration
62parseValueDeclaration :: P.Parsec String ParseState Declaration
63−parseValueDeclaration =
63+parseValueDeclaration = do
64− ValueDeclaration <$> parseIdent
64+ name <- parseIdent
65− <*> pure Value
66− <*> P.many parseBinderNoParens
65+ binders <- P.many parseBinderNoParens
67− <*> P.optionMaybe parseGuard
66+ guard <- P.optionMaybe parseGuard
68− <*> (lexeme (indented *> P.char '=') *> parseValue)
67+ value <- lexeme (indented *> P.char '=') *> parseValue
68+ whereClause <- P.optionMaybe $ do
69+ C.indented
70+ reserved "where"
71+ C.indented
72+ C.mark $ P.many1 (C.same *> parseLocalDeclaration)
73+ return $ ValueDeclaration name Value binders guard (maybe value (`Let` value) whereClause)
69
74
70parseExternDeclaration :: P.Parsec String ParseState Declaration
75parseExternDeclaration :: P.Parsec String ParseState Declaration
71parseExternDeclaration = P.try (reserved "foreign") *> indented *> reserved "import" *> indented *>
76parseExternDeclaration = P.try (reserved "foreign") *> indented *> reserved "import" *> indented *>