diff options
author | dan <[email protected]> | 2024-04-20 13:58:34 -0400 |
---|---|---|
committer | dan <[email protected]> | 2024-04-20 13:58:34 -0400 |
commit | 9331780bedd675ece82aebd1f12ce8b053a618f8 (patch) | |
tree | 0904c84c1207aad92585b4c9c2017db570ce4b50 | |
parent | 77a0492e543af59a2f3ed132e2ce08ac9816d8fc (diff) | |
download | bizexp-9331780bedd675ece82aebd1f12ce8b053a618f8.tar.gz bizexp-9331780bedd675ece82aebd1f12ce8b053a618f8.tar.bz2 bizexp-9331780bedd675ece82aebd1f12ce8b053a618f8.zip |
feat: project also builds a "repl" executable
-rw-r--r-- | app/Repl.hs | 6 | ||||
-rwxr-xr-x | hs.cabal | 21 | ||||
-rwxr-xr-x | src/BizExpr.hs | 9 |
3 files changed, 30 insertions, 6 deletions
diff --git a/app/Repl.hs b/app/Repl.hs new file mode 100644 index 0000000..9f26d78 --- /dev/null +++ b/app/Repl.hs @@ -0,0 +1,6 @@ +module Main where + +import BizExpr (repl) + +main :: IO () +main = repl @@ -24,10 +24,8 @@ build-type: Simple extra-source-files: CHANGELOG.md library - exposed-modules: RestService - - -- Modules included in this library but not exported. - other-modules: BizExpr + exposed-modules: RestService, + BizExpr -- LANGUAGE extensions used by modules in this package. -- other-extensions: @@ -43,6 +41,21 @@ library hs-source-dirs: src default-language: Haskell2010 +executable repl + main-is: Repl.hs + + -- Modules included in this executable, other than Main. + -- other-modules: + + -- LANGUAGE extensions used by modules in this package. + -- other-extensions: + build-depends: + base ^>=4.19.1.0, + hs + + hs-source-dirs: app + default-language: Haskell2010 + executable hs main-is: Main.hs 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) "" = |