A formula engine implemented in Go that parses and evaluates mathematical and logical expressions.
Formula is a Go-based expression evaluation engine that parses formulas into an Abstract Syntax Tree (AST) and evaluates them. It supports various mathematical operations, logical expressions, array functions, variable access, JSON data integration, and more.
- Mathematical operations
- Logical expressions
- Comparison operators
- Array functions
- Variable support
- JSON data integration
- Building spreadsheet-like applications
- Creating rule engines or decision systems
- Implementing custom calculation engines
- Building domain-specific languages for business rules
- Data transformation pipelines
- Calculations (Finance, Science, Statistics, Engineering, Medical, Legal, etc.)
The project includes several examples demonstrating different use cases:
- One-line expressions
- Multi-line formulas
- JSON array processing
- JSON map processing
- Compound interest calculations
This is a sample formula for Compound Interest Calculation:
MULTIPLY(
VAR('P'),
POW(
ADD(
1,
DIVIDE(
VAR('r'),
VAR('n')
)
),
MULTIPLY(
VAR('n'),
VAR('t')
)
)
)
The dump format of the formula above is like this:
=== AST Visualization ===
FuncCall(MULTIPLY)
├── FuncCall(VAR)
│ └── Literal(P)
└── FuncCall(POW)
├── FuncCall(ADD)
│ ├── Literal(1)
│ └── FuncCall(DIVIDE)
│ ├── FuncCall(VAR)
│ │ └── Literal(r)
│ └── FuncCall(VAR)
│ └── Literal(n)
└── FuncCall(MULTIPLY)
├── FuncCall(VAR)
│ └── Literal(n)
└── FuncCall(VAR)
└── Literal(t)
- Go 1.21.1 or higher
make buildmake testFor function-specific tests with coverage:
make test-functions
make test-functions-coveragecore/: Core types and AST definitionsengine/: Formula parsing and evaluation enginefunctions/: Implementation of supported functions and operatorsexamples/: Usage examples
Run make help to see all available commands:
make helpContributions are welcome! Please feel free to submit an issue or pull request.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.