https://github.com/biuld/calculator
an arithmetic calculator written with compiling theory in mind
https://github.com/biuld/calculator
arithmetic-calculator basic-compiler compiling-theory complier-tutorial functional-programming haskell
Last synced: 3 days ago
JSON representation
an arithmetic calculator written with compiling theory in mind
- Host: GitHub
- URL: https://github.com/biuld/calculator
- Owner: biuld
- License: bsd-3-clause
- Created: 2020-12-19T09:36:37.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2025-06-21T17:02:53.000Z (7 months ago)
- Last Synced: 2025-06-21T17:41:43.131Z (7 months ago)
- Topics: arithmetic-calculator, basic-compiler, compiling-theory, complier-tutorial, functional-programming, haskell
- Language: Haskell
- Homepage:
- Size: 2.56 MB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
- License: LICENSE
Awesome Lists containing this project
README
# calculator
A statically typed calculator language written in Haskell with a focus on type safety, parsing, and compilation theory.
## Features
- **Rich Type System**
- Integers and floating-point numbers
- Boolean values
- Strings
- Tuples
- Unit type
- First-class functions
- **Expressions and Operations**
- Arithmetic operations `(+ - * /)` for `Int` and `Double`
- Comparison operations `(== !=)`
- Logical operations `(&& || !)`
- Parenthesised expressions
- **Control Flow**
- `if … then … else …` expressions
- `while` loops
- Code blocks `{ e1; e2; … }`
- Expression sequences
- **Variables and Functions**
- `let` bindings (single and multiple)
- Lambda expressions with explicit type annotations
- Partial application & multi-argument functions
## Getting Started
### Prerequisites
The easiest way to build the project is with [Stack](https://docs.haskellstack.org/). Any recent GHC (≥ 9.8) should work if you prefer Cabal.
### Build
```bash
# Clone and enter the repository
$ git clone https://github.com/biuld/calculator.git
$ cd calculator
# Build everything
$ stack build
```
### CLI Usage
The executable is exposed as `calculator-exe`. Use `stack run` (or Cabal's `cabal run`) to invoke it:
```bash
# General form
stack run calculator-exe -- [-r]
```
*Commands*
- `parse` – Parse the expression and pretty-print the Concrete Syntax Tree (CST)
- `desugar` – Parse and desugar the CST into the typed Abstract Syntax Tree (AST)
- `eval` – Evaluate the expression with the small-step interpreter
Add the `-r` switch to display the raw `Show` instance instead of the pretty printer.
#### Examples
```bash
# Pretty print CST
stack run calculator-exe -- parse "(1 + 2) * 3"
# Show raw CST
stack run calculator-exe -- parse -r "(1 + 2) * 3"
# Desugar to AST
stack run calculator-exe -- desugar "let { f = \a:Int -> \b:Int -> a + b; x = 1; y = 2 } in f (x , y)"
# Evaluate an expression
stack run calculator-exe -- eval "let {x = 10; y = 20} in x + y"
```
## Project Layout
```
src/
Language/Calculator/
CST/ -- Lexer, parser, tokens & CST pretty printer
AST/ -- Typed AST, interpreter, value & environment utilities
Desugar.hs -- CST → AST desugaring with type checking
app/Main.hs -- Command-line interface
test/ -- HSpec test-suite
```
## Running the Test-suite
```bash
stack test
```
The suite exercises both the parser and desugaring phases (see `test/ParserSpec.hs` and `test/DesugarSpec.hs`).
## Roadmap
Planned improvements include:
- Pattern matching
- Algebraic data types
- A proper optimiser
- Bytecode compilation and a VM back-end
## License
This project is licensed under the BSD-3-Clause license. See `LICENSE` for the full text.