git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix #328
90b40d6Phil Freeman
examples/passing/SignedNumericLiterals.purs+8−3
@@ -1,10 +1,15 @@
1module Main where
1module Main where
2
2
3− p = +0.5
3+ p = 0.5
4− q = +1
4+ q = 1
5 x = -1
5 x = -1
6 y = -0.5
6 y = -0.5
7 z = 0.5
7 z = 0.5
8 w = 1
8 w = 1
9+
10+ f :: Number -> Number
11+ f x = -x
12+
13+ test1 = 2 - 1
9
14
10− main = Debug.Trace.trace "Done"
15+ main = Debug.Trace.trace "Done"
src/Language/PureScript/Declarations.hs+4
@@ -214,6 +214,10 @@ data Value
214 --
214 --
215 | BooleanLiteral Bool
215 | BooleanLiteral Bool
216 -- |
216 -- |
217+ -- A prefix -, will be desugared
218+ --
219+ | UnaryMinus Value
220+ -- |
217 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
221 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
218 -- will be removed.
222 -- will be removed.
219 --
223 --
src/Language/PureScript/Parser/Common.hs+2−8
@@ -227,14 +227,8 @@ parseQualified parser = part []
227-- Parse an integer or floating point value
227-- Parse an integer or floating point value
228--
228--
229integerOrFloat :: P.Parsec String u (Either Integer Double)
229integerOrFloat :: P.Parsec String u (Either Integer Double)
230−integerOrFloat = (Right <$> P.try (signed PT.float) <|>
230+integerOrFloat = (Right <$> P.try (PT.float tokenParser) <|>
231− Left <$> P.try (signed PT.natural)) P.<?> "number"
231+ Left <$> P.try (PT.natural tokenParser)) P.<?> "number"
232− where
233− signed p = do
234− let sign = (P.char '-' >> return negate) <|> (optional (P.char '+') >> return id)
235− f <- sign
236− n <- p tokenParser
237− return (f n)
238
232
239-- |
233-- |
240-- Parse an identifier or parenthesized operator
234-- Parse an identifier or parenthesized operator
src/Language/PureScript/Parser/Declarations.hs+5−2
@@ -324,8 +324,11 @@ parseValue =
324 postfixTable2 = [ \v -> P.try (flip App <$> (C.indented *> indexersAndAccessors)) <*> pure v
324 postfixTable2 = [ \v -> P.try (flip App <$> (C.indented *> indexersAndAccessors)) <*> pure v
325 , \v -> flip (TypedValue True) <$> (P.try (C.lexeme (C.indented *> P.string "::")) *> parsePolyType) <*> pure v
325 , \v -> flip (TypedValue True) <$> (P.try (C.lexeme (C.indented *> P.string "::")) *> parsePolyType) <*> pure v
326 ]
326 ]
327+ operators = [ [ P.Prefix (C.lexeme (P.try (C.indented *> P.char '-') >> return UnaryMinus))
328+ ]
327− operators = [ [ P.Infix (C.lexeme (P.try (C.indented *> C.parseIdentInfix P.<?> "operator") >>= \ident ->
329+ , [ P.Infix (C.lexeme (P.try (C.indented *> C.parseIdentInfix P.<?> "operator") >>= \ident ->
328− return (BinaryNoParens ident))) P.AssocRight ]
330+ return (BinaryNoParens ident))) P.AssocRight
331+ ]
329 ]
332 ]
330
333
331parseStringBinder :: P.Parsec String ParseState Binder
334parseStringBinder :: P.Parsec String ParseState Binder
src/Language/PureScript/Sugar.hs+4−1
@@ -27,6 +27,8 @@ import Language.PureScript.Sugar.BindingGroups as S
27import Language.PureScript.Sugar.TypeClasses as S
27import Language.PureScript.Sugar.TypeClasses as S
28import Language.PureScript.Sugar.Names as S
28import Language.PureScript.Sugar.Names as S
29
29
30+import Control.Category ((>>>))
31+
30-- |
32-- |
31-- The desugaring pipeline proceeds as follows:
33-- The desugaring pipeline proceeds as follows:
32--
34--
@@ -45,7 +47,8 @@ import Language.PureScript.Sugar.Names as S
45-- * Qualify any unqualified names and types
47-- * Qualify any unqualified names and types
46--
48--
47desugar :: [Module] -> Either String [Module]
49desugar :: [Module] -> Either String [Module]
50+desugar = removeSignedLiterals
48−desugar = desugarDo
51+ >>> desugarDo
49 >=> desugarCasesModule
52 >=> desugarCasesModule
50 >=> desugarTypeDeclarationsModule
53 >=> desugarTypeDeclarationsModule
51 >=> desugarImports
54 >=> desugarImports
src/Language/PureScript/Sugar/Operators.hs+12−1
@@ -20,7 +20,8 @@
20{-# LANGUAGE Rank2Types, FlexibleContexts #-}
20{-# LANGUAGE Rank2Types, FlexibleContexts #-}
21
21
22module Language.PureScript.Sugar.Operators (
22module Language.PureScript.Sugar.Operators (
23− rebracket
23+ rebracket,
24+ removeSignedLiterals
24) where
25) where
25
26
26import Language.PureScript.Names
27import Language.PureScript.Names
@@ -42,6 +43,8 @@ import qualified Text.Parsec as P
42import qualified Text.Parsec.Pos as P
43import qualified Text.Parsec.Pos as P
43import qualified Text.Parsec.Expr as P
44import qualified Text.Parsec.Expr as P
44
45
46+import qualified Language.PureScript.Constants as C
47+
45-- |
48-- |
46-- Remove explicit parentheses and reorder binary operator applications
49-- Remove explicit parentheses and reorder binary operator applications
47--
50--
@@ -52,6 +55,14 @@ rebracket ms = do
52 let opTable = customOperatorTable fixities
55 let opTable = customOperatorTable fixities
53 mapM (rebracketModule opTable) ms
56 mapM (rebracketModule opTable) ms
54
57
58+removeSignedLiterals :: (D.Data d) => d -> d
59+removeSignedLiterals = G.everywhere (G.mkT go)
60+ where
61+ go (UnaryMinus (NumericLiteral (Left n))) = NumericLiteral (Left $ negate n)
62+ go (UnaryMinus (NumericLiteral (Right n))) = NumericLiteral (Right $ negate n)
63+ go (UnaryMinus val) = App (Var (Qualified (Just (ModuleName [ProperName C.prelude])) (Ident C.negate))) val
64+ go other = other
65+
55rebracketModule :: [[(Qualified Ident, Value -> Value -> Value, Associativity)]] -> Module -> Either String Module
66rebracketModule :: [[(Qualified Ident, Value -> Value -> Value, Associativity)]] -> Module -> Either String Module
56rebracketModule opTable (Module mn ds exts) = Module mn <$> (removeParens <$> G.everywhereM' (G.mkM (matchOperators opTable)) ds) <*> pure exts
67rebracketModule opTable (Module mn ds exts) = Module mn <$> (removeParens <$> G.everywhereM' (G.mkM (matchOperators opTable)) ds) <*> pure exts
57
68