Fork me on GitHub

YACEP v0.2.0 发布,轻量级高性能表达式解析器

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相关技术工具服务技巧(上)

posted @ 2019-05-28 10:27  VAllen  阅读(405)  评论(0编辑  收藏  举报