Skip to content

Yuki-bun/haskell-parser

Repository files navigation

Haskell Excel Machine

A Haskell expression parser and evaluator that supports JavaScript-like expressions with proper operator precedence and evaluation.

Features

  • Complete expression parsing with recursive descent parser
  • Runtime evaluation of parsed expressions
  • Comprehensive operator support: arithmetic (+, -, *, /), logical (&&, ||, !), comparison (==, !=, <, <=, >, >=)
  • Multiple data types: strings, numbers, booleans, null, undefined, objects, arrays
  • Advanced expressions: ternary operator (?:), property access (.), array indexing ([]), arrow functions (=>), function invocation
  • Proper operator precedence following JavaScript semantics
  • Modular architecture with separate parsing and evaluation phases

Installation

Prerequisites

  • GHC (Glasgow Haskell Compiler)
  • Cabal build system

Build

cabal build

Run Tests

cabal test

Usage

Command Line

Parse and display the AST structure of an expression:

cabal run Main -- "2 + 3 * 4"
# Output: 
# Binary: +
#   Literal: 2.0
#   Binary: *
#     Literal: 3.0
#     Literal: 4.0

Expression Examples

Arithmetic:

2 + 3 * 4          // Proper precedence: 2 + (3 * 4)
(2 + 3) * 4        // Parentheses override precedence

Logical:

true && false || true     // Left-associative: (true && false) || true
!false                    // Unary negation

Comparison:

x > 5 && y <= 10         // Comparison with logical operators
a == b || c != d         // Equality checks

Property Access:

obj.property             // Object property access
arr[0]                   // Array indexing
obj.nested.prop          // Chained property access

Ternary Operator:

condition ? trueValue : falseValue

Arrays:

[1, 2, 3, "hello"]       // Mixed-type arrays

Arrow Functions:

(x) => x * 2             // Single parameter (parentheses required)
(x, y) => x + y          // Multiple parameters
() => 42                 // No parameters

Function Invocation:

func(arg1, arg2)         // Function calls with arguments

Array Methods:

arr.length()             // Get array length
arr.map((x) => x * 2)    // Transform each element
arr.filter((x) => x > 5) // Filter elements by condition
numbers.map(() => 42)    // Map with no-argument arrow function

Supported Grammar

The parser follows this operator precedence (lowest to highest):

  1. Ternary (?:)
  2. Logical OR (||)
  3. Logical AND (&&)
  4. Equality (==, !=)
  5. Relational (<, <=, >, >=)
  6. Addition/Subtraction (+, -)
  7. Multiplication/Division (*, /)
  8. Unary (!, -)
  9. Member access (.)
  10. Primary expressions (literals, identifiers, parentheses)

Architecture

Core Components

  • AST (src/AST.hs) - Abstract syntax tree definitions
  • Parser (src/Parser/) - Modular parser combinators and expression parsing
  • Evaluator (src/Eval/Evaluate.hs) - Runtime evaluation of parsed expressions

Module Structure

Main.hs                    # Command-line interface
├── Parser.Expression      # Main expression parser
│   ├── AST               # Abstract syntax tree
│   ├── Parser.Combinators # Parser building blocks
│   └── Parser.Types      # Core parser infrastructure
└── Eval.Evaluate         # Expression evaluator
    └── AST               # AST dependency

Development

Running Specific Tests

# Run all tests
cabal test

# Run tests matching a pattern
cabal test --test-options='-p "/precedence/"'

# Start interactive session
cabal repl

Test Coverage

The project includes 160 comprehensive tests covering:

  • All literal types and operators
  • Operator precedence and associativity
  • Complex nested expressions
  • Array method invocation (map, filter, length)
  • Arrow function evaluation within array methods
  • Error handling for malformed input and type errors

Current Status

✅ Implemented

  • Complete expression parsing (including arrow functions and function invocation)
  • All basic operators and data types
  • Property access and array indexing
  • Ternary operators
  • Runtime evaluation for all expressions
  • Arrow function evaluation (fully supported)
  • Array methods: length(), map(), filter()
  • Function invocation for array methods

🚧 In Progress

  • General function invocation (only array methods currently supported)

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors