revamped expressions

This commit is contained in:
Felix Dilke 2026-01-14 15:28:19 +00:00
parent 6dda64b0cf
commit 81feb1aee3
3 changed files with 77 additions and 17 deletions

View File

@ -16,26 +16,54 @@
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
module SimpleParser
( eval, Expr (..) )
( eval, Expr (..), BinaryOp (..), UnaryOp (..) ) -- literalParser
where
import Text.Megaparsec
import Data.Void (Void)
import Text.Megaparsec.Byte.Lexer (decimal)
import Control.Concurrent (yield)
type Parser = Parsec Void String
data BinaryOp = Add | Subtract | Multiply | Divide
data UnaryOp = Negate
data Expr =
Add Expr Expr |
Subtract Expr Expr |
Multiply Expr Expr |
Divide Expr Expr |
Negate Expr |
BinaryExpr BinaryOp Expr Expr |
UnaryExpr UnaryOp Expr |
Literal Int
lookUpBinaryOp :: BinaryOp -> Int -> Int -> Int
lookUpBinaryOp Add = (+)
lookUpBinaryOp Subtract = (-)
lookUpBinaryOp Multiply = (*)
lookUpBinaryOp Divide = div
lookUpUnaryOp :: UnaryOp -> Int -> Int
lookUpUnaryOp Negate x = -x
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)
eval (BinaryExpr binOp a b) = lookUpBinaryOp binOp (eval a) (eval b)
eval (UnaryExpr unaryOp a) = lookUpUnaryOp unaryOp (eval a)
-- parseExpr :: Parser Expr
-- parseExpr = choice [
-- literalParser,
-- expressionParser
-- -- bracketedExpParser,
-- -- negatedExp
-- ]
-- literalParser :: Parser Expr
-- literalParser = Literal <$> decimal
-- expressionParser :: Parser Expr
-- expressionParser = do
-- parseExpr
-- parseOp
-- parseExpr
-- parseOp :: Parser Expr

View File

@ -18,13 +18,16 @@ 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
eval (Negate (Literal 7) ) `shouldBe` -7
eval (BinaryExpr Add (Literal 2) (Literal 3) ) `shouldBe` 5
eval (BinaryExpr Subtract (Literal 2) (Literal 3) ) `shouldBe` -1
eval (BinaryExpr Multiply (Literal 2) (Literal 3) ) `shouldBe` 6
eval (BinaryExpr Divide (Literal 7) (Literal 3) ) `shouldBe` 2
eval (UnaryExpr Negate (Literal 7) ) `shouldBe` -7
it "more complex arithmetic on literals" $ do
eval (Add (Negate (Literal 1)) (Divide (Literal 7) (Literal 3))) `shouldBe` 1
eval (BinaryExpr Add
(UnaryExpr Negate (Literal 1))
(BinaryExpr Divide (Literal 7) (Literal 3))
) `shouldBe` 1

29
untitled/.gitignore vendored Normal file
View File

@ -0,0 +1,29 @@
### 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