From 2a77d6bd5d932865217509464c80c087bef5c9ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 21 Aug 2022 19:31:07 +0200 Subject: Generic expression parser with integer operators --- src/Test.hs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/Test.hs') 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 -- cgit v1.2.3