Crate slac

Source
Expand description

The Simple Logic & Arithmetic Compiler is a library to convert an single expression statement into a structured Expression abstract syntax tree.

The AST can be validated, (de)serialized, and executed using the built-in interpreter.

§Example

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

let ast = compile("max(10, 20) + 1").expect("compiles the ast");
let mut env = StaticEnvironment::default();

extend_environment(&mut env);
check_variables_and_functions(&env, &ast).expect("find the usage of max");

let result = execute(&env, &ast).expect("execute the expression");
assert_eq!(Value::Number(21.0), result);

§Serialization / Deserialization

The Expression can be fully serialized into an (e.g.) JSON string for precompilation and cached execution using serde. See test/serde_test.rs for the resulting JSON.

Modules§

environment
Dynamic variables and function calls can be provided by an Environment.
function
Wrapper structs for native Function definitions.
optimizer
Transformation routines to optimize an Expression AST.
stdlib
The SLAC standard library features various functions which can be included into a StaticEnvironment.

Structs§

Compiler
A compiler to transform a list of Tokens into a single nested Expression tree.
Scanner
A lexer to split a string into a list of Tokens.
StaticEnvironment
An Environment implementation in which all variables and functions are known ahead of execution. All variable and function names treated as case-insensitive.

Enums§

Error
The error type for failures while scanning, compiling or validation slac expressions.
Expression
An Expression is a statement which can always be evaluated to a single Value. A recursive Expression is the foundation of an AST.
Operator
A binary or arithemtic operator.
Token
A Token is the smallest logical unit evaluated by the compiler.
Value
A Wrapper for the four different possible variable types.

Functions§

check_boolean_result
Checks if the top level Expression produces a Value::Boolean result.
check_variables_and_functions
Validates Variable and Call Expressions by walking the AST and returning the first error.
compile
Compiles a string into an Expression tree.
execute
Executes an Expression using an Environment.
optimize
Transforms an Expression tree by applying transform_ternary and fold_constants in a loop until no further optimization is possible.

Type Aliases§

Result
A specialized Result type for Errors during the scanning, compiling or validation phase.