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§
- Dynamic variables and function calls can be provided by an
Environment
. - Transformation routines to optimize an
Expression
AST. - The SLAC standard library features various functions which can be included into a
StaticEnvironment
.
Structs§
- A compiler to transform a list of
Tokens
into a single nestedExpression
tree. - A lexer to split a string into a list of
Tokens
. - An
Environment
implementation in which all variables and functions are known ahead of execution. All variable and function names treated as case-insensitive.
Enums§
- The error type for failures while scanning, compiling or validation slac expressions.
- A binary or arithemtic operator.
- A
Token
is the smallest logical unit evaluated by the compiler. - A Wrapper for the four different possible variable types.
Functions§
- Checks if the top level
Expression
produces aValue::Boolean
result. - Compiles a string into an
Expression
tree. - Executes an
Expression
using anEnvironment
. - Transforms an
Expression
tree by applyingtransform_ternary
andfold_constants
in a loop until no further optimization is possible.