Parse expressions into different types
parent
2f790d6e55
commit
a7547dd670
|
@ -53,6 +53,7 @@ I recommend using VSCodium, which is preconfigured to have syntax highlighting a
|
||||||
- https://gh.sudoer.ch/danieljharvey/mimsa/blob/trunk/llvm-calc/src/Calc/Compile/ToLLVM.hs (source code for above resource)
|
- https://gh.sudoer.ch/danieljharvey/mimsa/blob/trunk/llvm-calc/src/Calc/Compile/ToLLVM.hs (source code for above resource)
|
||||||
- https://9to5tutorial.com/homebrew-compiler-made-with-haskell-llvm-configuration (for help using llvm-hs-pure)
|
- https://9to5tutorial.com/homebrew-compiler-made-with-haskell-llvm-configuration (for help using llvm-hs-pure)
|
||||||
- https://blog.ocharles.org.uk/blog/posts/2012-12-17-24-days-of-hackage-optparse-applicative.html (for help parsing command line arguments with optparse-applicative)
|
- https://blog.ocharles.org.uk/blog/posts/2012-12-17-24-days-of-hackage-optparse-applicative.html (for help parsing command line arguments with optparse-applicative)
|
||||||
|
- http://learnyouahaskell.com/making-our-own-types-and-typeclasses (for help defining types)
|
||||||
|
|
||||||
### Tools
|
### Tools
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ executable really-bad-compiler-in-haskell
|
||||||
other-modules:
|
other-modules:
|
||||||
Main.LLVMGen
|
Main.LLVMGen
|
||||||
Main.Parser.Megaparsec
|
Main.Parser.Megaparsec
|
||||||
Main.Type
|
Main.Types
|
||||||
Paths_really_bad_compiler_in_haskell
|
Paths_really_bad_compiler_in_haskell
|
||||||
hs-source-dirs:
|
hs-source-dirs:
|
||||||
src
|
src
|
||||||
|
|
|
@ -22,7 +22,7 @@ import LLVM.IRBuilder.Constant
|
||||||
import LLVM.IRBuilder.Instruction
|
import LLVM.IRBuilder.Instruction
|
||||||
import LLVM.IRBuilder.Module
|
import LLVM.IRBuilder.Module
|
||||||
import LLVM.IRBuilder.Monad
|
import LLVM.IRBuilder.Monad
|
||||||
import Main.Type as Expr
|
import Main.Types qualified as T
|
||||||
|
|
||||||
data Env = Env
|
data Env = Env
|
||||||
{ operands :: M.Map Text Operand,
|
{ operands :: M.Map Text Operand,
|
||||||
|
@ -56,8 +56,8 @@ 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 :: [T.Statement] -> Module
|
||||||
getLLVM expr =
|
getLLVM statement =
|
||||||
flip evalState (Env {operands = M.empty, strings = M.empty}) $
|
flip evalState (Env {operands = M.empty, strings = M.empty}) $
|
||||||
buildModuleT "program" $ mdo
|
buildModuleT "program" $ mdo
|
||||||
-- TODO: better module name
|
-- TODO: better module name
|
||||||
|
@ -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
|
||||||
_ <- forM_ expr exprToLLVM
|
_ <- forM_ statement statementToLLVM
|
||||||
ret $ int32 0
|
ret $ int32 0
|
||||||
|
|
||||||
--
|
--
|
||||||
|
@ -74,42 +74,49 @@ getLLVM expr =
|
||||||
-- _ <- call (FunctionType i32 [ptr] True) printf [(ConstantOperand numFormatStr, []), (ourExpression, [])]
|
-- _ <- call (FunctionType i32 [ptr] True) printf [(ConstantOperand numFormatStr, []), (ourExpression, [])]
|
||||||
-- ret $ int32 0
|
-- ret $ int32 0
|
||||||
|
|
||||||
exprToLLVM ::
|
statementToLLVM ::
|
||||||
( MonadIRBuilder m,
|
( MonadIRBuilder m,
|
||||||
MonadModuleBuilder m,
|
MonadModuleBuilder m,
|
||||||
MonadState Env m
|
MonadState Env m
|
||||||
) =>
|
) =>
|
||||||
Expr ->
|
T.Statement ->
|
||||||
m Operand
|
m Operand
|
||||||
exprToLLVM (Lit prim) = pure $ primToLLVM prim
|
statementToLLVM (T.Print e) = mdo
|
||||||
exprToLLVM (Paren e) = exprToLLVM e
|
val <- intExprToLLVM e
|
||||||
exprToLLVM (Print e) = mdo
|
|
||||||
val <- exprToLLVM e
|
|
||||||
printf <- getOperand "printf"
|
printf <- getOperand "printf"
|
||||||
formatStr <- getString "%d\n"
|
formatStr <- getString "%d\n"
|
||||||
_ <- call (FunctionType i32 [ptr] True) printf [(formatStr, []), (val, [])]
|
_ <- call (FunctionType i32 [ptr] True) printf [(formatStr, []), (val, [])]
|
||||||
pure val
|
pure val
|
||||||
exprToLLVM (Expr.Add a b) = mdo
|
|
||||||
lhs <- exprToLLVM a
|
intExprToLLVM ::
|
||||||
rhs <- exprToLLVM b
|
( MonadIRBuilder m,
|
||||||
|
MonadModuleBuilder m,
|
||||||
|
MonadState Env m
|
||||||
|
) =>
|
||||||
|
T.Int ->
|
||||||
|
m Operand
|
||||||
|
intExprToLLVM (T.Int prim) = pure $ primToLLVM prim
|
||||||
|
intExprToLLVM (T.IntArith T.Add a b) = mdo
|
||||||
|
lhs <- intExprToLLVM a
|
||||||
|
rhs <- intExprToLLVM b
|
||||||
add lhs rhs
|
add lhs rhs
|
||||||
exprToLLVM (Expr.Sub a b) = mdo
|
intExprToLLVM (T.IntArith T.Sub a b) = mdo
|
||||||
lhs <- exprToLLVM a
|
lhs <- intExprToLLVM a
|
||||||
rhs <- exprToLLVM b
|
rhs <- intExprToLLVM b
|
||||||
sub lhs rhs
|
sub lhs rhs
|
||||||
exprToLLVM (Expr.Mul a b) = mdo
|
intExprToLLVM (T.IntArith T.Mul a b) = mdo
|
||||||
lhs <- exprToLLVM a
|
lhs <- intExprToLLVM a
|
||||||
rhs <- exprToLLVM b
|
rhs <- intExprToLLVM b
|
||||||
mul lhs rhs
|
mul lhs rhs
|
||||||
exprToLLVM (Expr.Div a b) = mdo
|
intExprToLLVM (T.IntArith T.Div a b) = mdo
|
||||||
lhs <- exprToLLVM a
|
lhs <- intExprToLLVM a
|
||||||
rhs <- exprToLLVM b
|
rhs <- intExprToLLVM b
|
||||||
sdiv lhs rhs
|
sdiv lhs rhs
|
||||||
|
|
||||||
primToLLVM :: Int -> Operand
|
primToLLVM :: Int -> Operand
|
||||||
primToLLVM i = int32 $ fromIntegral i
|
primToLLVM i = int32 $ fromIntegral i
|
||||||
|
|
||||||
llvmGen :: [Expr] -> IO ByteString
|
llvmGen :: [T.Statement] -> 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
|
|
@ -8,7 +8,7 @@ import Control.Monad.Combinators.Expr
|
||||||
import Data.Functor.Identity qualified
|
import Data.Functor.Identity qualified
|
||||||
import Data.Text
|
import Data.Text
|
||||||
import Data.Void (Void)
|
import Data.Void (Void)
|
||||||
import Main.Type
|
import Main.Types qualified as M
|
||||||
import Text.Megaparsec as MP hiding (parse)
|
import Text.Megaparsec as MP hiding (parse)
|
||||||
import Text.Megaparsec qualified as MP
|
import Text.Megaparsec qualified as MP
|
||||||
import Text.Megaparsec.Char qualified as C
|
import Text.Megaparsec.Char qualified as C
|
||||||
|
@ -31,48 +31,41 @@ string = C.string
|
||||||
container :: Text -> Text -> Parser a -> Parser a
|
container :: Text -> Text -> Parser a -> Parser a
|
||||||
container b e = between (symbol b) (symbol e)
|
container b e = between (symbol b) (symbol e)
|
||||||
|
|
||||||
term :: Parser Expr
|
parens :: Parser a -> Parser a
|
||||||
term =
|
parens = container "(" ")"
|
||||||
|
|
||||||
|
intExprTerm :: ParsecT Void Text Data.Functor.Identity.Identity M.Int
|
||||||
|
intExprTerm =
|
||||||
choice
|
choice
|
||||||
[ Lit <$> int,
|
[ M.Int <$> int,
|
||||||
container "(" ")" expr
|
parens intExpr
|
||||||
]
|
]
|
||||||
|
|
||||||
table :: [[Operator Parser Expr]]
|
intExprTable :: [[Operator Parser M.Int]]
|
||||||
table =
|
intExprTable =
|
||||||
[ [methodOp "print" Print],
|
[ [ binaryOp "*" (M.IntArith M.Mul),
|
||||||
[ binaryOp "*" Mul,
|
binaryOp "/" (M.IntArith M.Div)
|
||||||
binaryOp "/" Div
|
|
||||||
],
|
],
|
||||||
[ binaryOp "+" Add,
|
[ binaryOp "+" (M.IntArith M.Add),
|
||||||
binaryOp "-" Sub
|
binaryOp "-" (M.IntArith M.Sub)
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
intExpr :: Parser M.Int
|
||||||
|
intExpr = makeExprParser intExprTerm intExprTable
|
||||||
|
|
||||||
binaryOp :: Text -> (a -> a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a
|
binaryOp :: Text -> (a -> a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a
|
||||||
binaryOp name f = InfixL $ f <$ symbol name
|
binaryOp name f = InfixL $ f <$ symbol name
|
||||||
|
|
||||||
-- prefixOp :: Text -> (a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a
|
statement :: Parser M.Statement
|
||||||
-- prefixOp name f = Prefix (f <$ symbol name)
|
statement = string "print" *> (M.Print <$> parens intExpr) <* symbol ";"
|
||||||
|
|
||||||
methodOp :: Text -> (a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a
|
parseStatements :: Text -> Either (ParseErrorBundle Text Void) [M.Statement]
|
||||||
methodOp name f = Prefix $ f <$ (string name <* C.space)
|
parseStatements = MP.parse (C.space *> many statement <* eof) ""
|
||||||
|
|
||||||
-- postfixOp :: Text -> (a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a
|
parse :: Text -> [M.Statement]
|
||||||
-- postfixOp name f = Postfix (f <$ symbol name)
|
|
||||||
|
|
||||||
expr :: Parser Expr
|
|
||||||
expr = makeExprParser term table
|
|
||||||
|
|
||||||
statement :: Parser Expr
|
|
||||||
statement = expr <* symbol ";"
|
|
||||||
|
|
||||||
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 parseStatements t of
|
||||||
Right r -> r
|
Right r -> r
|
||||||
|
|
||||||
-- TODO: add error handling
|
-- TODO: add error handling
|
|
@ -1,30 +0,0 @@
|
||||||
module Main.Type
|
|
||||||
( Expr (..),
|
|
||||||
-- AST (..)
|
|
||||||
)
|
|
||||||
where
|
|
||||||
|
|
||||||
import Data.Graph (Tree (Node))
|
|
||||||
|
|
||||||
data Expr
|
|
||||||
= Lit Int
|
|
||||||
| Paren Expr
|
|
||||||
| Add Expr Expr
|
|
||||||
| Sub Expr Expr
|
|
||||||
| Mul Expr Expr
|
|
||||||
| Div Expr Expr
|
|
||||||
| Print Expr
|
|
||||||
deriving
|
|
||||||
( Show
|
|
||||||
)
|
|
||||||
|
|
||||||
-- data AST = AST Node
|
|
||||||
|
|
||||||
-- data Node
|
|
||||||
-- = Reg
|
|
||||||
-- { cur :: Expr,
|
|
||||||
-- next :: Node
|
|
||||||
-- }
|
|
||||||
-- | End
|
|
||||||
-- { cur :: Expr
|
|
||||||
-- }
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
module Main.Types
|
||||||
|
( ArithOp (..),
|
||||||
|
EqOp (..),
|
||||||
|
OrdCmpOp (..),
|
||||||
|
-- BinExpr (..),
|
||||||
|
Int (..),
|
||||||
|
Bool (..),
|
||||||
|
Statement (..),
|
||||||
|
)
|
||||||
|
where
|
||||||
|
|
||||||
|
import qualified Prelude as P
|
||||||
|
|
||||||
|
data ArithOp = Add | Sub | Mul | Div
|
||||||
|
|
||||||
|
data EqOp = Eq | Neq
|
||||||
|
|
||||||
|
data OrdCmpOp = GT | GTE | LT | LTE
|
||||||
|
|
||||||
|
-- newtype BinExpr op i o = BinExpr (op -> i -> i -> o)
|
||||||
|
|
||||||
|
data Int
|
||||||
|
= Int P.Int
|
||||||
|
| IntArith ArithOp Int Int -- (BinExpr ArithOp Int Int)
|
||||||
|
|
||||||
|
data Bool
|
||||||
|
= Bool P.Bool
|
||||||
|
| IntEq EqOp Int Int -- (BinExpr EqOp Int Bool)
|
||||||
|
| IntOrdCmp OrdCmpOp Int Int -- (BinExpr OrdCmpOp Int Bool)
|
||||||
|
| BoolEq EqOp Bool Bool -- (BinExpr EqOp Bool Bool)
|
||||||
|
|
||||||
|
data Statement
|
||||||
|
= Print Int
|
Reference in New Issue