diff --git a/README.md b/README.md index 1370de1..ec14e4f 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Main repo: https://git.sudoer.ch/me/really-bad-compiler-in-haskell - Install `ghcup` (for managing Haskell tools) and `nix` (for managing external dependencies). - Clone the repo. -- Use `ghcup` to install `stack 2.9.3`, `HLS 2.2.0.0`, `GHC 9.2.8`, and `cabal 3.6.2.0`. +- Use `ghcup` to install `stack 2.11.1`, `HLS 2.3.0.0`, and `cabal 3.8.1.0`. ## Run Instructions diff --git a/really-bad-compiler-in-haskell.cabal b/really-bad-compiler-in-haskell.cabal index 13f796f..f153056 100644 --- a/really-bad-compiler-in-haskell.cabal +++ b/really-bad-compiler-in-haskell.cabal @@ -1,6 +1,6 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.35.1. +-- This file has been generated from package.yaml by hpack version 0.35.2. -- -- see: https://github.com/sol/hpack diff --git a/src/Main.hs b/src/Main.hs index 13e893f..c8b387f 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -12,12 +12,11 @@ import System.Process (callCommand) main :: IO () main = do - fileName <- fmap head getArgs + fileName <- head <$> getArgs contents <- T.readFile fileName - T.putStrLn "- Parsing file..." - let parsed = parse contents T.putStrLn "- Generating LLVM to './a.out.ll'..." - llvmGen parsed >>= B.writeFile "a.out.ll" + result <- (llvmGen . parse) contents + B.writeFile "a.out.ll" result T.putStrLn "- Compiling to executable './a.out'..." callCommand "clang a.out.ll" - T.putStrLn "- Done." + T.putStrLn "- Done." \ No newline at end of file diff --git a/src/Main/LLVMGen.hs b/src/Main/LLVMGen.hs index 818a79f..a8ce6c6 100644 --- a/src/Main/LLVMGen.hs +++ b/src/Main/LLVMGen.hs @@ -17,6 +17,9 @@ import LLVM.IRBuilder.Module import LLVM.IRBuilder.Monad import Main.Types.Expr as Expr +-- printf :: Operand +-- printf = externVarArgs "printf" [ptr] i32 + getLLVM :: Expr -> Module getLLVM expr = buildModule "program" $ mdo @@ -26,7 +29,12 @@ getLLVM expr = numFormatStr <- globalStringPtr "%d\n" (mkName "str") ourExpression <- exprToLLVM expr _ <- call (FunctionType i32 [ptr] True) printf [(ConstantOperand numFormatStr, []), (ourExpression, [])] - ret (int32 0) + ret $ int32 0 + +-- getLLVM2 expr = +-- getLLVM $ + +-- return (printf) exprToLLVM :: ( MonadIRBuilder m, @@ -36,6 +44,7 @@ exprToLLVM :: m Operand exprToLLVM (Lit prim) = pure $ primToLLVM prim exprToLLVM (Paren e) = exprToLLVM e +-- exprToLLVM (Print e) = mdo exprToLLVM (Expr.Add a b) = mdo lhs <- exprToLLVM a rhs <- exprToLLVM b @@ -54,12 +63,9 @@ exprToLLVM (Expr.Div a b) = mdo sdiv lhs rhs primToLLVM :: Int -> Operand -primToLLVM i = int32 (fromIntegral i) +primToLLVM i = int32 $ fromIntegral i llvmGen :: Expr -> IO ByteString llvmGen expr = do let l = getLLVM expr - withContext - ( \c -> - withModuleFromAST c l moduleLLVMAssembly - ) \ No newline at end of file + withContext $ \c -> withModuleFromAST c l moduleLLVMAssembly \ No newline at end of file diff --git a/src/Main/Parser/Megaparsec.hs b/src/Main/Parser/Megaparsec.hs index 6178a9d..33543de 100644 --- a/src/Main/Parser/Megaparsec.hs +++ b/src/Main/Parser/Megaparsec.hs @@ -50,13 +50,13 @@ table = ] binaryOp :: Text -> (a -> a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a -binaryOp name f = InfixL (f <$ symbol name) +binaryOp name f = InfixL $ f <$ symbol name -- prefixOp :: Text -> (a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a -- prefixOp name f = Prefix (f <$ symbol name) methodOp :: Text -> (a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a -methodOp name f = Prefix (f <$ (string name <* C.space)) +methodOp name f = Prefix $ f <$ (string name <* C.space) -- postfixOp :: Text -> (a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a -- postfixOp name f = Postfix (f <$ symbol name) @@ -68,7 +68,7 @@ parseExpr :: Text -> Either (ParseErrorBundle Text Void) Expr parseExpr = MP.parse (C.space *> expr <* eof) "" parse :: Text -> Expr -parse t = do +parse t = case parseExpr t of Right r -> r diff --git a/stack.yaml b/stack.yaml index 45dc800..c54066b 100644 --- a/stack.yaml +++ b/stack.yaml @@ -1,4 +1,11 @@ resolver: lts-20.26 +compiler: ghc-9.2.8 +# setup-info: +# ghc: +# aarch64: +# 9.4.6: +# url: "https://downloads.haskell.org/~ghc/9.4.6/ghc-9.4.6-aarch64-deb10-linux.tar.xz" +# sha256: "05896fc4bc52c117d281eac9c621c6c3a0b14f9f9eed5e42cce5e1c4485c7623" packages: - . @@ -12,4 +19,4 @@ nix: enable: true packages: [llvm_15, clang_15, libxml2] system-ghc: true -install-ghc: true +install-ghc: false