summaryrefslogtreecommitdiff
path: root/src/Test.hs
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2022-08-21 19:31:07 +0200
committerRoman Smrž <roman.smrz@seznam.cz>2022-08-22 22:48:16 +0200
commit2a77d6bd5d932865217509464c80c087bef5c9ae (patch)
tree9da274411938bd30548723104e9a336d5732ae68 /src/Test.hs
parentf40765688cc5c383cbf07550b06e7843e3acfe45 (diff)
Generic expression parser with integer operators
Diffstat (limited to 'src/Test.hs')
-rw-r--r--src/Test.hs3
1 files changed, 3 insertions, 0 deletions
diff --git a/src/Test.hs b/src/Test.hs
index 16c1b1f..7b9be6f 100644
--- a/src/Test.hs
+++ b/src/Test.hs
@@ -96,6 +96,7 @@ data Expr a where
Literal :: ExprType a => a -> Expr a
Concat :: [Expr Text] -> Expr Text
Regex :: [Expr Text] -> Expr Regex
+ UnOp :: (b -> a) -> Expr b -> Expr a
BinOp :: (b -> c -> a) -> Expr b -> Expr c -> Expr a
eval :: MonadEval m => Expr a -> m a
@@ -114,6 +115,7 @@ eval (Regex xs) = do
case compile defaultCompOpt defaultExecOpt $ T.concat $ concat [[T.singleton '^'], parts, [T.singleton '$']] of
Left err -> fail err
Right re -> return re
+eval (UnOp f x) = f <$> eval x
eval (BinOp f x y) = f <$> eval x <*> eval y
gatherVars :: forall a m. MonadEval m => Expr a -> m [(VarName, SomeVarValue)]
@@ -124,4 +126,5 @@ gatherVars = fmap (uniqOn fst . sortOn fst) . helper
helper (Literal _) = return []
helper (Concat es) = concat <$> mapM helper es
helper (Regex es) = concat <$> mapM helper es
+ helper (UnOp _ e) = helper e
helper (BinOp _ e f) = (++) <$> helper e <*> helper f