Skip to content

vgtony/js-add

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

JavaScript-Style Calculator

A powerful command-line calculator built in Haskell that supports JavaScript-style variable declarations and arithmetic expressions with proper operator precedence.

License Haskell Stack

πŸš€ Features

  • JavaScript Syntax: Familiar let and const declarations
  • 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

πŸ“‹ Supported Syntax

Variable Declarations

let x = 10;           // Variable declaration
const pi = 3.14159;   // Constant declaration  
z = x + y;            // Regular assignment

Mathematical Operations

addition = 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

πŸ› οΈ Installation

Prerequisites

  • Stack (Haskell build tool)
  • Git

Build from Source

# 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

Quick Start

# Interactive mode
stack exec js-add

# Single expression
echo "let result = 35 / 2" | stack exec js-add

🎯 Usage Examples

Interactive Session

$ 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!

Standalone Executable

A pre-built executable with batch launcher is available in the Calculator-Standalone/ folder:

  • Double-click Start Calculator.bat for guided startup
  • Or run JavaScript-Calculator.exe directly

πŸ—οΈ Architecture

Parser (Megaparsec)

  • Lexical Analysis: Tokenizes JavaScript-style syntax
  • Syntax Analysis: Builds Abstract Syntax Tree (AST)
  • Operator Precedence: Handles mathematical precedence correctly

Evaluator

  • Environment: Maps variable names to values
  • Type Safety: Uses Maybe for error handling
  • Functional Style: Clean applicative operations

REPL

  • Interactive Loop: Read-Eval-Print-Loop
  • Error Recovery: Continues after parse/evaluation errors
  • EOF Handling: Graceful exit on input end

πŸ”§ Technical Details

Built With

  • 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

Project Structure

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

πŸ§ͺ Development

Running Tests

# Build and test
stack build --test

# Run specific tests
stack test

Code Quality

  • Zero Warnings: Clean compilation
  • Haddock Documentation: Comprehensive API docs
  • Type Safety: Leverages Haskell's type system
  • Functional Style: Immutable data structures

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Setup

# Clone your fork
git clone https://github.com/yourusername/js-calculator.git

# Install dependencies
stack build --dependencies-only

# Run in development mode
stack ghci

πŸ“ License

This project is licensed under the BSD3 License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Megaparsec library for excellent parser combinators
  • Stack for reliable Haskell builds
  • Haskell Community for the amazing ecosystem

πŸ› Known Issues

  • Variables are session-only (not persistent between runs)
  • No support for functions or complex data types
  • Limited to basic arithmetic operations

🚧 Future Enhancements

  • 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!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published