Create very basic equation parser

main
Ethan Reece 2023-09-07 22:57:12 -05:00
parent 5655c91ea7
commit ff7fe0a1d3
Signed by: me
GPG Key ID: D3993665FF92E1C3
3 changed files with 64 additions and 7 deletions

View File

@ -6,17 +6,24 @@ Main repo: https://git.sudoer.ch/me/really-bad-compiler-in-haskell
## Build Instructions
Clone the repo. Then, use `ghcup tui` to install all of the Haskell tools.
- Clone the repo.
- Use `ghcup` to install all of the Haskell tools.
- Use `stack build` and `stack run <file>` to run the program.
## Credits
### Learning Resources
### Learning Resources Used
- Introduction to Compiler Design class at The University of Texas at Dallas, taught by Charles Averill
- learnyouahaskell.com (for learning Haskell basics)
- https://akashagrawal.me/2017/01/19/beginners-guide-to-megaparsec.html
- https://markkarpov.com/tutorial/megaparsec.html (for help writing a Haskell equation parser)
### Tools
- Language: Haskell
- Language-specific tools: GHCup, Stack, Cabal
- Haskell tools: GHCup, Stack, Cabal
- Libraries: megaparsec, parser-combinators, text
- IDE: VSCodium
- Git tool: Forgejo
- Git platform: Forgejo
- AI: Phind

View File

@ -1,4 +1,51 @@
module Main where
module Main (main) where
import Control.Monad.Combinators.Expr
import Data.Void (Void)
import Text.Megaparsec
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
symbolParser :: String -> Parser String
symbolParser = L.symbol C.space
intParser :: Parser Int
intParser = lexemeParser L.decimal
term :: Parser Expr
term = Int <$> intParser
table :: [[Operator Parser Expr]]
table =
[ [ binaryOp "*" Mul,
binaryOp "/" Div
],
[ binaryOp "+" Add,
binaryOp "-" Sub
]
]
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) ""
main :: IO ()
main = putStrLn "Hello, Haskell!"
main = do
print $ parseExpr "2+3* 4"

View File

@ -31,6 +31,9 @@ executable really-bad-compiler-in-haskell
-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
build-depends: base ^>=4.16.4.0,megaparsec
build-depends: base ^>=4.16.4.0
, megaparsec >= 9.2.2
, parser-combinators
, text
hs-source-dirs: app
default-language: Haskell2010