StructLayoutToolkitCalc
A safe, deterministic expression evaluator for computing bit‑accurate offsets, sizes, and field values within StructLayoutToolkit.
Overview
sltcalc provides a safe and deterministic expression evaluator used across StructLayoutToolkit to compute bit‑accurate offsets, sizes, and derived field values. It parses expressions into Python’s AST and evaluates a restricted subset of operations, ensuring predictable behavior without accessing Python globals or executing arbitrary code. The evaluator supports arithmetic, bitwise operations, conditional expressions, and user‑defined variables supplied through an isolated environment. This module enables dynamic field definitions, computed offsets, variable‑length structures, and protocol‑specific formulas while maintaining strict safety and structural consistency.
Usage
Basic
from sltcalc import SltEval
evaluator = SltEval()
result = evaluator.eval("1 + 2 * 3")
print(result) # 7
With variables
from sltcalc import SltEval
env = {
"offset": 8,
"width": 3,
"flag": True,
}
evaluator = SltEval(env)
print(evaluator.eval("offset + width * 2")) # 14
print(evaluator.eval("1 if flag else 0")) # 1
print(evaluator.eval("99 if offset >= 8 else -1")) # 99
Error behavior
from sltcalc import SltEval
evaluator = SltEval()
# NameError: undefined variable
evaluator.eval("unknown + 1")
# ValueError: unsafe function
evaluator.eval("sum(1, 2)")
Supported expressions
SltEval evaluates a restricted subset of Python expression AST nodes.
Literals
- Numeric constants (for example: 0, 42, 3.5)
- Boolean constants (True, False)
- List literals (for example: [1, 2, 3])
- Tuple literals (for example: (1, 2, 3))
- Dict literals (for example: {"a": 1, "b": 2})
Variables
- Variable lookup from the evaluator environment (SltEval(env))
Binary operators
- Arithmetic: +, -, *, /, //, %, **
- Bitwise: &, |, ^, <<, >>
Unary operators
- Unary plus: +x
- Unary minus: -x
- Boolean negation: not x
Boolean operators
- and
- or
Comparisons
- ==, !=, <, <=, >, >=, is, is not, in, not in
- Chained comparisons are supported (for example: 1 < 2 < 3)
Conditional expression
- Ternary expression: a if condition else b
Subscript access
- Index/key access (for example: arr[0], mapping["key"])
- Nested access is supported (for example: access key "items" and then index 0)
Function calls
- Built-in allowlist: abs(...), all(...), any(...), len(...), min(...), max(...), round(...), sorted(...), sum(...)
- Callables passed through environment are allowed (for example: SltEval({"triple": triple}) then triple(4))
Not supported
- Attribute access (for example: obj.value)
- Set literals in expressions
- Non-name call targets (for example: (lambda x: x)(1))
Unsupported constructs raise ValueError with an Unsupported expression message.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sltcalc-0.1.0.tar.gz.
File metadata
- Download URL: sltcalc-0.1.0.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
599dc33cf91d18dd257489989065debf5d20d28c299d95f586b371cc86af1b47
|
|
| MD5 |
1d76f047035911260a234a74278341ad
|
|
| BLAKE2b-256 |
a2162a988c23ac06d05c82e5555e0fb221a8e00786539ebe2826365dcb4f3abe
|
File details
Details for the file sltcalc-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sltcalc-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20f06501a583aec62c670e48b4d21eba02a42997461bd8f85640055092d710fa
|
|
| MD5 |
34e70d39c4806724bd71d3501e3073d3
|
|
| BLAKE2b-256 |
8a45c6f7d8074f1fb743e3902d71d8c028e9edeb032834639fdb72df213fdd11
|