From 9331780bedd675ece82aebd1f12ce8b053a618f8 Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 20 Apr 2024 13:58:34 -0400 Subject: feat: project also builds a "repl" executable --- src/BizExpr.hs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/BizExpr.hs b/src/BizExpr.hs index 1026e47..20a25d1 100755 --- a/src/BizExpr.hs +++ b/src/BizExpr.hs @@ -12,10 +12,13 @@ type Context = M.Map L.Text L.Text --import Data.Text as T repl :: IO () -repl = getLine >>= putStrLn . maybe "Failed to evaluate expression" show . (eval M.empty :: String -> Maybe Integer) >> repl +repl = getLine >>= putStrLn . maybe "Failed to evaluate expression" id . (eval M.empty :: String -> Maybe String) >> repl + +maybeHead (x:_) = Just x +maybeHead _ = Nothing eval :: CoerceTo a => Context -> String -> Maybe a -eval c x = coerceTo =<< evalAst . head . fst =<< parseLevel c x +eval c x = coerceTo =<< evalAst =<< maybeHead . fst =<< parseLevel c x data Value = IntVal Integer | StrVal String | BoolVal Bool | FloatVal Float deriving (Show) @@ -82,6 +85,8 @@ parseLevel c = go "" (Just []) go prev (Just l) (',' : next) = let l0 = [Val $ newVal c prev] in go "" (Just (l ++ l0)) next + go prev l (' ' : next) = + go "" l next go "" (Just l) "" = Just (l, "") go prev (Just l) "" = -- cgit v1.2.3