Fix EQ and NE not parsing

pull/29/head
Ethan Reece 2023-10-14 21:23:37 -05:00
parent f436c6bc71
commit 20c20b58b7
Signed by: me
GPG Key ID: D3993665FF92E1C3
4 changed files with 27 additions and 21 deletions

View File

@ -2,4 +2,7 @@ printInt(5*(3-2)+-4-4);
printBool(true);
printBool(false);
printBool(5 * 3 >= 5 + 9);
printBool(5*(3-2)+-4-4 < -3);
printBool(5*(3-2)+-4-4 < -3);
printBool(5 == 5);
printBool(5 == 6);
printBool(5 != 5);

View File

@ -16,7 +16,7 @@ import Data.String.Conversions
import Data.Text
import LLVM (moduleLLVMAssembly, withModuleFromAST)
import LLVM.AST hiding (function)
import LLVM.AST.IntegerPredicate (IntegerPredicate (SGE, SGT, SLE, SLT))
import LLVM.AST.IntegerPredicate
import LLVM.AST.Type
import LLVM.AST.Type qualified as AST
import LLVM.Context
@ -147,6 +147,14 @@ boolExprToLLVM (T.IntOrdCmp T.LTE a b) = mdo
lhs <- intExprToLLVM a
rhs <- intExprToLLVM b
icmp SLE lhs rhs
boolExprToLLVM (T.IntEq T.EQ a b) = mdo
lhs <- intExprToLLVM a
rhs <- intExprToLLVM b
icmp LLVM.AST.IntegerPredicate.EQ lhs rhs
boolExprToLLVM (T.IntEq T.NE a b) = mdo
lhs <- intExprToLLVM a
rhs <- intExprToLLVM b
icmp LLVM.AST.IntegerPredicate.NE lhs rhs
llvmGen :: [T.Statement] -> IO ByteString
llvmGen expr = do

View File

@ -62,40 +62,35 @@ intOrdCmpExpr = do
[ M.GTE <$ string ">=" <* C.space,
M.LTE <$ string "<=" <* C.space,
M.GT <$ string ">" <* C.space,
-- M.Eq <$ string "==",
-- M.Neq <$ string "!=",
M.LT <$ string "<" <* C.space
]
c <- intExpr
return (a, b, c)
intEqExpr :: ParsecT Void Text Data.Functor.Identity.Identity (M.EqOp, M.Int, M.Int)
intEqExpr = do
b <- intExpr
a <-
choice
[ M.EQ <$ string "==" <* C.space,
M.NE <$ string "!=" <* C.space
]
c <- intExpr
return (a, b, c)
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,
[ try (uncurry3 M.IntOrdCmp <$> intOrdCmpExpr),
uncurry3 M.IntEq <$> intEqExpr,
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),
-- binaryOp ">" (M.IntOrdCmp M.GT),
-- binaryOp ">=" (M.IntOrdCmp M.GTE)
-- ],
-- [ binaryOp "==" (M.IntOrdCmp M.Eq),
-- binaryOp "!=" (M.IntOrdCmp M.Neq)
-- ]
-- ]
-- boolExpr :: Parser M.Bool
-- boolExpr = makeExprParser boolExprTerm boolExprTable
binaryOp name f = InfixL $ f <$ symbol name
statement :: Parser M.Statement

View File

@ -13,7 +13,7 @@ import qualified Prelude as P
data ArithOp = Add | Sub | Mul | Div deriving (P.Show)
data EqOp = Eq | Neq deriving (P.Show)
data EqOp = EQ | NE deriving (P.Show)
data OrdCmpOp = GT | GTE | LT | LTE deriving (P.Show)