From 6e2031f71508afe96ecfce9830f1c217d85de6bc Mon Sep 17 00:00:00 2001 From: sudoer777 Date: Sat, 14 Oct 2023 18:01:01 -0500 Subject: [PATCH] Parse int comparisons as boolean expression --- README.md | 2 +- src/Main/Parser/Megaparsec.hs | 39 ++++++++++++++++++++++++++--------- src/Main/Types.hs | 11 ++++++---- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 17ef438..170d18f 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ I recommend using VSCodium, which is preconfigured to have syntax highlighting a ## File structure -- `app` - contains the compiler program +- `src` - contains the compiler program - `example` - contains example programs that can be compiled ## Credits diff --git a/src/Main/Parser/Megaparsec.hs b/src/Main/Parser/Megaparsec.hs index dc49b3d..3f2c922 100644 --- a/src/Main/Parser/Megaparsec.hs +++ b/src/Main/Parser/Megaparsec.hs @@ -54,15 +54,34 @@ intExprTable = intExpr :: Parser M.Int intExpr = makeExprParser intExprTerm intExprTable --- boolExprTerm :: ParsecT Void Text Data.Functor.Identity.Identity M.Bool --- boolExprTerm = --- choice --- [ M.Bool True <$ string "true", --- M.Bool False <$ string "false", --- parens boolExpr --- ] +intOrdCmpExpr :: ParsecT Void Text Data.Functor.Identity.Identity (M.OrdCmpOp, M.Int, M.Int) +intOrdCmpExpr = do + b <- intExpr + a <- + choice + [ M.GT <$ symbol ">", + M.GTE <$ symbol ">=", + -- M.Eq <$ string "==", + -- M.Neq <$ string "!=", + M.LTE <$ symbol "<=", + M.LT <$ symbol "<" + ] + c <- intExpr + return (a, b, c) --- boolExprTable :: [[Operator Parser (Either M.Bool M.Int)]] +uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d +uncurry3 f (x, y, z) = f x y z + +boolExprTerm :: ParsecT Void Text Data.Functor.Identity.Identity M.Bool +boolExprTerm = + choice + [ uncurry3 M.IntOrdCmp <$> intOrdCmpExpr, + M.Bool True <$ string "true", + M.Bool False <$ string "false", + parens boolExprTerm + ] + +-- boolExprTable :: [[Operator Parser M.Bool]] -- boolExprTable = -- [ [ binaryOp "<" (M.IntOrdCmp M.LT), -- binaryOp "<=" (M.IntOrdCmp M.LTE), @@ -82,8 +101,8 @@ binaryOp name f = InfixL $ f <$ symbol name statement :: Parser M.Statement statement = choice - [ string "printInt" *> (M.PrintInt <$> parens intExpr) - -- ,string "printBool" *> (M.PrintBool <$> parens boolExpr) + [ string "printInt" *> (M.PrintInt <$> parens intExpr), + string "printBool" *> (M.PrintBool <$> parens boolExprTerm) ] <* symbol ";" diff --git a/src/Main/Types.hs b/src/Main/Types.hs index 182b0db..c9cc000 100644 --- a/src/Main/Types.hs +++ b/src/Main/Types.hs @@ -11,24 +11,27 @@ where import qualified Prelude as P -data ArithOp = Add | Sub | Mul | Div +data ArithOp = Add | Sub | Mul | Div deriving (P.Show) -data EqOp = Eq | Neq +data EqOp = Eq | Neq deriving (P.Show) -data OrdCmpOp = GT | GTE | LT | LTE +data OrdCmpOp = GT | GTE | LT | LTE deriving (P.Show) -- newtype BinExpr op i o = BinExpr (op -> i -> i -> o) data Int = Int P.Int | IntArith ArithOp Int Int -- (BinExpr ArithOp Int Int) + deriving (P.Show) data Bool = Bool P.Bool | IntEq EqOp Int Int -- (BinExpr EqOp Int Bool) | IntOrdCmp OrdCmpOp Int Int -- (BinExpr OrdCmpOp Int Bool) | BoolEq EqOp Bool Bool -- (BinExpr EqOp Bool Bool) + deriving (P.Show) data Statement = PrintInt Int - | PrintBool Bool \ No newline at end of file + | PrintBool Bool + deriving (P.Show) \ No newline at end of file