Parse printInt instead of print

pull/29/head
Ethan Reece 2023-10-08 00:23:13 -05:00
parent a7547dd670
commit d64ad9aeda
Signed by: me
GPG Key ID: D3993665FF92E1C3
4 changed files with 33 additions and 5 deletions

View File

@ -1 +1 @@
print(5*(3-2)+-4-4); printInt(5*(3-2)+-4-4);

View File

@ -81,7 +81,7 @@ statementToLLVM ::
) => ) =>
T.Statement -> T.Statement ->
m Operand m Operand
statementToLLVM (T.Print e) = mdo statementToLLVM (T.PrintInt e) = mdo
val <- intExprToLLVM e val <- intExprToLLVM e
printf <- getOperand "printf" printf <- getOperand "printf"
formatStr <- getString "%d\n" formatStr <- getString "%d\n"

View File

@ -54,11 +54,38 @@ intExprTable =
intExpr :: Parser M.Int intExpr :: Parser M.Int
intExpr = makeExprParser intExprTerm intExprTable intExpr = makeExprParser intExprTerm intExprTable
binaryOp :: Text -> (a -> a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a -- boolExprTerm :: ParsecT Void Text Data.Functor.Identity.Identity M.Bool
-- boolExprTerm =
-- choice
-- [ M.Bool True <$ string "true",
-- M.Bool False <$ string "false",
-- parens boolExpr
-- ]
-- boolExprTable :: [[Operator Parser (Either M.Bool M.Int)]]
-- 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 binaryOp name f = InfixL $ f <$ symbol name
statement :: Parser M.Statement statement :: Parser M.Statement
statement = string "print" *> (M.Print <$> parens intExpr) <* symbol ";" statement =
choice
[ string "printInt" *> (M.PrintInt <$> parens intExpr)
-- ,string "printBool" *> (M.PrintBool <$> parens boolExpr)
]
<* 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) ""

View File

@ -30,4 +30,5 @@ data Bool
| BoolEq EqOp Bool Bool -- (BinExpr EqOp Bool Bool) | BoolEq EqOp Bool Bool -- (BinExpr EqOp Bool Bool)
data Statement data Statement
= Print Int = PrintInt Int
| PrintBool Bool