Allow parsing signed integers
parent
83aa5bd4d3
commit
cca9262fc3
|
@ -18,7 +18,6 @@ executable really-bad-compiler-in-haskell
|
||||||
other-modules:
|
other-modules:
|
||||||
Forth.LLVMGen
|
Forth.LLVMGen
|
||||||
Forth.Parser
|
Forth.Parser
|
||||||
Forth.Types.Expr
|
|
||||||
Forth.Types.Token
|
Forth.Types.Token
|
||||||
Paths_really_bad_compiler_in_haskell
|
Paths_really_bad_compiler_in_haskell
|
||||||
hs-source-dirs:
|
hs-source-dirs:
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
module Forth.LLVMGen (llvmGen) where
|
module Forth.LLVMGen (llvmGen) where
|
||||||
|
|
||||||
import Data.ByteString (ByteString)
|
import Data.ByteString (ByteString)
|
||||||
import Forth.Types.Expr as Expr
|
import Forth.Types.Token as Token
|
||||||
import LLVM (moduleLLVMAssembly, withModuleFromAST)
|
import LLVM (moduleLLVMAssembly, withModuleFromAST)
|
||||||
import LLVM.AST hiding (function)
|
import LLVM.AST hiding (function)
|
||||||
import LLVM.AST.Type
|
import LLVM.AST.Type
|
||||||
|
@ -17,7 +17,7 @@ import LLVM.IRBuilder.Instruction
|
||||||
import LLVM.IRBuilder.Module
|
import LLVM.IRBuilder.Module
|
||||||
import LLVM.IRBuilder.Monad
|
import LLVM.IRBuilder.Monad
|
||||||
|
|
||||||
getLLVM :: Expr -> Module
|
getLLVM :: Token -> Module
|
||||||
getLLVM expr =
|
getLLVM expr =
|
||||||
buildModule "program" $ mdo
|
buildModule "program" $ mdo
|
||||||
-- TODO: better module name
|
-- TODO: better module name
|
||||||
|
@ -32,22 +32,22 @@ exprToLLVM ::
|
||||||
( MonadIRBuilder m,
|
( MonadIRBuilder m,
|
||||||
MonadModuleBuilder m
|
MonadModuleBuilder m
|
||||||
) =>
|
) =>
|
||||||
Expr ->
|
Token ->
|
||||||
m Operand
|
m Operand
|
||||||
exprToLLVM (Lit prim) = pure $ primToLLVM prim
|
exprToLLVM (Lit prim) = pure $ primToLLVM prim
|
||||||
exprToLLVM (Expr.Add a b) = mdo
|
exprToLLVM (Token.Add a b) = mdo
|
||||||
lhs <- exprToLLVM a
|
lhs <- exprToLLVM a
|
||||||
rhs <- exprToLLVM b
|
rhs <- exprToLLVM b
|
||||||
add lhs rhs
|
add lhs rhs
|
||||||
exprToLLVM (Expr.Sub a b) = mdo
|
exprToLLVM (Token.Sub a b) = mdo
|
||||||
lhs <- exprToLLVM a
|
lhs <- exprToLLVM a
|
||||||
rhs <- exprToLLVM b
|
rhs <- exprToLLVM b
|
||||||
sub lhs rhs
|
sub lhs rhs
|
||||||
exprToLLVM (Expr.Mul a b) = mdo
|
exprToLLVM (Token.Mul a b) = mdo
|
||||||
lhs <- exprToLLVM a
|
lhs <- exprToLLVM a
|
||||||
rhs <- exprToLLVM b
|
rhs <- exprToLLVM b
|
||||||
mul lhs rhs
|
mul lhs rhs
|
||||||
exprToLLVM (Expr.Div a b) = mdo
|
exprToLLVM (Token.Div a b) = mdo
|
||||||
lhs <- exprToLLVM a
|
lhs <- exprToLLVM a
|
||||||
rhs <- exprToLLVM b
|
rhs <- exprToLLVM b
|
||||||
sdiv lhs rhs
|
sdiv lhs rhs
|
||||||
|
@ -55,7 +55,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 :: Token -> IO ByteString
|
||||||
llvmGen expr = do
|
llvmGen expr = do
|
||||||
let l = getLLVM expr
|
let l = getLLVM expr
|
||||||
withContext
|
withContext
|
||||||
|
|
|
@ -19,16 +19,18 @@ lexeme :: Parser a -> Parser a
|
||||||
lexeme = L.lexeme C.space
|
lexeme = L.lexeme C.space
|
||||||
|
|
||||||
int :: Parser Int
|
int :: Parser Int
|
||||||
int = lexeme L.decimal
|
int = lexeme $ L.signed (return ()) L.decimal
|
||||||
|
|
||||||
symbol :: Text -> Parser Text
|
symbol :: Text -> Parser Text
|
||||||
symbol = L.symbol C.space
|
symbol = L.symbol C.space
|
||||||
|
|
||||||
item :: Parser Token
|
item :: Parser Token
|
||||||
item =
|
item =
|
||||||
Lit
|
try
|
||||||
<$> int
|
( Lit
|
||||||
<|> Add
|
<$> int
|
||||||
|
)
|
||||||
|
<|> Add
|
||||||
<$ symbol "+"
|
<$ symbol "+"
|
||||||
<|> Sub
|
<|> Sub
|
||||||
<$ symbol "-"
|
<$ symbol "-"
|
||||||
|
@ -49,4 +51,4 @@ parse :: Text -> [Token]
|
||||||
parse t = do
|
parse t = do
|
||||||
case parseItems t of
|
case parseItems t of
|
||||||
Left err -> [] -- putStrLn $ errorBundlePretty err
|
Left err -> [] -- putStrLn $ errorBundlePretty err
|
||||||
Right tns -> tns
|
Right tns -> tns
|
|
@ -3,13 +3,10 @@
|
||||||
|
|
||||||
module Main (main) where
|
module Main (main) where
|
||||||
|
|
||||||
-- import Compiler.ExeGen
|
|
||||||
|
|
||||||
import Data.ByteString.Char8 qualified as B
|
import Data.ByteString.Char8 qualified as B
|
||||||
import Data.Text.IO qualified as T
|
import Data.Text.IO qualified as T
|
||||||
import Forth.LLVMGen
|
import Forth.LLVMGen
|
||||||
import Forth.Parser
|
import Forth.Parser
|
||||||
import Forth.Types.Expr
|
|
||||||
import System.Environment
|
import System.Environment
|
||||||
import System.Process
|
import System.Process
|
||||||
|
|
||||||
|
|
Reference in New Issue