#ast #语法树 #表达式 #编译器 #转换 #抽象 #

slac

“简单逻辑与算术编译器”将单个表达式转换成抽象语法树

12个重大版本

0.13.1 2024年8月9日
0.12.0 2024年5月19日
0.10.0 2024年3月17日
0.8.0 2023年10月15日
0.2.0 2023年7月30日

#939解析器实现

Download history 90/week @ 2024-05-13 51/week @ 2024-05-20 3/week @ 2024-05-27 64/week @ 2024-07-08 67/week @ 2024-07-29 133/week @ 2024-08-05 16/week @ 2024-08-12

每月下载量216

Apache-2.0

215KB
4.5K SLoC

Crates.io Version docs.rs GitHub License

SLAC:简单逻辑与算术编译器

SLAC是一个小型简单的编译器,它将单个表达式语句转换成AST。您可以使用SLAC在运行时将业务规则引擎与您的应用程序代码隔离。

它用Rust编写,因此可以轻松编译成可执行文件、wasm模块或独立DLL。

示例

库使用

use slac::{compile, Expression, Operator, Value};

fn main() {
    let ast = compile("1 * 2 + 3");

    let expected = Expression::Binary {
        left: Box::new(Expression::Binary {
            left: Box::new(Expression::Literal {
                value: Value::Number(1.0),
            }),
            right: Box::new(Expression::Literal {
                value: Value::Number(2.0),
            }),
            operator: Operator::Multiply,
        }),
        right: Box::new(Expression::Literal {
            value: Value::Number(3.0),
        }),
        operator: Operator::Plus,
    };

    assert_eq!(ast, Ok(expected));
}

解释器

SLAC内置了一个树遍历解释器。创建一个环境,其中包含变量和用户定义的函数。然后使用TreeWalkingInterpreter类执行AST对环境的操作。可选使用add_stdlib添加一些常用函数。

use slac::{compile, execute, stdlib::extend_environment, StaticEnvironment, Value};

fn main() {
    let ast = compile("max(some_var, 3) > 5").unwrap();
    let mut env = StaticEnvironment::default();
    extend_environment(&mut env);
    env.add_var("some_var", Value::Number(42.0));

    let result = execute(&env, &ast);

    assert_eq!(result, Some(Value::Boolean(true)));
}

脚本语法

脚本语法本身类似于Delphi Pascal代码。

// arithmetic operators
40 + 1 * 2 // = 42

// Integer Division and Modulo
50 div 20 mod 2 // = 2

// comparisons
50 + 50 = 100 // = True

// logical operators
True and not False // = True

// grouping
(40 + 1) * 2 // = 82

// arrays
[1, 2, 3] + ['Four'] // = [1, 2, 3, 'Four']

// application defined external functions
max(10, 20) // = 20

// application defined variables
pi * -10 // = -31,4

序列化

通过使用serde 特性标志,可以将表达式(反)序列化为各种格式,尤其是JSON。这有助于将后端的编译、验证和优化与前端执行分离。

use slac::{compile, execute, Expression, optimize, StaticEnvironment, Value};

fn main() {
    let mut input = compile("50 * 3 > 149").unwrap();
    optimize(&mut input).unwrap();
    let json = serde_json::to_value(&input).unwrap();

    // = Store the JSON in a database and load it on the client

    let output = serde_json::from_value::<Expression>(json).unwrap();
    let env = StaticEnvironment::default();

    let result = execute(&env, &output).unwrap();

    assert_eq!(input, output);
    assert_eq!(result, Value::Boolean(true));
}

安装

所需的最低Rust工具链版本是1.70.0

使用cargo add slaccrates.io安装库,作为您应用程序的依赖项。

许可证

版权所有2023 Dennis Prediger

Apache License, Version 2.0(“许可证”)下授权;除非遵守许可证,否则不得使用此文件。您可以在以下位置获得许可证副本:

https://apache.ac.cn/licenses/LICENSE-2.0

除非适用的法律要求或书面同意,否则在许可证下分发的软件是以“现状”为基础分发的,不提供任何形式的明示或暗示保证。有关许可证的具体语言规定,请参阅许可证。

依赖项

~1.4–2.5MB
~45K SLoC