A Haskell expression parser and evaluator that supports JavaScript-like expressions with proper operator precedence and evaluation.
- 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
- GHC (Glasgow Haskell Compiler)
- Cabal build system
cabal buildcabal testParse 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.0Arithmetic:
2 + 3 * 4 // Proper precedence: 2 + (3 * 4)
(2 + 3) * 4 // Parentheses override precedenceLogical:
true && false || true // Left-associative: (true && false) || true
!false // Unary negationComparison:
x > 5 && y <= 10 // Comparison with logical operators
a == b || c != d // Equality checksProperty Access:
obj.property // Object property access
arr[0] // Array indexing
obj.nested.prop // Chained property accessTernary Operator:
condition ? trueValue : falseValueArrays:
[1, 2, 3, "hello"] // Mixed-type arraysArrow Functions:
(x) => x * 2 // Single parameter (parentheses required)
(x, y) => x + y // Multiple parameters
() => 42 // No parametersFunction Invocation:
func(arg1, arg2) // Function calls with argumentsArray 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 functionThe parser follows this operator precedence (lowest to highest):
- Ternary (
?:) - Logical OR (
||) - Logical AND (
&&) - Equality (
==,!=) - Relational (
<,<=,>,>=) - Addition/Subtraction (
+,-) - Multiplication/Division (
*,/) - Unary (
!,-) - Member access (
.) - Primary expressions (literals, identifiers, parentheses)
- 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
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
# Run all tests
cabal test
# Run tests matching a pattern
cabal test --test-options='-p "/precedence/"'
# Start interactive session
cabal replThe 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
- 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
- General function invocation (only array methods currently supported)