Allow multiple print statements
parent
bdbe823bc3
commit
e145e91074
|
@ -1 +1 @@
|
||||||
(5*(3-2)+-4-4)
|
print(5*(3-2)+-4-4);
|
|
@ -1 +1,2 @@
|
||||||
(6+8/3)
|
print(6+8/3);
|
||||||
|
print(5000);
|
|
@ -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
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
-- }
|
Reference in New Issue