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§

Structs§

Enums§

  • The error type for failures while scanning, compiling or validation slac expressions.
  • An Expression is a statement which can always be evaluated to a single Value. A recursive Expression is the foundation of an AST.
  • 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§

Type Aliases§

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