Add solving expressions
parent
7f28dc939f
commit
af50529dc1
|
@ -27,3 +27,4 @@ Main repo: https://git.sudoer.ch/me/really-bad-compiler-in-haskell
|
|||
- IDE: VSCodium
|
||||
- Git platform: Forgejo
|
||||
- AI: Phind
|
||||
- Search: Kagi, Stack Overflow
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
module Eval.Expression (evalExpr) where
|
||||
|
||||
import Objects.Expression
|
||||
|
||||
eval :: Expr -> Int
|
||||
eval (Lit x) = x
|
||||
eval (Add x y) = eval x + eval y
|
||||
eval (Sub x y) = eval x - eval y
|
||||
eval (Mul x y) = eval x * eval y
|
||||
eval (Div x y) = eval x `div` eval y
|
||||
|
||||
evalExpr :: Expr -> Int
|
||||
evalExpr = eval
|
19
app/Main.hs
19
app/Main.hs
|
@ -1,10 +1,21 @@
|
|||
module Main (main) where
|
||||
|
||||
import Parser.Equation
|
||||
import Data.Either
|
||||
import Eval.Expression
|
||||
import Objects.Expression
|
||||
import Parser.Expression
|
||||
import System.Environment
|
||||
import Text.Megaparsec
|
||||
|
||||
-- main :: IO ()
|
||||
parseResult :: ParseResult
|
||||
parseResult = parseExpr "2+3*4"
|
||||
|
||||
getRight :: ParseResult -> Expr
|
||||
getRight (Right r) = r
|
||||
|
||||
expr = getRight parseResult
|
||||
|
||||
result = evalExpr expr
|
||||
|
||||
main = do
|
||||
input <- fmap head getArgs
|
||||
parseTest equationParser input
|
||||
print result
|
|
@ -0,0 +1,19 @@
|
|||
module Objects.Expression
|
||||
( Expr
|
||||
( Lit,
|
||||
Add,
|
||||
Sub,
|
||||
Mul,
|
||||
Div
|
||||
),
|
||||
-- solve,
|
||||
)
|
||||
where
|
||||
|
||||
data Expr
|
||||
= Lit Int
|
||||
| Add Expr Expr
|
||||
| Sub Expr Expr
|
||||
| Mul Expr Expr
|
||||
| Div Expr Expr
|
||||
deriving (Show)
|
|
@ -1,21 +1,14 @@
|
|||
module Parser.Equation (equationParser) where
|
||||
module Parser.Expression (parseExpr, ParseResult) where
|
||||
|
||||
import Control.Monad.Combinators.Expr
|
||||
import Data.Void (Void)
|
||||
import Text.Megaparsec
|
||||
import Objects.Expression
|
||||
import Text.Megaparsec as MP
|
||||
import Text.Megaparsec.Char as C
|
||||
import Text.Megaparsec.Char.Lexer as L
|
||||
|
||||
type Parser = Parsec Void String
|
||||
|
||||
data Expr
|
||||
= Int Int
|
||||
| Add Expr Expr
|
||||
| Sub Expr Expr
|
||||
| Mul Expr Expr
|
||||
| Div Expr Expr
|
||||
deriving (Show)
|
||||
|
||||
lexemeParser :: Parser a -> Parser a
|
||||
lexemeParser = L.lexeme C.space
|
||||
|
||||
|
@ -26,7 +19,7 @@ intParser :: Parser Int
|
|||
intParser = lexemeParser L.decimal
|
||||
|
||||
term :: Parser Expr
|
||||
term = Int <$> intParser
|
||||
term = Lit <$> intParser
|
||||
|
||||
table :: [[Operator Parser Expr]]
|
||||
table =
|
||||
|
@ -43,7 +36,9 @@ binaryOp name f = InfixL (f <$ symbolParser name)
|
|||
expr :: Parser Expr
|
||||
expr = makeExprParser term table
|
||||
|
||||
parseExpr :: String -> Either (ParseErrorBundle String Void) Expr
|
||||
parseExpr = parse (C.space *> expr <* eof) ""
|
||||
type ParseResult = Either (ParseErrorBundle String Void) Expr
|
||||
|
||||
equationParser = expr
|
||||
parseExpr :: String -> ParseResult
|
||||
parseExpr = MP.parse (C.space *> expr <* eof) ""
|
||||
|
||||
-- parseE = parseExpr
|
Reference in New Issue