YACEP
: yet another csharp expression parser
YACEP
是一款基於netstandard2.0
構建的輕量級高性能表達式解析器
特性:
- 開箱即用,內置了一系列的字面值, 一元及二元操作符以及內置的統計類與時間類函數可滿足大部分使用場景
- 跨平臺,基於netstandard2.0標準構建
- 輕量級,只有500多行代碼實現的輕量級詞法分析器
- 低消耗,詞法分析器使用 ReadOnlySpan 解析字符串
- 高性能,使用EMIT命名空間生成IL來構建可執行對象(查看基準測試報告)
- 支持條件表達式
- 支持索引器
- 支持 in 表達式
- 支持自定義字面量
- 支持自定義一元操作符
- 支持自定義二元操作符
- 支持自定義函數
樣例代碼 :
var state = new
{
x = 7,
y = 43.0f,
z = new Dictionary<string, string>
{
["yacep"] = "yet another csharp expression parser",
["tupac-amaru"] = "was the last indigenous monarch (Sapa Inca) of the Neo-Inca State"
},
rand = new Func<object>(() => new Random().Next(1, 3)),
array = Enumerable.Range(1971, 1996 - 1971)
};
var expr = "x + y - z['yacep'].Length + max([1, 2, 3]) + (this.rand() > 2 ? 1971 : 1996) - len(array)";
var evaluator = expr.Compile();
var value = evaluator.EvaluateAs<decimal>(state);
PS: 推薦接地氣的YACEP文章:YACEP相關技術工具服務技巧(上)