Crate ratslang

Crate ratslang 

Source
Expand description

§Ratslang

A compact configuration language for physical systems with native support for time and length units.

Ratslang solves the unit conversion problem that plagues physical system configuration, while keeping the implementation simple and declarative.

§Features

  • Time units: ms, s, min/mins, hour/hours
  • Length units: mm, cm, m
  • Ranges: 1mm..100m, 5s.., ..10m, ..
  • Namespaces: Dot notation (sensor.range) or blocks
  • Includes: Compose configuration files with namespace scoping
  • Types: Booleans, integers, floats, strings, paths, arrays

§Example

use ratslang::compile_code;

let source = r#"
    distance = 100mm..2m
    scan_rate = 10.5s
     
    sensor {
        type = Lidar
        enabled = true
    }
"#;

let result = compile_code(source).unwrap();
let distance = result.vars.resolve("distance").unwrap();

§Usage

Compile and resolve variables:

use ratslang::{compile_code, Rhs, Val, NumVal};

let code = r#"
    timeout = 5000
    sensor.type = Lidar
    sensor.range = 10
"#;

let ast = compile_code(code).unwrap();

// Manual resolution
let timeout = ast.vars.resolve("timeout").unwrap().unwrap();
assert_eq!(timeout, Rhs::Val(Val::NumVal(NumVal::Integer(5000))));

let name = ast.vars.resolve("sensor.type").unwrap().unwrap();
assert_eq!(name, Rhs::Val(Val::StringVal("Lidar".to_string())));

// Filter namespace
let sensor = ast.vars.filter_ns(&["sensor"]);
let range = sensor.resolve("range").unwrap().unwrap();
assert_eq!(range, Rhs::Val(Val::NumVal(NumVal::Integer(10))));

Note: Ratslang is deliberately simple—no arithmetic, loops, or conditionals.

§Syntax Highlighting

Syntax highlighting is available with this tree-sitter grammar or this VS Code extension.

Macros§

resolve_bool
Resolves a boolean value from user or default configuration.
resolve_float
Resolves a floating-point value from user or default configuration.
resolve_float_force_range
Resolves a numeric range, converting integers to floats if needed.
resolve_float_range
Resolves a floating-point range without implicit integer conversion.
resolve_int
Resolves an integer value from user or default configuration.
resolve_int_range
Resolves an integer range without type conversion.
resolve_length_range_meters_float
Resolves a length range from millimeters to meters as f32.
resolve_path
Resolves a path value from user or default configuration.
resolve_string
Resolves a string value from user or default configuration.
resolve_time_range_seconds_float
Resolves a time range from milliseconds to seconds as f32.
resolve_var
Resolves a variable from user or default configuration.

Structs§

Evaluated
The result of compiling and evaluating ratslang code.
Root
The root of the abstract syntax tree.
UnitVal
A numeric value paired with a physical unit.
VariableHistory
Tracks the history of variable definitions and provides resolution capabilities.

Enums§

NumVal
Represents a numeric value that can be either a floating-point or integer.
Operator
Rhs
Right-hand side of an assignment, representing various expression types.
Statement
A statement in the language (currently only variable assignments).
StatementKind
StatementKindOwned
StatementKindOwnedPass1
StatementKindPass1
StatementPass1
Unit
Physical units supported by the language.
Val
A value in the language, which can be a number (with or without units), string, or boolean.
Var
A variable reference with optional namespace qualification.

Functions§

compile_code
Compiles ratslang source code from a string.
compile_code_with_state
Compiles ratslang source code with existing variable state.
compile_file
Compiles a ratslang source file.
compile_file_with_state
Compiles a ratslang source file with existing variable state.