From cf6424b1864a563ac8dae60d13990e6b0d6a834a Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 11 Dec 2017 18:56:11 +0000 Subject: [PATCH] Clean up DateExpr module --- DateExpr.hs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/DateExpr.hs b/DateExpr.hs index 5f16c0a..e138dea 100644 --- a/DateExpr.hs +++ b/DateExpr.hs @@ -1,12 +1,16 @@ module DateExpr ( BoolExpr - , IntExpr + , parseBoolExpr + , firstMatchingDay + , evalBoolExpr ) where +import Data.List import Data.Time.Calendar import Data.Time.Calendar.WeekDate import Data.Time.Calendar.MonthDay import Data.Time.Calendar.Easter +import Text.Parsec data BoolExpr = BValue Bool | BStatement DateStatement @@ -37,6 +41,13 @@ data SpecialDate = SJulianDay | SMonthCount | SEaster +{- + - Evaluating expressions + -} + +firstMatchingDay :: BoolExpr -> Int -> Day -> Maybe Day +firstMatchingDay expr duration = find (evalBoolExpr expr) . take duration . iterate (addDays 1) + evalBoolExpr :: BoolExpr -> Day -> Bool evalBoolExpr (BValue v) _ = v evalBoolExpr (BStatement s) d = evalDateStatement s d @@ -71,6 +82,10 @@ evalSpecialDate SYearCount d = ((yearday d - 1) `div` 7) + 1 evalSpecialDate SMonthCount d = ((day d - 1) `div` 7) + 1 evalSpecialDate SEaster d = diffDays d $ orthodoxEaster $ year d +{- + - Helper functions for evaluation + -} + julian :: Day -> Integer julian = flip diffDays (fromGregorian 1858 11 17) @@ -90,3 +105,10 @@ yearday :: Day -> Integer yearday day = let (y,m,d) = toGregorian day dayofyear = monthAndDayToDayOfYear (isLeapYear y) m d in toInteger dayofyear + +{- + - Parsing BoolExpr + -} + +parseBoolExpr :: String -> Either ParseError BoolExpr +parseBoolExpr = undefined