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