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
Functiondefinitions. - optimizer
- Transformation routines to optimize an
ExpressionAST. - stdlib
- The SLAC standard library features various functions which can be included into a
StaticEnvironment.
Structs§
- Compiler
- A compiler to transform a list of
Tokensinto a single nestedExpressiontree. - Scanner
- A lexer to split a string into a list of
Tokens. - Static
Environment - An
Environmentimplementation 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
Expressionis a statement which can always be evaluated to a singleValue. A recursiveExpressionis the foundation of an AST. - Operator
- A binary or arithemtic operator.
- Token
- A
Tokenis 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
Expressionproduces aValue::Booleanresult. - check_
variables_ and_ functions - Validates
VariableandCallExpressionsby walking the AST and returning the first error. - compile
- Compiles a string into an
Expressiontree. - execute
- Executes an
Expressionusing anEnvironment. - optimize
- Transforms an
Expressiontree by applyingtransform_ternaryandfold_constantsin a loop until no further optimization is possible.