Allow parsing signed integers

archive/forth
Ethan Reece 2023-09-30 02:54:48 -05:00
parent 83aa5bd4d3
commit cca9262fc3
Signed by: me
GPG Key ID: D3993665FF92E1C3
5 changed files with 15 additions and 17 deletions

View File

@ -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:

View File

@ -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

View File

@ -19,16 +19,18 @@ 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
<$> int
<|> Add
try
( Lit
<$> int
)
<|> Add
<$ symbol "+"
<|> Sub
<$ symbol "-"

View File

@ -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