Compare commits

..

3 Commits

Author SHA1 Message Date
Ethan Reece b89be8de8c
Add division 2023-09-29 05:31:33 -05:00
Ethan Reece 08e684206e
Update README.md 2023-09-29 05:21:50 -05:00
Ethan Reece 0b915f5788
Remove llvm-hs-pretty since it was generating broken code 2023-09-29 05:19:11 -05:00
7 changed files with 15 additions and 23 deletions

View File

@ -1,15 +1,19 @@
# Really Bad Compiler in Haskell
A compiler written in Haskell which can currently perform basic arithmetic. Currently using the megaparsec and llvm-hs-\* libraries, but I plan to reimplement parsing and llvm generation myself. Built for the Introduction to Compiler Design class at The University of Texas at Dallas.
A compiler written in Haskell which can currently perform basic arithmetic. Currently using the megaparsec and llvm-hs-\* libraries, but I may reimplement certain libraries myself. Built for the Introduction to Compiler Design class at The University of Texas at Dallas.
Main repo: https://git.sudoer.ch/me/really-bad-compiler-in-haskell
## Build Instructions
## Setup Instructions
- 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`.
## Run Instructions
- Use `stack run <file>` to run the program (for example, `stack run example/1`).
- The LLVM will be generated as `a.out.ll`, and the executable will be generated as `a.out`.
## To edit
@ -45,7 +49,7 @@ I recommend using VSCodium, which is preconfigured to have syntax highlighting a
- Language: Haskell
- Haskell tools: GHCup, Stack, Cabal, GHC 9.2
- Libraries: megaparsec, parser-combinators, text, process, llvm-hs-pure 15, llvm-hs-pretty-15
- Libraries: megaparsec, parser-combinators, text, process, llvm-hs 15, llvm-hs-pure 15,
- Dependencies: llvm 15, clang 15
- IDE: VSCodium
- Git platform: Forgejo

View File

@ -27,9 +27,7 @@ getLLVM expr =
printf <- externVarArgs "printf" [ptr] i32
function "main" [] i32 $ \_ -> mdo
numFormatStr <- globalStringPtr "%d\n" (mkName "str")
traceShow numFormatStr $ pure ()
ourExpression <- exprToLLVM expr
-- _ <- call (FunctionType i32 [i32])
_ <- call (FunctionType i32 [ptr] True) printf [(ConstantOperand numFormatStr, []), (ourExpression, [])]
ret (int32 0)
@ -52,6 +50,10 @@ exprToLLVM (Expr.Mul a b) = mdo
lhs <- exprToLLVM a
rhs <- exprToLLVM b
mul lhs rhs
exprToLLVM (Expr.Div a b) = mdo
lhs <- exprToLLVM a
rhs <- exprToLLVM b
sdiv lhs rhs
primToLLVM :: Int -> Operand
primToLLVM i = int32 (fromIntegral i)

View File

@ -27,8 +27,8 @@ term = Lit <$> intParser
table :: [[Operator Parser Expr]]
table =
[ [ binaryOp "*" Mul
-- binaryOp "/" Div
[ [ binaryOp "*" Mul,
binaryOp "/" Div
],
[ binaryOp "+" Add,
binaryOp "-" Sub

View File

@ -5,7 +5,7 @@ data Expr
| Add Expr Expr
| Sub Expr Expr
| Mul Expr Expr
| Div Expr Expr
deriving
( -- | Div Expr Expr
Show
( Show
)

View File

@ -9,7 +9,6 @@ dependencies:
- process
- llvm-hs >= 15 && < 16
- llvm-hs-pure >= 15 && < 16
# - llvm-hs-pretty >= 15 && < 16
- bytestring
tested-with: GHC == 9.2.8
category: Compilers/Interpreters

View File

@ -3,8 +3,6 @@ resolver: lts-20.26
packages:
- .
extra-deps:
- github: hyunsooda/llvm-hs-pretty-15
commit: 79283942d1667168ecd65237667aff7fed730303
- github: llvm-hs/llvm-hs
commit: 5bca2c1a2a3aa98ecfb19181e7a5ebbf3e212b76
subdirs:

View File

@ -4,17 +4,6 @@
# https://docs.haskellstack.org/en/stable/lock_files
packages:
- completed:
name: llvm-hs-pretty
pantry-tree:
sha256: 22ac4a47e1833dece52af7a6f8589958021d67292f6cc3de015ecb7c699db62d
size: 5197
sha256: 8348b72a61807414ad15e0b7e82a7753e4473701e7ab98520c2e9d9a51c5ed7f
size: 43787
url: https://github.com/hyunsooda/llvm-hs-pretty-15/archive/79283942d1667168ecd65237667aff7fed730303.tar.gz
version: 15.0.0
original:
url: https://github.com/hyunsooda/llvm-hs-pretty-15/archive/79283942d1667168ecd65237667aff7fed730303.tar.gz
- completed:
name: llvm-hs-pure
pantry-tree: