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
- `app` - contains the compiler program
- `src` - contains the compiler program
- `example` - contains example programs that can be compiled
## Credits

View File

@ -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 ";"

View File

@ -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
deriving (P.Show)