A powerful command-line calculator built in Haskell that supports JavaScript-style variable declarations and arithmetic expressions with proper operator precedence.
- JavaScript Syntax: Familiar letandconstdeclarations
- Decimal Arithmetic: Proper floating-point calculations (35/2 = 17.5)
- Variable Storage: Variables persist throughout the session
- Operator Precedence: Follows mathematical rules (*, / before +, -)
- Error Handling: Division by zero and undefined variable protection
- Interactive REPL: Real-time expression evaluation
- Comments Support: //line comments and/* */block comments
let x = 10;           // Variable declaration
const pi = 3.14159;   // Constant declaration  
z = x + y;            // Regular assignmentaddition = 5 + 3;           // Addition: 8
subtraction = 10 - 4;       // Subtraction: 6
multiplication = 6 * 7;     // Multiplication: 42
division = 35 / 2;          // Division: 17.5
precedence = 5 + 3 * 2;     // Precedence: 11 (not 16)
parentheses = (5 + 3) * 2;  // Parentheses: 16- Stack (Haskell build tool)
- Git
# Clone the repository
git clone https://github.com/yourusername/js-calculator.git
cd js-calculator
# Build with Stack
stack build
# Run the calculator
stack exec js-add# Interactive mode
stack exec js-add
# Single expression
echo "let result = 35 / 2" | stack exec js-add$ stack exec js-add
JavaScript-Style Calculator!
Supports: +, -, *, /, variables, decimals, and JavaScript syntax
calc> let x = 10;
Let declaration: x = 10.0
Result: x = 10.0
calc> let y = 20;
Let declaration: y = 20.0
Result: y = 20.0
calc> result = (x + y) / 2;
Assignment: result = ((10.0 + 20.0) / 2.0)
Result: result = 15.0
calc> const pi = 3.14159;
Const declaration: pi = 3.14159
Result: pi = 3.14159
calc> area = pi * 5 * 5;
Assignment: area = ((3.14159 * 5.0) * 5.0)
Result: area = 78.53975
calc> quit
Goodbye!
A pre-built executable with batch launcher is available in the Calculator-Standalone/ folder:
- Double-click Start Calculator.batfor guided startup
- Or run JavaScript-Calculator.exedirectly
- Lexical Analysis: Tokenizes JavaScript-style syntax
- Syntax Analysis: Builds Abstract Syntax Tree (AST)
- Operator Precedence: Handles mathematical precedence correctly
- Environment: Maps variable names to values
- Type Safety: Uses Maybefor error handling
- Functional Style: Clean applicative operations
- Interactive Loop: Read-Eval-Print-Loop
- Error Recovery: Continues after parse/evaluation errors
- EOF Handling: Graceful exit on input end
- Language: Haskell (GHC 9.10.3)
- Parser: Megaparsec library
- Build Tool: Stack
- Dependencies:
- megaparsec- Parser combinators
- text- Efficient text processing
- containers- Data structures
- parser-combinators- Expression parsing
 
js-calculator/
βββ src/
β   βββ Main.hs              # Main calculator implementation
βββ Calculator-Standalone/    # Pre-built executables
βββ js-add.cabal            # Package configuration
βββ stack.yaml              # Stack configuration
βββ README.md               # This file
βββ LICENSE                 # BSD3 License
# Build and test
stack build --test
# Run specific tests
stack test- Zero Warnings: Clean compilation
- Haddock Documentation: Comprehensive API docs
- Type Safety: Leverages Haskell's type system
- Functional Style: Immutable data structures
- Fork the repository
- Create a feature branch (git checkout -b feature/amazing-feature)
- Commit your changes (git commit -m 'Add amazing feature')
- Push to the branch (git push origin feature/amazing-feature)
- Open a Pull Request
# Clone your fork
git clone https://github.com/yourusername/js-calculator.git
# Install dependencies
stack build --dependencies-only
# Run in development mode
stack ghciThis project is licensed under the BSD3 License - see the LICENSE file for details.
- Megaparsec library for excellent parser combinators
- Stack for reliable Haskell builds
- Haskell Community for the amazing ecosystem
- Variables are session-only (not persistent between runs)
- No support for functions or complex data types
- Limited to basic arithmetic operations
- Mathematical functions (sin, cos, sqrt, etc.)
- Variable persistence to file
- Multi-line expressions
- Function definitions
- More JavaScript syntax (if/else, loops)
- Web interface
Made with β€οΈ and Haskell | Perfect for learning parser techniques!