Skip to content

Commit

Permalink
Add support for not equal in Bash parser. Re: fosskers#174.
Browse files Browse the repository at this point in the history
  • Loading branch information
joneshf committed Dec 16, 2013
1 parent 8cd4369 commit 14b9842
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Bash/Base.hs
Expand Up @@ -37,7 +37,9 @@ data BashIf = If Comparison [Field] (Maybe BashIf)
| Else [Field]
deriving (Eq,Show)

data Comparison = Comp BashString BashString deriving (Eq,Show)
data Comparison = CompEq BashString BashString
| CompNe BashString BashString
deriving (Eq,Show)

data BashFor = Incr -- for (x;y;z); do ... done -- Incomplete!
| Iter String BashString [Field] -- for x in y; do ... done
Expand Down
9 changes: 7 additions & 2 deletions Bash/Parser.hs
Expand Up @@ -174,9 +174,14 @@ comparison :: Parser Comparison
comparison = do
spaces >> leftBs >> spaces
left <- head `fmap` single
try (string "= ") <|> string "== " <|> string "-eq "
compOp <- comparisonOp
right <- head `fmap` single
rightBs
return (Comp left right) <?> "valid comparison"
return (compOp left right) <?> "valid comparison"
where leftBs = skipMany1 $ char '['
rightBs = skipMany1 $ char ']'

comparisonOp :: Parser (BashString -> BashString -> Comparison)
comparisonOp = choice [eq, neq]
where eq = CompEq <$ (string "= " <|> string "== " <|> string "-eq ")
neq = CompNe <$ (string "!= " <|> string "-ne ")
10 changes: 8 additions & 2 deletions Bash/Simplify.hs
Expand Up @@ -89,12 +89,18 @@ replaceStr' s = get >>= \ns ->

-- | An `if` statement can have an [el]if or [el]se, but it might not.
replaceIf :: BashIf -> State Namespace [Field]
replaceIf i@(If (Comp l r) fs el) = do
replaceIf i@(If comp fs el) = do
let (op, l, r) = deComp comp
left <- fromBashString `fmap` replaceStr l
right <- fromBashString `fmap` replaceStr r
if left == right -- Only checks for equality at the moment.
if left `op` right
then replace fs
else case el of
Nothing -> return [IfBlock i]
Just el' -> replaceIf el'
replaceIf (Else fs) = replace fs

-- | Pull out operation, and both sides to a comparison.
deComp :: Eq a => Comparison -> ((a -> a -> Bool), BashString, BashString)
deComp (CompEq l r) = ((==), l, r)
deComp (CompNe l r) = ((/=), l, r)

0 comments on commit 14b9842

Please sign in to comment.