prefix-expression 1.2.2 → 1.2.3
raw patch · 3 files changed
+7/−4 lines, 3 files
Files
- prefix-expression.cabal +1/−1
- src/Text/Exp/Prefix.hs +5/−3
- test/Text/Exp/PrefixSpec.hs +1/−0
prefix-expression.cabal view
@@ -1,5 +1,5 @@ name: prefix-expression-version: 1.2.2+version: 1.2.3 -- synopsis: description: convert infix to prefix expression homepage: https://github.com/VonFry/prefix-expression
src/Text/Exp/Prefix.hs view
@@ -7,6 +7,8 @@ import Data.Char (isSpace) import Text.Regex.PCRE ((=~)) +import Debug.Trace (trace)+ -- | a operators list which cantains a list in order with precedence. expOperators :: [[(String, Int)]] expOperators = [[("^", 2)], [("*", 2), ("/", 2), ("\\", 2), ("%", 2)], [("+", 2), ("-", 2)], [("=~", 2)], [("&&", 2)], [("||", 2)], [("!", 1)], [("<", 2), ("<=", 2), (">", 2), (">=", 2), ("==", 2)], [("(", 0), (")", 0)]]@@ -82,13 +84,13 @@ in if count' == 0 then checkOpArgs' stack'' op (count + 1) _count (len + 1) else let subLen = checkOpArgs' stack'' top 0 count' 1- stack''' = drop subLen stack''+ stack''' = drop (subLen - 1) stack'' in checkOpArgs' stack''' op (count + 1) _count (len + subLen) | count == _count = len | [] <- stack' , count /= _count- = len + 1+ = 0 | otherwise = len @@ -113,7 +115,7 @@ splitExp' sp exp | "" <- exp = filter (\x -> x /= "") sp | otherwise- = let match = exp =~ " +|'.*'|\".*\"|\\( *[\\+-][0-9A-Za-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
test/Text/Exp/PrefixSpec.hs view
@@ -17,6 +17,7 @@ fromInfix "a + b + c" `shouldBe` Right "+ + a b c" fromInfix "\"a\" + b + 'c'" `shouldBe` Right "+ + \"a\" b 'c'" fromInfix "a >= b" `shouldBe` Right ">= a b"+ fromInfix "(a > 1)&&(b < 1)" `shouldBe` Right "&& > a 1 < b 1" it "failed" $ do (isLeft $ fromInfix "!((1 + 3) * 3 - 4 ^ 5") `shouldBe` True (isLeft $ fromInfix "1 + + 2") `shouldBe` True