prefix-expression 1.0.0 → 1.0.1
raw patch · 3 files changed
+12/−11 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- prefix-expression.cabal +3/−3
- src/Text/Exp/Prefix.hs +6/−6
- test/Text/Exp/PrefixSpec.hs +3/−2
prefix-expression.cabal view
@@ -1,13 +1,13 @@ name: prefix-expression-version: 1.0.0+version: 1.0.1 -- synopsis: description: convert infix to prefix expression-homepage: https://github.com/VonFry/ParseExpression#readme+homepage: https://github.com/VonFry/prefix-expression license: GPL-3 license-file: LICENSE author: Vonfry maintainer: vonfry314@gmail.com-copyright: Copyright: © 2017 Vonfry+copyright: Copyright: © 2018 Vonfry category: Text build-type: Simple cabal-version: >=1.10
src/Text/Exp/Prefix.hs view
@@ -1,4 +1,4 @@--- | Convert infix exp to prefix exp. The expression's operators contains: @ + - * / \ ** || && ! =~ ( ) @+-- | Convert infix exp to prefix exp. The expression's operators contains: @ + - * / \ ^ || && ! =~ ( ) @ module Text.Exp.Prefix ( fromInfix ) where@@ -9,7 +9,7 @@ -- ^ a operators list which cantains a list in order with precedence. expOperators :: [[(String, Int)]]-expOperators = [[("**", 2)], [("*", 2), ("/", 2), ("\\", 2)], [("+", 2), ("-", 2)], [("=~", 2)], [("&&", 2)], [("||", 2)], [("!", 1)], [("(", 0), (")", 0)]]+expOperators = [[("^", 2)], [("*", 2), ("/", 2), ("\\", 2)], [("+", 2), ("-", 2)], [("=~", 2)], [("&&", 2)], [("||", 2)], [("!", 1)], [("(", 0), (")", 0)]] expOperators' = foldr (++) [] expOperators expOperators'' = unzip expOperators' @@ -60,7 +60,7 @@ = (opStack'', popedStack) checkInfixExp :: String -> Bool-checkInfixExp exp = exp =~ "((&&|\\*\\*|\\|\\||=~|\\(|\\)|\\+|-|\\*|/|\\\\) *(&&|\\*\\*|\\|\\||=~|\\(|\\)|\\+|-|\\*|/|\\\\))|([0-z]+ +[0-z]+)"+checkInfixExp exp = not $ exp =~ "((&&|\\|\\||\\^|=~|\\+|-|\\*|/|\\\\) *(&&|\\|\\||\\^|=~|\\+|-|\\*|/|\\\\))|([0-9A-Za-z]+ +[0-9A-Za-z]+)" checkOpArgs :: [String] -> Bool checkOpArgs stack = checkOpArgs' stack "" 0 0 0 == length stack@@ -113,12 +113,12 @@ splitExp' sp exp | "" <- exp = filter (\x -> x /= "") sp | otherwise- = let match = exp =~ " +|\\( *[\\+-][0-z]+ *\\)|&&|\\*\\*|\\|\\||=~|!|\\(|\\)|\\+|-|\\*|/|\\\\" :: String+ = let match = exp =~ " +|\\( *[\\+-][0-9A-Za-z]+ *\\)|&&|\\^|\\|\\||=~|!|\\(|\\)|\\+|-|\\*|/|\\\\" :: String in if match /= "" then let Just (idx, _) = elemSubIndex match exp (arg, exp') = splitAt idx exp (op, exp'') = splitAt (length match) exp'- op' = if op =~ "\\( *[\\+-][0-z]+ *\\)" :: Bool+ op' = if op =~ "\\( *[\\+-][0-9A-Za-z]+ *\\)" :: Bool then filter (not.(`elem`"()")) op else op in splitExp' (sp ++ [filter (not.isSpace) arg, filter (not.isSpace) op']) exp''@@ -126,7 +126,7 @@ doPrefixNegative :: String -> String doPrefixNegative exp =- let match = exp =~ "^ *[\\+-][0-z]+" :: String+ let match = exp =~ "^ *[\\+-][0-9A-Za-z]+" :: String in if match /= "" then let Just (t, d) = elemSubIndex match exp in take t exp ++ "(" ++ match ++ ")" ++ drop d exp
test/Text/Exp/PrefixSpec.hs view
@@ -12,10 +12,11 @@ spec = do describe "from infix" $ do it "correct" $ do- fromInfix "!((1 + 3) * 3 - 4 ** 5)" `shouldBe` Right "! - * + 1 3 3 ** 4 5"+ fromInfix "!((1 + 3) * 3 - 4 ^ 5)" `shouldBe` Right "! - * + 1 3 3 ^ 4 5" fromInfix "+2 * (-1) + 3" `shouldBe` Right "+ * +2 -1 3"+ fromInfix "a + b + c" `shouldBe` Right "+ + a b c" it "failed" $ do- (isLeft $ fromInfix "!((1 + 3) * 3 - 4 ** 5") `shouldBe` True+ (isLeft $ fromInfix "!((1 + 3) * 3 - 4 ^ 5") `shouldBe` True (isLeft $ fromInfix "1 + + 2") `shouldBe` True (isLeft $ fromInfix "(1 +) + 2") `shouldBe` True where