Skip to content

Commit 8e2c175

Browse files
committed
v1.1.1 :: Format and style
1 parent 9ea430e commit 8e2c175

7 files changed

Lines changed: 67 additions & 77 deletions

File tree

app/Main.hs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,31 @@ import Control.DeepSeq (NFData, force)
44
import Control.Exception (SomeException (..), catch, evaluate)
55
import Control.Monad (forever, unless)
66
import Lapse (runExpression', runExpressionIO)
7+
import Lapse.Modules (fileExists)
78
import System.Environment (getArgs)
89
import System.IO (
9-
IOMode (ReadMode),
10-
hClose,
1110
hFlush,
12-
openFile,
1311
readFile',
1412
stdout,
1513
)
1614

17-
fileExists :: FilePath -> IO Bool
18-
fileExists path =
19-
do
20-
handle <- openFile path ReadMode
21-
hClose handle
22-
return True
23-
`catch` (\(SomeException _) -> return False)
24-
2515
catchAny :: (NFData (m String)) => m String -> (SomeException -> IO (m String)) -> IO (m String)
2616
catchAny = catch . evaluate . force
2717

2818
repl :: IO ()
29-
repl = forever $ do
30-
putStr "(repl@lapse)>> "
31-
hFlush stdout
32-
expr <- getLine
33-
res <- catchAny (runExpression' expr) (pure . pure . show)
34-
putStrLn $ head res
19+
repl = forever $ read' >>= eval >>= print'
20+
where
21+
read' = do
22+
putStr "(repl@lapse)>> "
23+
hFlush stdout
24+
getLine
25+
eval expr = catchAny (runExpression' expr) (pure . pure . show)
26+
print' = putStrLn . head
3527

3628
executeFile :: String -> IO ()
3729
executeFile s = do
3830
exists <- fileExists s
39-
unless exists (error $ "No such file: " ++ s)
31+
unless exists $ error $ "No such file: " ++ s
4032
expr <- readFile' s
4133
_ <- runExpressionIO expr
4234
pure ()

lapse.cabal

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 3.0
22
name: lapse
3-
version: 1.1.0
3+
version: 1.1.1
44
license: GPL-3.0-only
55
license-file: LICENSE
66
author: ProggerX
@@ -24,30 +24,28 @@ library
2424
hs-source-dirs:
2525
src
2626
build-depends:
27-
base ^>=4.18.2.1
27+
base >=4.18.2.1 && <4.20
2828
, containers
2929
, mtl
30-
, split
3130
default-language: GHC2021
3231

3332
test-suite test
3433
type: exitcode-stdio-1.0
3534
hs-source-dirs: test
3635
main-is: Main.hs
3736
build-depends:
38-
base ^>=4.18.2.1
37+
base >=4.18.2.1 && <4.20
38+
, lapse
3939
, tasty
4040
, tasty-hunit
41-
, lapse
4241
default-language: GHC2021
4342

4443
executable lapse
4544
import: warnings
4645
main-is: Main.hs
4746
build-depends:
48-
base ^>=4.18.2.1
49-
, lapse
50-
, mtl
47+
base >=4.18.2.1 && <4.20
5148
, deepseq
49+
, lapse
5250
hs-source-dirs: app
5351
default-language: GHC2021

src/Lapse.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ runExpression :: (Monad m) => String -> m [Value m]
2626
runExpression = evalLapseM . mapM eval . parse
2727

2828
runExpression' :: (Monad m) => String -> m String
29-
runExpression' = (pure . show) <=< runExpression
29+
runExpression' = pure . show <=< runExpression
3030

3131
evalLapseMIO :: LapseM IO a -> IO a
3232
evalLapseMIO = (`evalStateT` 0) . (`evalStateT` initIOState)

src/Lapse/Modules.hs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Lapse.Modules where
22

3-
import Control.Exception (SomeException (..), catch)
3+
import Control.Exception (onException)
44
import Control.Monad.IO.Class (liftIO)
55
import Control.Monad.State (evalStateT, runStateT)
66
import Data.Map.Strict (Map, empty, fromList, (!?))
@@ -12,9 +12,8 @@ import Lapse.Scopes (addScope, addScopes)
1212
import Lapse.Types (Func, LapseM, Scope, Scopes, Value (..))
1313
import System.IO (
1414
IOMode (ReadMode),
15-
hClose,
16-
openFile,
1715
readFile',
16+
withFile,
1817
)
1918

2019
std :: (Monad m) => Scope m
@@ -69,11 +68,7 @@ builtins =
6968

7069
fileExists :: FilePath -> IO Bool
7170
fileExists path =
72-
do
73-
handle <- openFile path ReadMode
74-
hClose handle
75-
return True
76-
`catch` (\(SomeException _) -> return False)
71+
withFile path ReadMode (\_ -> pure True) `onException` pure False
7772

7873
getScopesIO' :: LapseM IO a -> IO (Scopes IO)
7974
getScopesIO' = (snd <$>) . (`evalStateT` 0) . (`runStateT` initIOState)

src/Lapse/Parser.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,5 @@ unList Nil = []
102102
unList (Pair h t) = h : unList t
103103
unList _ = error "Parse error in unList"
104104

105-
parse :: (Monad m) => String -> [Value m]
105+
parse :: String -> [Value m]
106106
parse = unList . parse' [Nil] . tokenize

src/Lapse/Scopes.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1+
{-# LANGUAGE BlockArguments #-}
12
{-# LANGUAGE LambdaCase #-}
23

34
module Lapse.Scopes where
45

5-
import Control.Monad.State (get, gets, put)
6+
import Control.Monad.State (gets, modify)
67
import Data.Map.Strict ((!?))
78
import Data.Map.Strict qualified as Map
89
import Lapse.Types (LapseM, Scope, Scopes, Value (..))
910

1011
newScope :: (Monad m) => LapseM m ()
11-
newScope = get >>= put . (Map.empty :)
12+
newScope = modify (Map.empty :)
1213

1314
addScope :: (Monad m) => Scope m -> LapseM m ()
14-
addScope = (get >>=) . (put .) . (:)
15+
addScope = modify . (:)
1516

1617
addScopes :: (Monad m) => Scopes m -> LapseM m ()
17-
addScopes = (get >>=) . (put .) . (++) . foldr (:) []
18+
addScopes = modify . (++)
1819

1920
dropScope :: (Monad m) => LapseM m ()
20-
dropScope = get >>= put . tail
21+
dropScope = modify $ drop 1
2122

2223
changeValue :: (Monad m) => String -> Value m -> LapseM m ()
2324
changeValue k v =
24-
get >>= \case
25-
(s : ss) -> put (Map.insert k v s : ss)
25+
modify \case
26+
(s : ss) -> Map.insert k v s : ss
2627
_ -> undefined
2728

2829
getValue' :: String -> Scopes m -> Value m

test/Main.hs

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Test.Tasty (defaultMain, testGroup)
22
import Test.Tasty.HUnit (testCase, (@?=))
33

4-
import Data.Functor.Identity (Identity)
4+
import Data.Functor.Identity (Identity, runIdentity)
55
import Lapse (evalLapseM, list, numList, runExpression')
66
import Lapse.Operators
77
import Lapse.Types (Func, LapseM, Value (..))
@@ -18,57 +18,61 @@ showTests =
1818
, (Pair (Number 5) Nil, "(5)")
1919
]
2020

21-
opTests :: [(LapseM Identity PValue, Identity PValue)]
21+
opTests :: [(LapseM Identity PValue, PValue)]
2222
opTests =
23-
[ (ladd Nil, pure $ Number 0)
24-
, (lmul Nil, pure $ Number 1)
25-
, (ladd $ numList [1, 2, 3], pure $ Number 6)
26-
, (lsub $ numList [5, 3], pure $ Number 2)
27-
, (lmul $ numList [1, 2, 3, 4], pure $ Number 24)
28-
, (ldiv $ numList [55, 11], pure $ Number 5)
29-
, (lgrt $ numList [7, 2], pure $ Number 1)
30-
, (lgrt $ numList [2, 7], pure Nil)
31-
, (lgrt $ numList [5, 5], pure Nil)
32-
, (llss $ numList [7, 2], pure Nil)
33-
, (llss $ numList [2, 7], pure $ Number 1)
34-
, (llss $ numList [5, 5], pure Nil)
35-
, (leql $ numList [7, 2], pure Nil)
36-
, (leql $ numList [2, 7], pure Nil)
37-
, (leql $ numList [5, 5], pure $ Number 1)
23+
[ (ladd Nil, Number 0)
24+
, (lmul Nil, Number 1)
25+
, (ladd $ numList [1, 2, 3], Number 6)
26+
, (lsub $ numList [5, 3], Number 2)
27+
, (lmul $ numList [1, 2, 3, 4], Number 24)
28+
, (ldiv $ numList [55, 11], Number 5)
29+
, (lgrt $ numList [7, 2], Number 1)
30+
, (lgrt $ numList [2, 7], Nil)
31+
, (lgrt $ numList [5, 5], Nil)
32+
, (llss $ numList [7, 2], Nil)
33+
, (llss $ numList [2, 7], Number 1)
34+
, (llss $ numList [5, 5], Nil)
35+
, (leql $ numList [7, 2], Nil)
36+
, (leql $ numList [2, 7], Nil)
37+
, (leql $ numList [5, 5], Number 1)
3838
]
3939

40-
condTests :: [(PValue, Identity PValue)]
40+
condTests :: [(PValue, PValue)]
4141
condTests =
42-
[ (Nil, pure Nil)
43-
, (list [list [Number 1, Number 2], list [Number 2, Number 3], list [Number 3, Number 4]], pure $ Number 2)
44-
, (list [list [Nil, Number 2], list [Number 2, Number 3], list [Number 3, Number 4]], pure $ Number 3)
45-
, (list [list [Nil, Number 2], list [Nil, Number 3], list [Number 3, Number 4]], pure $ Number 4)
46-
, (list [list [Nil, Number 2], list [Nil, Number 3], list [Nil, Number 4]], pure Nil)
42+
[ (Nil, Nil)
43+
, (list [list [Number 1, Number 2], list [Number 2, Number 3], list [Number 3, Number 4]], Number 2)
44+
, (list [list [Nil, Number 2], list [Number 2, Number 3], list [Number 3, Number 4]], Number 3)
45+
, (list [list [Nil, Number 2], list [Nil, Number 3], list [Number 3, Number 4]], Number 4)
46+
, (list [list [Nil, Number 2], list [Nil, Number 3], list [Nil, Number 4]], Nil)
4747
]
4848

49-
exprTests :: [(String, Identity String)]
49+
exprTests :: [(String, String)]
5050
exprTests =
51-
[ ("(+ 1 2)", pure "[3]")
52-
, ("(let ((a 1)) a)", pure "[1]")
53-
, ("(let ((a 1) (b 2) (c 3)) '(,a ,b ,c ,(+ a b c)))", pure "[(1 2 3 6)]")
54-
, ("(let ((a \"stra\") (b \"bstr\")) (concat a b))", pure "[\"strabstr\"]")
51+
[ ("(+ 1 2)", "[3]")
52+
, ("(let ((a 1)) a)", "[1]")
53+
, ("(let ((a 1) (b 2) (c 3)) '(,a ,b ,c ,(+ a b c)))", "[(1 2 3 6)]")
54+
, ("(let ((a \"stra\") (b \"bstr\")) (concat a b))", "[\"strabstr\"]")
5555
]
5656

5757
main :: IO ()
5858
main =
5959
defaultMain $
6060
testGroup
6161
"all"
62-
[ testGroup
63-
"show"
64-
$ map (\(t, x) -> testCase "test" $ show t @?= x) showTests
62+
[ testGroup "show" [testCase x $ show t @?= x | (t, x) <- showTests]
6563
, testGroup
6664
"operators"
67-
$ map (\(t, x) -> testCase "test" $ evalLapseM t @?= x) opTests
65+
[ testCase (show x) $ runIdentity (evalLapseM t) @?= x
66+
| (t, x) <- opTests
67+
]
6868
, testGroup
6969
"cond"
70-
$ map (\(t, x) -> testCase "test" $ (evalLapseM . cond) t @?= x) condTests
70+
[ testCase (show x) $ (runIdentity . evalLapseM . cond) t @?= x
71+
| (t, x) <- condTests
72+
]
7173
, testGroup
7274
"expression tests"
73-
$ map (\(t, x) -> testCase "test" $ runExpression' t @?= x) exprTests
75+
[ testCase x $ runIdentity (runExpression' t) @?= x
76+
| (t, x) <- exprTests
77+
]
7478
]

0 commit comments

Comments
 (0)