{"id":23003095,"url":"https://github.com/programmerstevie/haskellmather","last_synced_at":"2025-11-01T08:04:41.340Z","repository":{"id":197692055,"uuid":"265741242","full_name":"programmerstevie/haskellMather","owner":"programmerstevie","description":"A Haskell-based mathematical expression parser and evaluator that converts input strings into Abstract Syntax Trees (ASTs) and evaluates them. Supports basic arithmetic operations including addition, subtraction, multiplication, division, exponentiation, and modulus, with both integer and floating-point numbers.","archived":false,"fork":false,"pushed_at":"2024-10-04T04:26:33.000Z","size":24,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T20:56:38.795Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/programmerstevie.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-21T03:02:55.000Z","updated_at":"2024-10-04T04:27:10.000Z","dependencies_parsed_at":"2025-02-08T05:35:00.644Z","dependency_job_id":null,"html_url":"https://github.com/programmerstevie/haskellMather","commit_stats":null,"previous_names":["stothesecond/haskellmather","programmerstevie/haskellmather"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/programmerstevie%2FhaskellMather","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/programmerstevie%2FhaskellMather/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/programmerstevie%2FhaskellMather/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/programmerstevie%2FhaskellMather/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/programmerstevie","download_url":"https://codeload.github.com/programmerstevie/haskellMather/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246828842,"owners_count":20840533,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-15T07:13:23.024Z","updated_at":"2025-11-01T08:04:41.311Z","avatar_url":"https://github.com/programmerstevie.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Haskell Mathematical Expression Parser and Evaluator\n\nThis project is a Haskell implementation of a mathematical expression parser and evaluator. The parser takes an input string representing a mathematical expression, tokenizes it into meaningful tokens (numbers, operators, parentheses), parses it into an abstract syntax tree (AST), and evaluates the result. It supports basic arithmetic operations and can be extended with additional functionality as needed.\n\n## Table of Contents\n- [Features](#features)\n- [Getting Started](#getting-started)\n  - [Prerequisites](#prerequisites)\n  - [Installing](#installing)\n- [Usage](#usage)\n- [Project Structure](#project-structure)\n- [How It Works](#how-it-works)\n  - [1. Lexical Analysis (Tokenization)](#1-lexical-analysis-tokenization)\n  - [2. Parsing](#2-parsing)\n  - [3. Evaluation](#3-evaluation)\n- [Extending the Parser](#extending-the-parser)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Features\n- Parses mathematical expressions into an Abstract Syntax Tree (AST).\n- Supports basic arithmetic operations: addition (`+`), subtraction (`-`), multiplication (`*`), division (`/`), exponentiation (`^`), and modulus (`%`).\n- Handles parentheses for grouping expressions.\n- Supports both integer and floating-point numbers.\n- Clean and modular design with a lexer, parser, and expression evaluation.\n- Easily extendable to add more operations and functions.\n\n## Getting Started\n\n### Prerequisites\nYou will need [GHC (Glasgow Haskell Compiler)](https://www.haskell.org/ghc/) installed to compile and run this project.\n\n### Installing\n1. Clone the repository:\n   ```bash\n   git clone https://github.com/programmerstevie/haskell-parser.git\n   cd haskell-parser\n   ```\n\n2. Compile the project:\n   ```bash\n   ghc -o parser Main.hs\n   ```\n\n## Usage\nOnce the project is compiled, run the executable to start evaluating mathematical expressions.\n\n```bash\n./parser\n```\n\nEnter a mathematical expression to evaluate:\n\n```bash\n(5 + 3) * 2 - 4 ^ 2\n```\n\nThe output will show the result:\n\n```bash\n(5 + 3) * 2 - 4 ^ 2 = -4\n```\n\n## Project Structure\nThe project is divided into several modules:\n- `Lexer.hs`: Defines how input strings are broken into tokens representing numbers, operators, and parentheses. It supports both integer and floating-point numbers.\n- `Token.hs`: Defines the different types of tokens used in the parsing process, including constants (integers and doubles), binary operators, and parentheses.\n- `Parser.hs`: Uses the `ParserLib` module to parse the tokens into an Abstract Syntax Tree (AST).\n- `ParserLib.hs`: A custom parser combinator library used to compose the lexer and parser.\n- `Expression.hs`: Contains the logic for parsing and evaluating expressions. It defines how to handle operator precedence, associativity, and the conversion from tokens to an `Expression`.\n- `Grammar.hs`: Defines the structure of valid expressions, including binary operations and constants, as well as helper functions for converting between binary operators and operation symbols.\n- `Evaluator.hs`: Handles the actual evaluation of the parsed AST into either an integer or a floating-point value, depending on the expression type.\n- `Main.hs`: The entry point of the application, responsible for combining the lexer, parser, and evaluator into a functional program.\n\n## How It Works\n\n### 1. Lexical Analysis (Tokenization)\nThe lexer (`Lexer.hs`) breaks down the input string into tokens representing constants, operators, and parentheses. These tokens are represented by the `Token` data type defined in the `Token.hs` module. It supports both integer and floating-point constants.\n\nFor example, the expression `3 + (5.2 * 2)` is tokenized as:\n\n```plaintext\nConstI_T(3), Binop_T(ADD), LParen_T, ConstD_T(5.2), Binop_T(MUL), ConstI_T(2), RParen_T\n```\n\n### 2. Parsing\nThe `Parser.hs` module uses a parser combinator library (`ParserLib.hs`) to parse the tokens into an Abstract Syntax Tree (AST). The AST represents the structure of the expression, with nodes for operations and constants. Binary operators and constants are represented by the `Expression` data type from the `Grammar.hs` module.\n\nFor the input `(5 + 3) * 2`, the AST might look like this:\n\n```plaintext\n       (*)\n      /   \\\n    (+)    2\n   /   \\\n  5     3\n```\n\n### 3. Evaluation\nThe `Evaluator.hs` module is responsible for evaluating the AST, producing either an integer or a floating-point value, depending on the type of expression.\n\n- **Evaluation Functions**: \n  - `evalExpr`: The main function that takes an input string and returns the result as a custom `Val` type, which can be either `IntVal` for integers or `DubVal` for doubles.\n  - `evalExprInt` and `evalExprDub`: Utility functions that return the evaluation result as an integer or double, respectively.\n\n```haskell\nevalExpr :: String -\u003e Val\nevalExpr s = evaluate $ parse s expression\n\nevalExprInt :: String -\u003e Integer\nevalExprInt s = case evalExpr s of\n  IntVal n -\u003e n\n  DubVal n -\u003e floor n\n```\n\n- **Arithmetic Operations**: The `evaluate` function processes each node of the AST recursively, handling the following operations:\n  - Addition (`+`)\n  - Subtraction (`-`)\n  - Multiplication (`*`)\n  - Division (`/`)\n  - Modulus (`%`)\n  - Exponentiation (`^`)\n\nFor example, evaluating `5 + 3 * 2` follows these steps:\n1. Multiply `3 * 2` to get `6`.\n2. Add `5 + 6` to get `11`.\n\nThe `Evaluator.hs` module handles cases where the operations involve mixed integer and floating-point values, automatically promoting integers to floating-point numbers when necessary.\n\n### Data Structures\nThe `Grammar.hs` module defines the core data structures that represent expressions and binary operations:\n\n- **`Binop`**: This data type represents the various binary operations that the parser can handle, such as addition (`ADD`), subtraction (`SUB`), multiplication (`MUL`), division (`DIV`), exponentiation (`EXP`), and modulus (`MOD`).\n  \n  ```haskell\n  data Binop = ADD | SUB | MUL | DIV | EXP | MOD\n  ```\n\n- **`Expression`**: This data type represents expressions in the AST, which can be binary operations, constant values (integers or doubles), or negated expressions.\n  \n  ```haskell\n  data Expression = Binop_E Binop Expression Expression\n                  | ConstD_E Double\n                  | ConstI_E Integer\n                  | Neg_E Expression\n  ```\n\n- **`Val`**: This custom type is used in the `Evaluator.hs` module to represent the result of an evaluation as either an `IntVal` for integers or `DubVal` for doubles.\n\n  ```haskell\n  data Val = IntVal Integer | DubVal Double\n  ```\n\n## Extending the Parser\nTo extend the parser with more operations or functions, you can modify the `Lexer.hs`, `Token.hs`, and `Parser.hs` files to recognize new tokens and define how they should be parsed. Additionally, you can modify the `Evaluator.hs` file to support new operations during the evaluation phase.\n\nFor example, to add support for sine (`sin`), you would:\n1. Add a new token for `sin` in `Lexer.hs`.\n2. Modify the grammar in `Grammar.hs` to include `sin` as a valid operation.\n3. Update the evaluation logic in `Evaluator.hs` to handle the `sin` operation.\n\n## Contributing\nContributions are welcome! If you have ideas for improvements or additional features, feel free to fork the project and submit a pull request.\n\n1. Fork the repository.\n2. Create your feature branch (`git checkout -b feature/AmazingFeature`).\n3. Commit your changes (`git commit -m 'Add some AmazingFeature'`).\n4. Push to the branch (`git push origin feature/AmazingFeature`).\n5. Open a pull request.\n\n## License\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprogrammerstevie%2Fhaskellmather","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprogrammerstevie%2Fhaskellmather","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprogrammerstevie%2Fhaskellmather/lists"}