I'm interested in designing some interesting applications in different programming languages (such as JavaScript, Java, Go, C#).
This project embodies my curiosity to Haskell programming language. Haskell is different from any other programming languages. And it requires thinking differently.
I'm going to design and build an awesome compiler for C language.
To get this build and run you should install Haskell programming language and prepare GHC compiler.
You can also use any other Haskell compilers (such as Hugs), but this program was not tested with this compilers and it can lead to some side-effects and bugs. Use it at your own risk.
Clone the repository:
git clone https://github.com/technoboom/haskell-c-compiler
.
The basic command for building your project is:
stack build
Very likely, you'll need to first tell Stack to install the appropriate GHC version for your project. You can do this with:
stack setup
or by using the --install-ghc option to stack build:
stack --install-ghc build
Let's suppose your project defines an executable called my-executable. How do you run it? There are three common ways:
stack exec my-executable
will modify yourPATH
variable to include a number of additional directories, including the internal executable destination, and your build tools (like ghc).
Example: stack exec haskell-c-compiler-exe
stack exec which my-executable
will use thewhich
command to find the full path to your executable, which you could then run, without the additional modifications thatstack exec
implies. If you want to be clever, you could do something like this from your shell:
$ $(stack exec which my-executable)
- The
stack install
command will copy your executables into a user-specific directory, such as$HOME/.local/bin
on POSIX systems. The directory will be printed to your console.
Testing is also straightforward:
stack test
As it happens, this is just a convenience shortcut for:
stack build --test
The same applies to stack bench (for benchmarking) and stack haddock (for building Haddock documentation). What this means is that you can compose these flags to build the code, build the docs, run tests, and run benchmarks:
stack build --test --bench --haddock
--file-watch
will run build in file-watch mode, where it will wait for changes to files and then automatically rebuild. This can be very convenient to run in a terminal while simulatenously editing in another window.--fast
will disable optimizations--pedantic
turns on-Wall
-Werror
for GHC (all warnings on, and warnings treated as errors)
So throwing a few of these together:
stack build --test --file-watch --fast --pedantic
- app/Main.hs - the main haskell source file
- src/Lib.hs - the core lib source file
- test/Spec.hs - the spec source file
- haskell-c-compiler.cabal - the cabal build description
- Setup.hs
- stack.yaml
- ChangeLog.md - change log
- README - info
- LICENSE - license
- Haskell programming language
- Compiler: GHC
- Build system: Stack
- Haddok for generating documentation from annotated Haskell source code
- Testing: HSpec (spec testing), HUnit (unit testing)
- Distribution: Hackage
- Lint: HLint package
- Haskell documentation: https://www.haskell.org/documentation
- Stack Guide: https://docs.haskellstack.org/en/stable/GUIDE/
- Haddok Doc: http://haskell.org/haddock/
- HSpec: http://hspec.github.io/
- HUnit: http://hackage.haskell.org/package/HUnit
- Hackage: http://hackage.haskell.org/packages/hackage.html