diff --git a/really-bad-compiler-in-haskell.cabal b/really-bad-compiler-in-haskell.cabal index b068795..7410b31 100644 --- a/really-bad-compiler-in-haskell.cabal +++ b/really-bad-compiler-in-haskell.cabal @@ -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: diff --git a/src/Forth/LLVMGen.hs b/src/Forth/LLVMGen.hs index 662783b..6a3d9e9 100644 --- a/src/Forth/LLVMGen.hs +++ b/src/Forth/LLVMGen.hs @@ -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 diff --git a/src/Forth/Parser.hs b/src/Forth/Parser.hs index ecef115..0f564af 100644 --- a/src/Forth/Parser.hs +++ b/src/Forth/Parser.hs @@ -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 "-" @@ -49,4 +51,4 @@ parse :: Text -> [Token] parse t = do case parseItems t of Left err -> [] -- putStrLn $ errorBundlePretty err - Right tns -> tns + Right tns -> tns \ No newline at end of file diff --git a/src/Forth/Types/Expr.hs b/src/Forth/Types/Expr.hs.old similarity index 100% rename from src/Forth/Types/Expr.hs rename to src/Forth/Types/Expr.hs.old diff --git a/src/Main.hs b/src/Main.hs index 4cf4dde..3883f90 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -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