basic arithmetic DSL
This commit is contained in:
parent
c9e8feacc9
commit
f71984b647
@ -3,7 +3,9 @@
|
|||||||
<component name="MaterialThemeProjectNewConfig">
|
<component name="MaterialThemeProjectNewConfig">
|
||||||
<option name="metadata">
|
<option name="metadata">
|
||||||
<MTProjectMetadataState>
|
<MTProjectMetadataState>
|
||||||
<option name="userId" value="-70c0f615:19bbbe81457:-7f6e" />
|
<option name="migrated" value="true" />
|
||||||
|
<option name="pristineConfig" value="false" />
|
||||||
|
<option name="userId" value="-4395108f:199cee938a2:-7ff9" />
|
||||||
</MTProjectMetadataState>
|
</MTProjectMetadataState>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
|
|||||||
@ -78,13 +78,13 @@ test-suite haskell-exps-test
|
|||||||
|
|
||||||
-- Test dependencies.
|
-- Test dependencies.
|
||||||
build-depends: base, hspec, langfeatures
|
build-depends: base, hspec, langfeatures
|
||||||
other-modules: Test.OlogsSpec
|
other-modules: Test.OlogsSpec,
|
||||||
|
Test.SimpleParserSpec
|
||||||
|
|
||||||
library langfeatures
|
library langfeatures
|
||||||
build-depends: base, containers
|
build-depends: base, containers, megaparsec
|
||||||
hs-source-dirs: src
|
hs-source-dirs: src
|
||||||
exposed-modules: Ologs
|
exposed-modules: Ologs, SimpleParser
|
||||||
ghc-options: -Wall
|
ghc-options: -Wall
|
||||||
|
|
||||||
executable haskell-experiments
|
executable haskell-experiments
|
||||||
|
|||||||
41
haskell-experiments/src/SimpleParser.hs
Normal file
41
haskell-experiments/src/SimpleParser.hs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
{-# LANGUAGE BlockArguments #-}
|
||||||
|
{-# LANGUAGE DuplicateRecordFields #-}
|
||||||
|
{-# LANGUAGE NamedFieldPuns #-}
|
||||||
|
{-# LANGUAGE OverloadedRecordDot #-}
|
||||||
|
{-# HLINT ignore "Unused LANGUAGE pragma" #-}
|
||||||
|
{-# HLINT ignore "Use concatMap" #-}
|
||||||
|
{-# LANGUAGE RecordWildCards #-}
|
||||||
|
{-# LANGUAGE ScopedTypeVariables #-}
|
||||||
|
{-# LANGUAGE TypeApplications #-}
|
||||||
|
{-# HLINT ignore "Move catMaybes" #-}
|
||||||
|
{-# HLINT ignore "Use mapMaybe" #-}
|
||||||
|
{-# HLINT ignore "Use list comprehension" #-}
|
||||||
|
{-# LANGUAGE TypeOperators #-}
|
||||||
|
{-# LANGUAGE NoFieldSelectors #-}
|
||||||
|
{-# LANGUAGE NoMonomorphismRestriction #-}
|
||||||
|
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
|
||||||
|
|
||||||
|
module SimpleParser
|
||||||
|
( eval, Expr (..) )
|
||||||
|
where
|
||||||
|
import Text.Megaparsec
|
||||||
|
import Data.Void (Void)
|
||||||
|
|
||||||
|
type Parser = Parsec Void String
|
||||||
|
|
||||||
|
data Expr =
|
||||||
|
Add Expr Expr |
|
||||||
|
Subtract Expr Expr |
|
||||||
|
Multiply Expr Expr |
|
||||||
|
Divide Expr Expr |
|
||||||
|
Negate Expr |
|
||||||
|
Literal Int
|
||||||
|
|
||||||
|
eval :: Expr -> Int
|
||||||
|
eval (Literal n) = n
|
||||||
|
eval (Add a b) = (eval a) + (eval b)
|
||||||
|
eval (Subtract a b) = (eval a) - (eval b)
|
||||||
|
eval (Multiply a b) = (eval a) * (eval b)
|
||||||
|
eval (Divide a b) = (eval a) `div` (eval b)
|
||||||
|
eval (Negate a) = -(eval a)
|
||||||
|
|
||||||
@ -2,10 +2,12 @@ module Main (main) where
|
|||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
|
|
||||||
import qualified Test.OlogsSpec as Ologs
|
import qualified Test.OlogsSpec as Ologs
|
||||||
|
import qualified Test.SimpleParserSpec as SimpleParserSpec
|
||||||
--import Test.FooSpec
|
--import Test.FooSpec
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = hspec $ do
|
main = hspec $ do
|
||||||
describe "Ologs" Ologs.spec
|
describe "Ologs" Ologs.spec
|
||||||
|
describe "SimpleParser" SimpleParserSpec.spec
|
||||||
-- describe "My amazing tests" [ FastLaneSpec FooSpec ]
|
-- describe "My amazing tests" [ FastLaneSpec FooSpec ]
|
||||||
|
|
||||||
|
|||||||
26
haskell-experiments/test/Test/SimpleParserSpec.hs
Normal file
26
haskell-experiments/test/Test/SimpleParserSpec.hs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
{-# LANGUAGE DuplicateRecordFields #-}
|
||||||
|
{-# LANGUAGE OverloadedRecordDot #-}
|
||||||
|
{-# HLINT ignore "Use const" #-}
|
||||||
|
{-# HLINT ignore "Unused LANGUAGE pragma" #-}
|
||||||
|
{-# HLINT ignore "Avoid lambda" #-}
|
||||||
|
{-# LANGUAGE ScopedTypeVariables #-}
|
||||||
|
{-# LANGUAGE NoFieldSelectors #-}
|
||||||
|
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
|
||||||
|
{-# LANGUAGE TypeApplications #-}
|
||||||
|
|
||||||
|
module Test.SimpleParserSpec where
|
||||||
|
|
||||||
|
import Test.Hspec
|
||||||
|
|
||||||
|
import SimpleParser
|
||||||
|
|
||||||
|
spec :: Spec
|
||||||
|
spec = do
|
||||||
|
describe "evaluate expressions" $ do
|
||||||
|
it "arithmetic on literals" $ do
|
||||||
|
eval (Add (Literal 2) (Literal 3) ) `shouldBe` 5
|
||||||
|
eval (Subtract (Literal 2) (Literal 3) ) `shouldBe` -1
|
||||||
|
eval (Multiply (Literal 2) (Literal 3) ) `shouldBe` 6
|
||||||
|
eval (Divide (Literal 7) (Literal 3) ) `shouldBe` 2
|
||||||
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user