Albatross.Expression
4.0.0-248.main
Prefix Reserved
dotnet add package Albatross.Expression --version 4.0.0-248.main
NuGet\Install-Package Albatross.Expression -Version 4.0.0-248.main
<PackageReference Include="Albatross.Expression" Version="4.0.0-248.main" />
<PackageVersion Include="Albatross.Expression" Version="4.0.0-248.main" />
<PackageReference Include="Albatross.Expression" />
paket add Albatross.Expression --version 4.0.0-248.main
#r "nuget: Albatross.Expression, 4.0.0-248.main"
#:package Albatross.Expression@4.0.0-248.main
#addin nuget:?package=Albatross.Expression&version=4.0.0-248.main&prerelease
#tool nuget:?package=Albatross.Expression&version=4.0.0-248.main&prerelease
About
Albatross.Expression is a powerful .NET expression parsing and evaluation library that processes and evaluates text-based expression strings. The library tokenizes expression text, creates a tree model from the tokens, and can evaluate expressions or convert them to different formats. It includes a comprehensive ExecutionContext class that allows evaluation of expressions with variables that can be read internally or directly from external objects.
Features
- Expression Parsing: Tokenize and parse complex mathematical and logical expressions
- Multiple Operation Types: Support for infix, prefix, and unary operations
- Variable Support: Evaluate expressions with variables using ExecutionContext
- External Data Integration: Access external data sources during expression evaluation
- Asynchronous Evaluation: Support for async operations within expressions
- Type Conversion: Built-in support for string, boolean, and numeric literals
- Rich Function Library: Comprehensive set of built-in functions including:
- Mathematical operations (Add, Subtract, Multiply, Divide, Power, Floor, Round, etc.)
- String operations (Concat, Left, Right, Upper, Lower, PadLeft, PadRight, etc.)
- Date/Time operations (Now, Today, Year, Month, DayOfWeek, etc.)
- Logical operations (And, Or, Not, comparison operators)
- Array operations (Array creation, GetJsonArrayItem)
- Regex operations (RegexCapture)
- Utility functions (Format, If, Random, etc.)
- Command Line Utility: CLI tool for expression evaluation and variable management
Prerequisites
- .NET 8.0 SDK or later
- C# 12.0 language features support
Installation
NuGet Package
Install the main library via NuGet Package Manager:
dotnet add package Albatross.Expression
Or via Package Manager Console:
Install-Package Albatross.Expression
CLI Tool
Install the command-line utility globally:
dotnet tool install -g Albatross.Expression.Utility
Build from Source
git clone https://github.com/RushuiGuan/expression.git
cd expression
dotnet restore
dotnet build
Usage Examples
Basic Expression Evaluation
using Albatross.Expression.Parsing;
// Create parser instance
var parser = new ParserBuilder().BuildDefault();
// Evaluate simple expressions
var result1 = parser.Eval("1 + 5", null); // Returns: 6
var result2 = parser.Eval("10 * (2 + 3)", null); // Returns: 50
var result3 = parser.Eval("upper('hello world')", null); // Returns: "HELLO WORLD"
Using ExecutionContext with Variables
using Albatross.Expression.Context;
using Albatross.Expression.Parsing;
var parser = new ParserBuilder().BuildDefault();
var context = new DefaultExecutionContext<object>(parser);
// Set variables
context.Set(new ExpressionContextValue<object>("a", "10", parser));
context.Set(new ExpressionContextValue<object>("b", "20", parser));
context.Set(new ExpressionContextValue<object>("sum", "a + b", parser));
// Evaluate expression with variables
var result = context.GetValue("sum", new object()); // Returns: 30
External Data Integration
var parser = new ParserBuilder().BuildDefault();
var context = new DefaultExecutionContext<Dictionary<string, object>>(parser);
// Set up expression that references external data
context.Set(new ExpressionContextValue<Dictionary<string, object>>("total", "price + tax", parser));
context.Set(new ExternalContextValue<Dictionary<string, object>>("price", dict => dict["price"]));
context.Set(new ExternalContextValue<Dictionary<string, object>>("tax", dict => dict["tax"]));
// Provide external data
var data = new Dictionary<string, object> { { "price", 100.0 }, { "tax", 8.5 } };
var result = context.GetValue("total", data); // Returns: 108.5
Asynchronous Operations
var parser = new ParserBuilder().BuildDefault();
var context = new DefaultExecutionContext<object>(parser);
// Set up async external value
context.Set(new AsyncExternalContextValue<object>("api_data", async _ => await FetchDataAsync()));
context.Set(new ExpressionContextValue<object>("result", "upper(api_data)", parser));
// Evaluate asynchronously
var result = await context.GetValueAsync("result", new object());
Command Line Usage
After installing the CLI tool:
# Evaluate expressions
ex eval "2 + 3 * 4"
# Set variables
ex set -n myvar -v "10 + 5"
# List all variables
ex list
# Evaluate with variables
ex eval "myvar * 2"
Project Structure
├── Albatross.Expression/ # Main expression library
│ ├── Context/ # ExecutionContext implementations
│ ├── Exceptions/ # Custom exception classes
│ ├── Infix/ # Infix operations (+, -, *, /, etc.)
│ ├── Nodes/ # Expression tree node types
│ ├── Parsing/ # Core parsing logic
│ ├── Prefix/ # Prefix functions (if, max, concat, etc.)
│ └── Unary/ # Unary operations (-, +)
├── Albatross.Expression.Test/ # Unit tests
└── Albatross.Expression.Utility/ # CLI utility tool
Running Tests
To run the unit tests:
# Run all tests
dotnet test
# Run tests with detailed output
dotnet test --logger "console;verbosity=detailed"
# Run specific test project
dotnet test ./Albatross.Expression.Test/
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add or update tests as needed
- Ensure all tests pass
- Submit a pull request
Documentation
For Api Reference, visit: https://rushuiguan.github.io/expression/
License
This project is licensed under the MIT License - see the LICENSE file for details.
Copyright (c) 2017 Rushui Guan
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Albatross.Dates (>= 7.5.12)
- JsonPointer.Net (>= 5.3.1)
- System.Text.Json (>= 8.0.6)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Albatross.Expression:
Package | Downloads |
---|---|
DnDGen.RollGen
This gives a basic, fluid interface for rolling dice from the D20 system - commonly known to work with Dungeons and Dragons. Die included are d2, d3, d4, d6, d8, d10, d12, d20, Percentile (d100), and custom die rolls. |
|
Albatross.Text.CliFormat
A library that prints text output based on the runtime format expression |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
4.0.0-248.main | 39 | 9/2/2025 |
4.0.0-243.main | 34 | 8/27/2025 |
4.0.0-229.main | 25 | 8/27/2025 |
4.0.0-225.main | 29 | 8/26/2025 |
3.0.13 | 39,233 | 5/24/2023 |
3.0.12 | 800 | 5/24/2023 |
3.0.11 | 34,699 | 4/19/2022 |
3.0.8 | 1,102 | 3/23/2022 |
3.0.5 | 1,110 | 3/15/2022 |
3.0.3 | 4,170 | 3/24/2021 |
3.0.2 | 2,829 | 1/8/2021 |
3.0.0 | 1,179 | 12/22/2020 |
2.0.4 | 24,161 | 11/25/2019 |
2.0.3 | 16,028 | 5/4/2018 |
2.0.2 | 2,026 | 4/16/2018 |
1.3.6218.36673 | 9,813 | 1/10/2017 |