parsing simple expressions
This commit is contained in:
parent
81feb1aee3
commit
763cae02b7
@ -77,7 +77,7 @@ test-suite haskell-exps-test
|
|||||||
main-is: Main.hs
|
main-is: Main.hs
|
||||||
|
|
||||||
-- Test dependencies.
|
-- Test dependencies.
|
||||||
build-depends: base, hspec, langfeatures
|
build-depends: base, hspec, langfeatures, megaparsec
|
||||||
other-modules: Test.OlogsSpec,
|
other-modules: Test.OlogsSpec,
|
||||||
Test.SimpleParserSpec
|
Test.SimpleParserSpec
|
||||||
|
|
||||||
|
|||||||
@ -16,23 +16,24 @@
|
|||||||
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
|
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
|
||||||
|
|
||||||
module SimpleParser
|
module SimpleParser
|
||||||
( eval, Expr (..), BinaryOp (..), UnaryOp (..) ) -- literalParser
|
( eval, Expr (..), BinaryOp (..), UnaryOp (..), parseExpr ) -- literalParser
|
||||||
where
|
where
|
||||||
import Text.Megaparsec
|
import Text.Megaparsec
|
||||||
import Data.Void (Void)
|
import Data.Void (Void)
|
||||||
import Text.Megaparsec.Byte.Lexer (decimal)
|
import Text.Megaparsec.Char.Lexer (decimal)
|
||||||
import Control.Concurrent (yield)
|
import Text.Megaparsec.Char (char)
|
||||||
|
|
||||||
type Parser = Parsec Void String
|
type Parser = Parsec Void String
|
||||||
|
|
||||||
data BinaryOp = Add | Subtract | Multiply | Divide
|
data BinaryOp = Add | Subtract | Multiply | Divide deriving (Show, Eq)
|
||||||
|
|
||||||
data UnaryOp = Negate
|
data UnaryOp = Negate deriving (Show, Eq)
|
||||||
|
|
||||||
data Expr =
|
data Expr =
|
||||||
BinaryExpr BinaryOp Expr Expr |
|
BinaryExpr BinaryOp Expr Expr |
|
||||||
UnaryExpr UnaryOp Expr |
|
UnaryExpr UnaryOp Expr |
|
||||||
Literal Int
|
Literal Int
|
||||||
|
deriving (Show, Eq)
|
||||||
|
|
||||||
lookUpBinaryOp :: BinaryOp -> Int -> Int -> Int
|
lookUpBinaryOp :: BinaryOp -> Int -> Int -> Int
|
||||||
lookUpBinaryOp Add = (+)
|
lookUpBinaryOp Add = (+)
|
||||||
@ -48,22 +49,29 @@ eval (Literal n) = n
|
|||||||
eval (BinaryExpr binOp a b) = lookUpBinaryOp binOp (eval a) (eval b)
|
eval (BinaryExpr binOp a b) = lookUpBinaryOp binOp (eval a) (eval b)
|
||||||
eval (UnaryExpr unaryOp a) = lookUpUnaryOp unaryOp (eval a)
|
eval (UnaryExpr unaryOp a) = lookUpUnaryOp unaryOp (eval a)
|
||||||
|
|
||||||
|
parseExpr :: Parser Expr
|
||||||
-- parseExpr :: Parser Expr
|
parseExpr = choice [
|
||||||
-- parseExpr = choice [
|
try parseBinaryExpr,
|
||||||
-- literalParser,
|
parseLiteral
|
||||||
-- expressionParser
|
-- bracketedExpParser,
|
||||||
-- -- bracketedExpParser,
|
-- negatedExp
|
||||||
-- -- negatedExp
|
]
|
||||||
-- ]
|
|
||||||
|
|
||||||
-- literalParser :: Parser Expr
|
parseLiteral :: Parser Expr
|
||||||
-- literalParser = Literal <$> decimal
|
parseLiteral = Literal <$> decimal
|
||||||
|
|
||||||
-- expressionParser :: Parser Expr
|
parseBinaryExpr :: Parser Expr
|
||||||
-- expressionParser = do
|
parseBinaryExpr = do
|
||||||
-- parseExpr
|
lhs <- parseLiteral <|> parseExpr
|
||||||
-- parseOp
|
binOp <- parseOp
|
||||||
-- parseExpr
|
rhs <- parseLiteral <|> parseExpr
|
||||||
|
pure (BinaryExpr binOp lhs rhs)
|
||||||
|
|
||||||
|
parseOp :: Parser BinaryOp
|
||||||
|
parseOp = choice [
|
||||||
|
char '+' >> pure Add,
|
||||||
|
char '-' >> pure Subtract,
|
||||||
|
char '/' >> pure Divide,
|
||||||
|
char '*' >> pure Multiply
|
||||||
|
]
|
||||||
|
|
||||||
-- parseOp :: Parser Expr
|
|
||||||
@ -11,7 +11,7 @@
|
|||||||
module Test.SimpleParserSpec where
|
module Test.SimpleParserSpec where
|
||||||
|
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
|
import Text.Megaparsec
|
||||||
import SimpleParser
|
import SimpleParser
|
||||||
|
|
||||||
spec :: Spec
|
spec :: Spec
|
||||||
@ -28,6 +28,8 @@ spec = do
|
|||||||
(UnaryExpr Negate (Literal 1))
|
(UnaryExpr Negate (Literal 1))
|
||||||
(BinaryExpr Divide (Literal 7) (Literal 3))
|
(BinaryExpr Divide (Literal 7) (Literal 3))
|
||||||
) `shouldBe` 1
|
) `shouldBe` 1
|
||||||
|
it "can parse basic expressions" $ do
|
||||||
|
parse parseExpr "" "2" `shouldBe` Right (Literal 2)
|
||||||
|
parse parseExpr "" "2+3" `shouldBe` Right (BinaryExpr Add (Literal 2) (Literal 3))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
29
untitled/.gitignore
vendored
29
untitled/.gitignore
vendored
@ -1,29 +0,0 @@
|
|||||||
### IntelliJ IDEA ###
|
|
||||||
out/
|
|
||||||
!**/src/main/**/out/
|
|
||||||
!**/src/test/**/out/
|
|
||||||
|
|
||||||
### Eclipse ###
|
|
||||||
.apt_generated
|
|
||||||
.classpath
|
|
||||||
.factorypath
|
|
||||||
.project
|
|
||||||
.settings
|
|
||||||
.springBeans
|
|
||||||
.sts4-cache
|
|
||||||
bin/
|
|
||||||
!**/src/main/**/bin/
|
|
||||||
!**/src/test/**/bin/
|
|
||||||
|
|
||||||
### NetBeans ###
|
|
||||||
/nbproject/private/
|
|
||||||
/nbbuild/
|
|
||||||
/dist/
|
|
||||||
/nbdist/
|
|
||||||
/.nb-gradle/
|
|
||||||
|
|
||||||
### VS Code ###
|
|
||||||
.vscode/
|
|
||||||
|
|
||||||
### Mac OS ###
|
|
||||||
.DS_Store
|
|
||||||
Loading…
x
Reference in New Issue
Block a user