Add boolean logic operators

pull/29/head
Ethan Reece 2023-10-23 12:20:37 -05:00
parent 20c20b58b7
commit b1d2f65992
Signed by: me
GPG Key ID: D3993665FF92E1C3
5 changed files with 79 additions and 13 deletions

View File

@ -6,3 +6,8 @@ printBool(5*(3-2)+-4-4 < -3);
printBool(5 == 5); printBool(5 == 5);
printBool(5 == 6); printBool(5 == 6);
printBool(5 != 5); printBool(5 != 5);
printBool(true == true);
printBool(true && true);
printBool(true && false);
printBool(!true);
printBool(!(5 == 5));

View File

@ -22,11 +22,15 @@ run opts = do
let fileName = filePath opts let fileName = filePath opts
contents <- T.readFile fileName contents <- T.readFile fileName
T.putStrLn "- Generating LLVM to './a.out.ll'..." T.putStrLn "- Generating LLVM to './a.out.ll'..."
result <- (llvmGen . parse) contents let parseResult = parse contents
case parseResult of
Right r -> do
result <- llvmGen r
B.writeFile "a.out.ll" result B.writeFile "a.out.ll" result
T.putStrLn "- Compiling to executable './a.out'..." T.putStrLn "- Compiling to executable './a.out'..."
callCommand "clang a.out.ll" callCommand "clang a.out.ll"
T.putStrLn "- Done." T.putStrLn "- Done."
Left l -> putStrLn l
main :: IO () main :: IO ()
main = execParser opts >>= run main = execParser opts >>= run

View File

@ -155,6 +155,25 @@ boolExprToLLVM (T.IntEq T.NE a b) = mdo
lhs <- intExprToLLVM a lhs <- intExprToLLVM a
rhs <- intExprToLLVM b rhs <- intExprToLLVM b
icmp LLVM.AST.IntegerPredicate.NE lhs rhs icmp LLVM.AST.IntegerPredicate.NE lhs rhs
boolExprToLLVM (T.BoolEq T.EQ a b) = mdo
lhs <- boolExprToLLVM a
rhs <- boolExprToLLVM b
icmp LLVM.AST.IntegerPredicate.EQ lhs rhs
boolExprToLLVM (T.BoolEq T.NE a b) = mdo
lhs <- boolExprToLLVM a
rhs <- boolExprToLLVM b
icmp LLVM.AST.IntegerPredicate.NE lhs rhs
boolExprToLLVM (T.BoolLogic T.AND a b) = mdo
lhs <- boolExprToLLVM a
rhs <- boolExprToLLVM b
LLVM.IRBuilder.Instruction.and lhs rhs
boolExprToLLVM (T.BoolLogic T.OR a b) = mdo
lhs <- boolExprToLLVM a
rhs <- boolExprToLLVM b
LLVM.IRBuilder.Instruction.or lhs rhs
boolExprToLLVM (T.BoolNeg a) = mdo
l <- boolExprToLLVM a
LLVM.IRBuilder.Instruction.xor l $ bit 1
llvmGen :: [T.Statement] -> IO ByteString llvmGen :: [T.Statement] -> IO ByteString
llvmGen expr = do llvmGen expr = do

View File

@ -78,6 +78,31 @@ intEqExpr = do
c <- intExpr c <- intExpr
return (a, b, c) return (a, b, c)
boolExprTable :: [[Operator Parser M.Bool]]
boolExprTable =
[ [ binaryOp "==" (M.BoolEq M.EQ),
binaryOp "!=" (M.BoolEq M.NE)
],
[prefixOp "!" M.BoolNeg],
[binaryOp "&&" (M.BoolLogic M.AND)],
[binaryOp "||" (M.BoolLogic M.OR)]
]
-- boolEqExpr :: ParsecT Void Text Data.Functor.Identity.Identity (M.EqOp, M.Bool, M.Bool)
-- boolEqExpr = do
-- b <-
-- choice
-- [
-- ]
-- 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 :: (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 f (x, y, z) = f x y z uncurry3 f (x, y, z) = f x y z
@ -86,27 +111,35 @@ boolExprTerm =
choice choice
[ try (uncurry3 M.IntOrdCmp <$> intOrdCmpExpr), [ try (uncurry3 M.IntOrdCmp <$> intOrdCmpExpr),
uncurry3 M.IntEq <$> intEqExpr, uncurry3 M.IntEq <$> intEqExpr,
M.Bool True <$ string "true", M.Bool True <$ string "true" <* C.space,
M.Bool False <$ string "false", M.Bool False <$ string "false" <* C.space,
parens boolExprTerm parens boolExpr
] ]
binaryOp name f = InfixL $ f <$ symbol name boolExpr :: ParsecT Void Text Data.Functor.Identity.Identity M.Bool
boolExpr = makeExprParser boolExprTerm boolExprTable
binaryOp :: Text -> (a -> a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a
binaryOp name f = InfixL $ f <$ string name <* C.space
prefixOp :: Text -> (a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a
prefixOp name f = Prefix $ 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 boolExprTerm) string "printBool" *> (M.PrintBool <$> parens boolExpr)
] ]
<* symbol ";" <* symbol ";"
parseStatements :: Text -> Either (ParseErrorBundle Text Void) [M.Statement] parseStatements :: Text -> Either (ParseErrorBundle Text Void) [M.Statement]
parseStatements = MP.parse (C.space *> many statement <* eof) "" parseStatements = MP.parse (C.space *> many statement <* eof) ""
parse :: Text -> [M.Statement] parse :: Text -> Either String [M.Statement]
parse t = parse t =
case parseStatements t of case parseStatements t of
Right r -> r Right r -> Right r
Left e -> Left (errorBundlePretty e)
-- TODO: add error handling -- TODO: add error handling

View File

@ -2,6 +2,7 @@ module Main.Types
( ArithOp (..), ( ArithOp (..),
EqOp (..), EqOp (..),
OrdCmpOp (..), OrdCmpOp (..),
LogicOp (..),
-- BinExpr (..), -- BinExpr (..),
Int (..), Int (..),
Bool (..), Bool (..),
@ -17,6 +18,8 @@ data EqOp = EQ | NE deriving (P.Show)
data OrdCmpOp = GT | GTE | LT | LTE deriving (P.Show) data OrdCmpOp = GT | GTE | LT | LTE deriving (P.Show)
data LogicOp = AND | OR 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
@ -26,9 +29,11 @@ data Int
data Bool data Bool
= Bool P.Bool = Bool P.Bool
| BoolNeg 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)
| BoolLogic LogicOp Bool Bool
deriving (P.Show) deriving (P.Show)
data Statement data Statement