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.
- Variable
History - 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).
- Statement
Kind - Statement
Kind Owned - Statement
Kind Owned Pass1 - Statement
Kind Pass1 - Statement
Pass1 - 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.