Allow multiple print statements

pull/25/head
Ethan Reece 2023-10-07 00:50:33 -05:00
parent bdbe823bc3
commit e145e91074
Signed by: me
GPG Key ID: D3993665FF92E1C3
5 changed files with 31 additions and 10 deletions

View File

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

View File

@ -1 +1,2 @@
(6+8/3) print(6+8/3);
print(5000);

View File

@ -56,7 +56,7 @@ getString str = do
modify $ \env -> env {strings = M.insert str operand (strings env)} modify $ \env -> env {strings = M.insert str operand (strings env)}
return operand return operand
getLLVM :: Expr -> Module getLLVM :: [Expr] -> Module
getLLVM expr = getLLVM expr =
flip evalState (Env {operands = M.empty, strings = M.empty}) $ flip evalState (Env {operands = M.empty, strings = M.empty}) $
buildModuleT "program" $ mdo buildModuleT "program" $ mdo
@ -66,7 +66,7 @@ getLLVM expr =
function "main" [] i32 $ \_ -> mdo function "main" [] i32 $ \_ -> mdo
printNumStr <- globalStringPtr "%d\n" (mkName "str") printNumStr <- globalStringPtr "%d\n" (mkName "str")
lift $ registerString "%d\n" $ ConstantOperand printNumStr lift $ registerString "%d\n" $ ConstantOperand printNumStr
_ <- exprToLLVM expr _ <- forM_ expr exprToLLVM
ret $ int32 0 ret $ int32 0
-- --
@ -109,7 +109,7 @@ exprToLLVM (Expr.Div a b) = mdo
primToLLVM :: Int -> Operand primToLLVM :: Int -> Operand
primToLLVM i = int32 $ fromIntegral i primToLLVM i = int32 $ fromIntegral i
llvmGen :: Expr -> IO ByteString llvmGen :: [Expr] -> IO ByteString
llvmGen expr = do llvmGen expr = do
let l = getLLVM expr let l = getLLVM expr
withContext $ \c -> withModuleFromAST c l moduleLLVMAssembly withContext $ \c -> withModuleFromAST c l moduleLLVMAssembly

View File

@ -64,10 +64,13 @@ methodOp name f = Prefix $ f <$ (string name <* C.space)
expr :: Parser Expr expr :: Parser Expr
expr = makeExprParser term table expr = makeExprParser term table
parseExpr :: Text -> Either (ParseErrorBundle Text Void) Expr statement :: Parser Expr
parseExpr = MP.parse (C.space *> expr <* eof) "" 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 = parse t =
case parseExpr t of case parseExpr t of
Right r -> r Right r -> r

View File

@ -1,4 +1,10 @@
module Main.Type (Expr (..)) where module Main.Type
( Expr (..),
-- AST (..)
)
where
import Data.Graph (Tree (Node))
data Expr data Expr
= Lit Int = Lit Int
@ -10,4 +16,15 @@ data Expr
| Print Expr | Print Expr
deriving deriving
( Show ( Show
) )
-- data AST = AST Node
-- data Node
-- = Reg
-- { cur :: Expr,
-- next :: Node
-- }
-- | End
-- { cur :: Expr
-- }