Parse int comparisons as boolean expression

pull/29/head
Ethan Reece 2023-10-14 18:01:01 -05:00
parent d64ad9aeda
commit 6e2031f715
Signed by: me
GPG Key ID: D3993665FF92E1C3
3 changed files with 37 additions and 15 deletions

View File

@ -35,7 +35,7 @@ I recommend using VSCodium, which is preconfigured to have syntax highlighting a
## File structure ## File structure
- `app` - contains the compiler program - `src` - contains the compiler program
- `example` - contains example programs that can be compiled - `example` - contains example programs that can be compiled
## Credits ## Credits

View File

@ -54,15 +54,34 @@ intExprTable =
intExpr :: Parser M.Int intExpr :: Parser M.Int
intExpr = makeExprParser intExprTerm intExprTable intExpr = makeExprParser intExprTerm intExprTable
-- boolExprTerm :: ParsecT Void Text Data.Functor.Identity.Identity M.Bool intOrdCmpExpr :: ParsecT Void Text Data.Functor.Identity.Identity (M.OrdCmpOp, M.Int, M.Int)
-- boolExprTerm = intOrdCmpExpr = do
-- choice b <- intExpr
-- [ M.Bool True <$ string "true", a <-
-- M.Bool False <$ string "false", choice
-- parens boolExpr [ 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 = -- boolExprTable =
-- [ [ binaryOp "<" (M.IntOrdCmp M.LT), -- [ [ binaryOp "<" (M.IntOrdCmp M.LT),
-- binaryOp "<=" (M.IntOrdCmp M.LTE), -- binaryOp "<=" (M.IntOrdCmp M.LTE),
@ -82,8 +101,8 @@ binaryOp name f = InfixL $ f <$ symbol name
statement :: Parser M.Statement statement :: Parser M.Statement
statement = statement =
choice choice
[ string "printInt" *> (M.PrintInt <$> parens intExpr) [ string "printInt" *> (M.PrintInt <$> parens intExpr),
-- ,string "printBool" *> (M.PrintBool <$> parens boolExpr) string "printBool" *> (M.PrintBool <$> parens boolExprTerm)
] ]
<* symbol ";" <* symbol ";"

View File

@ -11,24 +11,27 @@ where
import qualified Prelude as P 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) -- newtype BinExpr op i o = BinExpr (op -> i -> i -> o)
data Int data Int
= Int P.Int = Int P.Int
| IntArith ArithOp Int Int -- (BinExpr ArithOp Int Int) | IntArith ArithOp Int Int -- (BinExpr ArithOp Int Int)
deriving (P.Show)
data Bool data Bool
= Bool P.Bool = Bool P.Bool
| IntEq EqOp Int Int -- (BinExpr EqOp Int Bool) | IntEq EqOp Int Int -- (BinExpr EqOp Int Bool)
| IntOrdCmp OrdCmpOp Int Int -- (BinExpr OrdCmpOp Int Bool) | IntOrdCmp OrdCmpOp Int Int -- (BinExpr OrdCmpOp Int Bool)
| BoolEq EqOp Bool Bool -- (BinExpr EqOp Bool Bool) | BoolEq EqOp Bool Bool -- (BinExpr EqOp Bool Bool)
deriving (P.Show)
data Statement data Statement
= PrintInt Int = PrintInt Int
| PrintBool Bool | PrintBool Bool
deriving (P.Show)