From e145e9107463f90c61f130bee477939f89ffeda5 Mon Sep 17 00:00:00 2001 From: sudoer777 Date: Sat, 7 Oct 2023 00:50:33 -0500 Subject: [PATCH] Allow multiple print statements --- example/1.hear | 2 +- example/2.hear | 3 ++- src/Main/LLVMGen.hs | 6 +++--- src/Main/Parser/Megaparsec.hs | 9 ++++++--- src/Main/Type.hs | 21 +++++++++++++++++++-- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/example/1.hear b/example/1.hear index 62c2d80..62b1f81 100644 --- a/example/1.hear +++ b/example/1.hear @@ -1 +1 @@ -(5*(3-2)+-4-4) \ No newline at end of file +print(5*(3-2)+-4-4); \ No newline at end of file diff --git a/example/2.hear b/example/2.hear index 2078094..977a5ab 100644 --- a/example/2.hear +++ b/example/2.hear @@ -1 +1,2 @@ - (6+8/3) \ No newline at end of file + print(6+8/3); + print(5000); \ No newline at end of file diff --git a/src/Main/LLVMGen.hs b/src/Main/LLVMGen.hs index 8394fb7..620e168 100644 --- a/src/Main/LLVMGen.hs +++ b/src/Main/LLVMGen.hs @@ -56,7 +56,7 @@ getString str = do modify $ \env -> env {strings = M.insert str operand (strings env)} return operand -getLLVM :: Expr -> Module +getLLVM :: [Expr] -> Module getLLVM expr = flip evalState (Env {operands = M.empty, strings = M.empty}) $ buildModuleT "program" $ mdo @@ -66,7 +66,7 @@ getLLVM expr = function "main" [] i32 $ \_ -> mdo printNumStr <- globalStringPtr "%d\n" (mkName "str") lift $ registerString "%d\n" $ ConstantOperand printNumStr - _ <- exprToLLVM expr + _ <- forM_ expr exprToLLVM ret $ int32 0 -- @@ -109,7 +109,7 @@ exprToLLVM (Expr.Div a b) = mdo primToLLVM :: Int -> Operand primToLLVM i = int32 $ fromIntegral i -llvmGen :: Expr -> IO ByteString +llvmGen :: [Expr] -> IO ByteString llvmGen expr = do let l = getLLVM expr withContext $ \c -> withModuleFromAST c l moduleLLVMAssembly \ No newline at end of file diff --git a/src/Main/Parser/Megaparsec.hs b/src/Main/Parser/Megaparsec.hs index 9435ca0..aa1ea72 100644 --- a/src/Main/Parser/Megaparsec.hs +++ b/src/Main/Parser/Megaparsec.hs @@ -64,10 +64,13 @@ methodOp name f = Prefix $ f <$ (string name <* C.space) expr :: Parser Expr expr = makeExprParser term table -parseExpr :: Text -> Either (ParseErrorBundle Text Void) Expr -parseExpr = MP.parse (C.space *> expr <* eof) "" +statement :: Parser Expr +statement = expr <* symbol ";" -parse :: Text -> Expr +parseExpr :: Text -> Either (ParseErrorBundle Text Void) [Expr] +parseExpr = MP.parse (C.space *> many statement <* eof) "" + +parse :: Text -> [Expr] parse t = case parseExpr t of Right r -> r diff --git a/src/Main/Type.hs b/src/Main/Type.hs index 1c38360..23178b8 100644 --- a/src/Main/Type.hs +++ b/src/Main/Type.hs @@ -1,4 +1,10 @@ -module Main.Type (Expr (..)) where +module Main.Type + ( Expr (..), + -- AST (..) + ) +where + +import Data.Graph (Tree (Node)) data Expr = Lit Int @@ -10,4 +16,15 @@ data Expr | Print Expr deriving ( Show - ) \ No newline at end of file + ) + +-- data AST = AST Node + +-- data Node +-- = Reg +-- { cur :: Expr, +-- next :: Node +-- } +-- | End +-- { cur :: Expr +-- } \ No newline at end of file