git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix #328
commit90b40d62544da3254ebcf69057143fa5eed39595 authorPhil Freeman <paf31@cantab.net> dateThu 27 Mar 2014, 15:26 (13 years ago) parent29348e8
6 files changed+35−15
examples/passing/SignedNumericLiterals.purs+8−3 src/Language/PureScript/Declarations.hs+4 src/Language/PureScript/Parser/Common.hs+2−8 src/Language/PureScript/Parser/Declarations.hs+5−2 src/Language/PureScript/Sugar.hs+4−1 src/Language/PureScript/Sugar/Operators.hs+12−1
examples/passing/SignedNumericLiterals.purs+8−3
@@ -1,10 +1,15 @@
11module Main where
22
3− p = +0.5
4− q = +1
3+ p = 0.5
4+ q = 1
55 x = -1
66 y = -0.5
77 z = 0.5
88 w = 1
9+
10+ f :: Number -> Number
11+ f x = -x
12+
13+ test1 = 2 - 1
914
10− main = Debug.Trace.trace "Done"
15+ main = Debug.Trace.trace "Done"
src/Language/PureScript/Declarations.hs+4
@@ -214,6 +214,10 @@ data Value
214214 --
215215 | BooleanLiteral Bool
216216 -- |
217+ -- A prefix -, will be desugared
218+ --
219+ | UnaryMinus Value
220+ -- |
217221 -- Binary operator application. During the rebracketing phase of desugaring, this data constructor
218222 -- will be removed.
219223 --
src/Language/PureScript/Parser/Common.hs+2−8
@@ -227,14 +227,8 @@ parseQualified parser = part []
227227-- Parse an integer or floating point value
228228--
229229integerOrFloat :: P.Parsec String u (Either Integer Double)
230−integerOrFloat = (Right <$> P.try (signed PT.float) <|>
231− Left <$> P.try (signed PT.natural)) 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)
230+integerOrFloat = (Right <$> P.try (PT.float tokenParser) <|>
231+ Left <$> P.try (PT.natural tokenParser)) P.<?> "number"
238232
239233-- |
240234-- Parse an identifier or parenthesized operator
src/Language/PureScript/Parser/Declarations.hs+5−2
@@ -324,8 +324,11 @@ parseValue =
324324 postfixTable2 = [ \v -> P.try (flip App <$> (C.indented *> indexersAndAccessors)) <*> pure v
325325 , \v -> flip (TypedValue True) <$> (P.try (C.lexeme (C.indented *> P.string "::")) *> parsePolyType) <*> pure v
326326 ]
327− operators = [ [ P.Infix (C.lexeme (P.try (C.indented *> C.parseIdentInfix P.<?> "operator") >>= \ident ->
328− return (BinaryNoParens ident))) P.AssocRight ]
327+ operators = [ [ P.Prefix (C.lexeme (P.try (C.indented *> P.char '-') >> return UnaryMinus))
328+ ]
329+ , [ P.Infix (C.lexeme (P.try (C.indented *> C.parseIdentInfix P.<?> "operator") >>= \ident ->
330+ return (BinaryNoParens ident))) P.AssocRight
331+ ]
329332 ]
330333
331334parseStringBinder :: P.Parsec String ParseState Binder
src/Language/PureScript/Sugar.hs+4−1
@@ -27,6 +27,8 @@ import Language.PureScript.Sugar.BindingGroups as S
2727import Language.PureScript.Sugar.TypeClasses as S
2828import Language.PureScript.Sugar.Names as S
2929
30+import Control.Category ((>>>))
31+
3032-- |
3133-- The desugaring pipeline proceeds as follows:
3234--
@@ -45,7 +47,8 @@ import Language.PureScript.Sugar.Names as S
4547-- * Qualify any unqualified names and types
4648--
4749desugar :: [Module] -> Either String [Module]
48−desugar = desugarDo
50+desugar = removeSignedLiterals
51+ >>> desugarDo
4952 >=> desugarCasesModule
5053 >=> desugarTypeDeclarationsModule
5154 >=> desugarImports
src/Language/PureScript/Sugar/Operators.hs+12−1
@@ -20,7 +20,8 @@
2020{-# LANGUAGE Rank2Types, FlexibleContexts #-}
2121
2222module Language.PureScript.Sugar.Operators (
23− rebracket
23+ rebracket,
24+ removeSignedLiterals
2425) where
2526
2627import Language.PureScript.Names
@@ -42,6 +43,8 @@ import qualified Text.Parsec as P
4243import qualified Text.Parsec.Pos as P
4344import qualified Text.Parsec.Expr as P
4445
46+import qualified Language.PureScript.Constants as C
47+
4548-- |
4649-- Remove explicit parentheses and reorder binary operator applications
4750--
@@ -52,6 +55,14 @@ rebracket ms = do
5255 let opTable = customOperatorTable fixities
5356 mapM (rebracketModule opTable) ms
5457
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+
5566rebracketModule :: [[(Qualified Ident, Value -> Value -> Value, Associativity)]] -> Module -> Either String Module
5667rebracketModule opTable (Module mn ds exts) = Module mn <$> (removeParens <$> G.everywhereM' (G.mkM (matchOperators opTable)) ds) <*> pure exts
5768