diff --git a/example/1.hear b/example/1.hear index 265a993..98aa9bf 100644 --- a/example/1.hear +++ b/example/1.hear @@ -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); \ No newline at end of file +printBool(5*(3-2)+-4-4 < -3); +printBool(5 == 5); +printBool(5 == 6); +printBool(5 != 5); \ No newline at end of file diff --git a/src/Main/LLVMGen.hs b/src/Main/LLVMGen.hs index ecb8d65..9a0bbcf 100644 --- a/src/Main/LLVMGen.hs +++ b/src/Main/LLVMGen.hs @@ -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 diff --git a/src/Main/Parser/Megaparsec.hs b/src/Main/Parser/Megaparsec.hs index 3281b77..64f9712 100644 --- a/src/Main/Parser/Megaparsec.hs +++ b/src/Main/Parser/Megaparsec.hs @@ -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 diff --git a/src/Main/Types.hs b/src/Main/Types.hs index c9cc000..aa504a8 100644 --- a/src/Main/Types.hs +++ b/src/Main/Types.hs @@ -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)