mirror of
https://github.com/rizinorg/rizin
synced 2026-08-22 20:26:16 -04:00
Replace the hand-written parser in calc.c with a tree-sitter grammar (subprojects/rizin-math-parser) and a typed evaluator. The old parser could only ever produce a ut64 and folded anything it failed to read to 0, which left callers unable to tell a failed expression from one that evaluated to zero. Expressions now evaluate to an RzNumValue, a tagged union over ut64, double, RzBitVector, arbitrary-precision integer and arbitrary-precision decimal, carrying an RzNumError rather than signalling failure as 0. Literals keep the width they were written with (5u8, 0xffu128, any width from 1 to 65536), results that outgrow 64 bits promote to a big number on their own, and a parse error, division by zero or unresolved identifier reaches the caller. rz_num_math() is deprecated. rz_num_math_ut64() keeps its exact behaviour for callers that want a ut64, and rz_num_math_value() exposes the typed result. rz_core_math() adds the RzCore-backed form used by the % command, with rz_core_math_ut64() deprecated alongside it. rz-ax routes through the typed API, so it prints values at full precision, reports errors on stderr and exits non-zero. rz_il_lift_num() converts an expression to an RzILOpPure, so a numeric argument can be lifted instead of pre-evaluated. Legacy input still works: trailing base suffixes (101b, 35o, 212t), the trailing-'h' hex form and the k/m/g scale suffixes are all accepted and warn once, pointing at the 0b/0o/0t prefixes. doc/math.md documents the language and doc/math-il-lift.md the lift; the grammar, the evaluator, rz-ax and the % command are covered by unit and db tests.
31 lines
671 B
JSON
31 lines
671 B
JSON
{
|
|
"$schema": "https://tree-sitter.github.io/tree-sitter/assets/schemas/config.schema.json",
|
|
"grammars": [
|
|
{
|
|
"name": "rznum",
|
|
"camelcase": "Rznum",
|
|
"scope": "source.rznum",
|
|
"path": ".",
|
|
"file-types": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"version": "0.1.0",
|
|
"license": "LGPL-3.0-only",
|
|
"description": "Tree-sitter grammar for the Rizin RzNum mathematical expression language.",
|
|
"authors": [
|
|
{
|
|
"name": "Anton Kochkov",
|
|
"email": "anton.kochkov@gmail.com"
|
|
}
|
|
]
|
|
},
|
|
"bindings": {
|
|
"c": true,
|
|
"go": false,
|
|
"node": false,
|
|
"python": false,
|
|
"rust": false,
|
|
"swift": false
|
|
}
|
|
}
|