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

View File

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

View File

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

View File

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