rizin/test/db/cmd/cmd_math
Anton Kochkov 689bafb7a4
Rewrite the RzNum parser and calculator on tree-sitter (#4326)
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.
2026-07-24 02:57:27 +08:00

952 lines
13 KiB
Text

FILE=malloc://1024
NAME=%vi-1
CMDS=<<EOF
%vi 0xffffffff
%vi8 0xffffffff
%vi4 0xffffffff
%vi2 0xffffffff
%vi1 0xffffffff
%vi4 0xffffffff
%vi2 0xffff
%vi1 0xff
EOF
EXPECT=<<EOF
4294967295
4294967295
-1
-1
-1
-1
-1
-1
EOF
RUN
FILE=malloc://1024
NAME=%vi-8
CMDS=<<EOF
%vi 0xfffffff8
%vi4 0xfffffff8
%vi2 0xfffffff8
%vi1 0xfffffff8
%vi4 0xfffffff8
%vi2 0xfff8
%vi1 0xf8
EOF
EXPECT=<<EOF
4294967288
-8
-8
-8
-8
-8
-8
EOF
RUN
FILE=malloc://1024
CMDS=<<EOF
%v
%p
EOF
EXPECT=<<EOF
0x0
0x00000000
EOF
RUN
NAME=%v
FILE=--
CMDS=<<EOF
%v 1024
%v 0x42
EOF
EXPECT=<<EOF
0x400
0x42
EOF
RUN
NAME="%v 'A'"
# BROKEN: char literals ('X') were dropped from the new grammar, which has
# no char-literal rule, so a quoted character no longer evaluates to its code
BROKEN=1
FILE=--
CMDS=%v "'A'"
EXPECT=<<EOF
0x41
EOF
RUN
NAME="%v 'A'+3"
# BROKEN: char literals ('X') were dropped from the new grammar, which has
# no char-literal rule, so a quoted character no longer evaluates to its code
BROKEN=1
FILE=--
CMDS=%v "'A'+3"
EXPECT=<<EOF
0x44
EOF
RUN
NAME="%v 3+'A'-3"
# BROKEN: char literals ('X') were dropped from the new grammar, which has
# no char-literal rule, so a quoted character no longer evaluates to its code
BROKEN=1
FILE=--
CMDS=%v "3+'A'-3"
EXPECT=<<EOF
0x41
EOF
RUN
NAME="%v 33^'A'"
# BROKEN: char literals ('X') were dropped from the new grammar, which has
# no char-literal rule, so a quoted character no longer evaluates to its code
BROKEN=1
FILE=--
CMDS=%v "32^'A'"
EXPECT=<<EOF
0x61
EOF
RUN
NAME="% 1;%v"
FILE=--
CMDS=% 1;%v
EXPECT=<<EOF
int32 1
uint32 1
hex 0x1
octal 01
unit 1
segment 0000:0001
string "\x01"
fvalue 1.0
float 1.000000f
double 1.000000
binary 0b00000001
trits 0t1
0x1
EOF
RUN
NAME="% 0;%v"
FILE=--
CMDS=% 0;%v
EXPECT=<<EOF
int32 0
uint32 0
hex 0x0
octal 00
unit 0
segment 0000:0000
string "\0"
fvalue 0.0
float 0.000000f
double 0.000000
binary 0b00000000
trits 0t0
0x0
EOF
RUN
NAME="% -1"
FILE=--
CMDS=% -1
EXPECT=<<EOF
int64 -1
uint64 18446744073709551615
hex 0xffffffffffffffff
octal 01777777777777777777777
unit 16E
segment fffff000:0fff
string "\xff\xff\xff\xff\xff\xff\xff\xff"
fvalue -1.0
float -1.000000f
double -1.000000
binary 0b1111111111111111111111111111111111111111111111111111111111111111
trits 0t11112220022122120101211020120210210211220
EOF
RUN
NAME="% 2*1.5"
# BROKEN: a fractional result is now an exact RzBigDecimal ('decimal N'),
# not a 64-bit double, so the old 'float'/'hex' (double bit-pattern) lines differ
BROKEN=1
FILE=--
CMDS=% 2*1.5
EXPECT=<<EOF
float 3
scifmt 3
hex 0x4008000000000000
EOF
RUN
NAME="% 2**3"
FILE=--
CMDS=% 2**3
EXPECT=<<EOF
int32 8
uint32 8
hex 0x8
octal 010
unit 8
segment 0000:0008
string "\b"
fvalue 8.0
float 8.000000f
double 8.000000
binary 0b00001000
trits 0t22
EOF
RUN
NAME="% -25**3"
FILE=--
CMDS=% -25**3
EXPECT=<<EOF
int64 -15625
uint64 18446744073709535991
hex 0xffffffffffffc2f7
octal 01777777777777777741367
unit 16.0E
segment fffff000:02f7
string "\xf7\xc2\xff\xff\xff\xff\xff\xff"
fvalue -15625.0
float -15625.000000f
double -15625.000000
binary 0b1111111111111111111111111111111111111111111111111100001011110111
trits 0t11112220022122120101211020120210000102020
EOF
RUN
NAME="% -76**2"
FILE=--
CMDS=% -76**2
EXPECT=<<EOF
int32 5776
uint32 5776
hex 0x1690
octal 013220
unit 5.6K
segment 0000:0690
string "\x90\x16"
fvalue 5776.0
float 5776.000000f
double 5776.000000
binary 0b0001011010010000
trits 0t21220221
EOF
RUN
NAME="% 10**-2"
FILE=--
CMDS=% 10**-2
EXPECT=<<EOF
float 0.01
scifmt 0.01
hex 0x3f847ae147ae147b
EOF
RUN
NAME="% 1024**0"
FILE=--
CMDS=% 1024**0
EXPECT=<<EOF
int32 1
uint32 1
hex 0x1
octal 01
unit 1
segment 0000:0001
string "\x01"
fvalue 1.0
float 1.000000f
double 1.000000
binary 0b00000001
trits 0t1
EOF
RUN
NAME="% 2**4.5"
FILE=--
CMDS=% 2**4.5
EXPECT=<<EOF
float 22.6274
scifmt 22.627416997969522
hex 0x4036a09e667f3bcd
EOF
RUN
NAME="% 10 ** 2 * 3"
FILE=--
CMDS=% 10 ** 2 * 3
EXPECT=<<EOF
int32 300
uint32 300
hex 0x12c
octal 0454
unit 300
segment 0000:012c
string ",\x01"
fvalue 300.0
float 300.000000f
double 300.000000
binary 0b0000000100101100
trits 0t102010
EOF
RUN
NAME="% 10 ** 2 / 3"
FILE=--
CMDS=% 10 ** 2 / 3
EXPECT=<<EOF
int32 33
uint32 33
hex 0x21
octal 041
unit 33
segment 0000:0021
string "!"
fvalue 33.0
float 33.000000f
double 33.000000
binary 0b00100001
trits 0t1020
EOF
RUN
NAME="% 10 ** 2 + 3"
FILE=--
CMDS=% 10 ** 2 + 3
EXPECT=<<EOF
int32 103
uint32 103
hex 0x67
octal 0147
unit 103
segment 0000:0067
string "g"
fvalue 103.0
float 103.000000f
double 103.000000
binary 0b01100111
trits 0t10211
EOF
RUN
NAME="% 10 - 2 ** 3"
FILE=--
CMDS=% 10 - 2 ** 3
EXPECT=<<EOF
int32 2
uint32 2
hex 0x2
octal 02
unit 2
segment 0000:0002
string "\x02"
fvalue 2.0
float 2.000000f
double 2.000000
binary 0b00000010
trits 0t2
EOF
RUN
NAME="% 2**-1+4"
FILE=--
CMDS=% 2**-1+4
EXPECT=<<EOF
float 4.5
scifmt 4.5
hex 0x4012000000000000
EOF
RUN
NAME="math help"
FILE==
CMDS=<<EOF
%?~Usage
EOF
EXPECT=<<EOF
Usage: %[?] # Math commands
EOF
RUN
NAME=expr with parentheses
FILE==
CMDS=<<EOF
%v (0x10000192c + 4294965314) & 0xffffffff
EOF
EXPECT=<<EOF
0x116e
EOF
RUN
NAME=binary shift
FILE==
CMDS=<<EOF
%b 1<<1
%b 1 << 1
%b 1 << 2
EOF
EXPECT=<<EOF
10b
10b
100b
EOF
RUN
NAME="comparison operators"
FILE==
CMDS=<<EOF
%v "1<1"
%v "1<2"
%v "2<1"
%v "1>1"
%v "1>2"
%v "2>1"
EOF
EXPECT=<<EOF
0x0
0x1
0x0
0x0
0x0
0x1
EOF
RUN
NAME=%p and %P ignore io.va
FILE=bins/elf/analysis/ls2
CMDS=<<EOF
%p main @e:io.va=true
%p main @e:io.va=false
%v main
%P 0x28a0 @e:io.va=1
%P 0x28a0 @e:io.va=0
EOF
EXPECT=<<EOF
0x000028a0
0x000028a0
0x4028a0
0x004028a0
0x004028a0
EOF
RUN
NAME=djb2 hash
FILE==
CMDS=<<EOF
%h 1024
%h ILoveRizin
%h RizinLovesMe2
EOF
EXPECT=<<EOF
0x50489162
0x56b7215a
0xe859883a
EOF
RUN
FILE=malloc://1024
NAME=% bignum literal
CMDS=<<EOF
% 0x10000000000000000
EOF
EXPECT=<<EOF
decimal 18446744073709551616
hex 0x10000000000000000
width 68 bits (approx)
EOF
RUN
FILE=malloc://1024
NAME=% exact integer power
CMDS=<<EOF
% 2 ** 100
EOF
EXPECT=<<EOF
decimal 1267650600228229401496703205376
hex 0x10000000000000000000000000
width 104 bits (approx)
EOF
RUN
FILE=malloc://1024
NAME=% float power
CMDS=<<EOF
% 3.14159 ** 2
EOF
EXPECT=<<EOF
float 9.86959
scifmt 9.8695877280999991
hex 0x4023bd3a9a4a6287
EOF
RUN
FILE=malloc://1024
NAME=% builtin min max
CMDS=<<EOF
% min(0x100, max(0x50, 0x80))
EOF
EXPECT=<<EOF
int32 128
uint32 128
hex 0x80
octal 0200
unit 128
segment 0000:0080
string "\x80"
fvalue 128.0
float 128.000000f
double 128.000000
binary 0b10000000
trits 0t11202
EOF
RUN
FILE=malloc://1024
NAME=% unicode function minimum
CMDS=<<EOF
% минимум(0x100, 0x200)
EOF
EXPECT=<<EOF
int32 256
uint32 256
hex 0x100
octal 0400
unit 256
segment 0000:0100
string "\x01"
fvalue 256.0
float 256.000000f
double 256.000000
binary 0b0000000100000000
trits 0t100111
EOF
RUN
FILE=malloc://1024
NAME=% let binding reassignment
CMDS=<<EOF
% (c = 1) + (c = 9) + c
EOF
EXPECT=<<EOF
int32 19
uint32 19
hex 0x13
octal 023
unit 19
segment 0000:0013
string "\x13"
fvalue 19.0
float 19.000000f
double 19.000000
binary 0b00010011
trits 0t201
EOF
RUN
FILE=malloc://1024
NAME=% crc32 default
CMDS=<<EOF
w AAAABBBBCCCCDDDD
% crc(0, 16)
EOF
EXPECT=<<EOF
int32 1501082128
uint32 1501082128
hex 0x5978b210
octal 013136131020
unit 1.4G
segment 5978000:0210
string "\x10\xb2xY"
fvalue 1501082128.0
float 1501082112.000000f
double 1501082128.000000
binary 0b01011001011110001011001000010000
trits 0t10212121112212120101
EOF
RUN
FILE=malloc://1024
NAME=% crc width 16 selector
CMDS=<<EOF
w AAAABBBBCCCCDDDD
% crc(0, 16, 16)
EOF
EXPECT=<<EOF
int32 26619
uint32 26619
hex 0x67fb
octal 063773
unit 26.0K
segment 0000:07fb
string "\xfbg"
fvalue 26619.0
float 26619.000000f
double 26619.000000
binary 0b0110011111111011
trits 0t1100111220
EOF
RUN
FILE=malloc://1024
NAME=% md5 digest
CMDS=<<EOF
w AAAABBBBCCCCDDDD
% md5(0, 16)
EOF
EXPECT=<<EOF
decimal 196160137941607695467225652873056001787
hex 0x9393117fc22175732ea7042f33d89efb
width 128 bits (approx)
EOF
RUN
FILE=malloc://1024
NAME=% sha256 digest
CMDS=<<EOF
w AAAABBBBCCCCDDDD
% sha256(0, 16)
EOF
EXPECT=<<EOF
decimal 46411692673025759693186424457495866733606273688990416351710414590085438416608
hex 0x669c164f44198b43b7175ac0dff496fe43393717428747ccb44c294fbeaca6e0
width 256 bits (approx)
EOF
RUN
FILE=malloc://1024
NAME=% entropy uniform data
CMDS=<<EOF
w AAAABBBBCCCCDDDD
% entropy(0, 16)
EOF
EXPECT=<<EOF
float 2
scifmt 2
hex 0x4000000000000000
EOF
RUN
FILE=malloc://1024
NAME=% division by zero
CMDS=<<EOF
% 1/0
EOF
EXPECT=<<EOF
EOF
EXPECT_ERR=<<EOF
ERROR: core: RzNum ERROR: Division by Zero
EOF
RUN
FILE=malloc://1024
NAME=% bitvector arithmetic wraps at width
CMDS=<<EOF
e scr.utf8=false
% 200u8 + 100u8
EOF
EXPECT=<<EOF
hex 0x2c
binary 0b00101100
width 8 bits
EOF
RUN
FILE=malloc://1024
NAME=% bitvector multiply 16-bit
CMDS=<<EOF
e scr.utf8=false
% 10u16 * 10u16
EOF
EXPECT=<<EOF
hex 0x0064
binary 0b0000000001100100
width 16 bits
EOF
RUN
FILE=malloc://1024
NAME=% bitvector unicode rendering with scr.utf8
CMDS=<<EOF
e scr.utf8=true
% 200u8 + 100u8
EOF
EXPECT=<<EOF
hex 0x2c₈
binary 00101100₈
width 8 bits
EOF
RUN
FILE=malloc://1024
NAME=% bitvector ascii rendering without scr.utf8
CMDS=<<EOF
e scr.utf8=false
% 200u8 + 100u8
EOF
EXPECT=<<EOF
hex 0x2c
binary 0b00101100
width 8 bits
EOF
RUN
FILE=malloc://1024
NAME=% unicode function Chinese max
CMDS=<<EOF
% 最大(0x10, 0x20)
EOF
EXPECT=<<EOF
int32 32
uint32 32
hex 0x20
octal 040
unit 32
segment 0000:0020
string " "
fvalue 32.0
float 32.000000f
double 32.000000
binary 0b00100000
trits 0t1012
EOF
RUN
FILE=malloc://1024
NAME=% unicode function Arabic abs
CMDS=<<EOF
% دالة(0xfffffffffffffffb)
EOF
EXPECT=<<EOF
int32 5
uint32 5
hex 0x5
octal 05
unit 5
segment 0000:0005
string "\x05"
fvalue 5.0
float 5.000000f
double 5.000000
binary 0b00000101
trits 0t12
EOF
RUN
FILE=malloc://1024
NAME=% unicode variable Chinese radius
CMDS=<<EOF
% (半径 = 5) * 半径
EOF
EXPECT=<<EOF
int32 25
uint32 25
hex 0x19
octal 031
unit 25
segment 0000:0019
string "\x19"
fvalue 25.0
float 25.000000f
double 25.000000
binary 0b00011001
trits 0t221
EOF
RUN
FILE=malloc://1024
NAME=% sequence semicolon
CMDS=<<EOF
% x = 10; y = 20; x + y
EOF
EXPECT=<<EOF
int32 10
uint32 10
hex 0xa
octal 012
unit 10
segment 0000:000a
string "\n"
fvalue 10.0
float 10.000000f
double 10.000000
binary 0b00001010
trits 0t101
EOF
RUN
FILE=malloc://1024
NAME=% signed division
CMDS=<<EOF
% 0xfffffffffffffffb sdiv 2
EOF
EXPECT=<<EOF
int64 -2
uint64 18446744073709551614
hex 0xfffffffffffffffe
octal 01777777777777777777776
unit 16E
segment fffff000:0ffe
string "\xfe\xff\xff\xff\xff\xff\xff\xff"
fvalue -2.0
float -2.000000f
double -2.000000
binary 0b1111111111111111111111111111111111111111111111111111111111111110
trits 0t11112220022122120101211020120210210211212
EOF
RUN
FILE=malloc://1024
NAME=% arithmetic shift right
CMDS=<<EOF
% 0xffffffffffffff00 sar 4
EOF
EXPECT=<<EOF
int64 -16
uint64 18446744073709551600
hex 0xfffffffffffffff0
octal 01777777777777777777760
unit 16E
segment fffff000:0ff0
string "\xf0\xff\xff\xff\xff\xff\xff\xff"
fvalue -16.0
float -16.000000f
double -16.000000
binary 0b1111111111111111111111111111111111111111111111111111111111110000
trits 0t11112220022122120101211020120210210211100
EOF
RUN
FILE=malloc://1024
NAME=% len of string bytes
CMDS=<<EOF
% len("ABCD") / 8
EOF
EXPECT=<<EOF
int32 4
uint32 4
hex 0x4
octal 04
unit 4
segment 0000:0004
string "\x04"
fvalue 4.0
float 4.000000f
double 4.000000
binary 0b00000100
trits 0t11
EOF
RUN
FILE=malloc://1024
NAME=% len of bitvector
CMDS=<<EOF
% len(0xffu16)
EOF
EXPECT=<<EOF
int32 16
uint32 16
hex 0x10
octal 020
unit 16
segment 0000:0010
string "\x10"
fvalue 16.0
float 16.000000f
double 16.000000
binary 0b00010000
trits 0t121
EOF
RUN
FILE=malloc://1024
NAME=% persistent variable across commands
CMDS=<<EOF
% bla = 0x100
% bla + 5
EOF
EXPECT=<<EOF
int32 256
uint32 256
hex 0x100
octal 0400
unit 256
segment 0000:0100
string "\x01"
fvalue 256.0
float 256.000000f
double 256.000000
binary 0b0000000100000000
trits 0t100111
int32 261
uint32 261
hex 0x105
octal 0405
unit 261
segment 0000:0105
string "\x05\x01"
fvalue 261.0
float 261.000000f
double 261.000000
binary 0b0000000100000101
trits 0t100200
EOF
RUN
FILE=malloc://1024
NAME=% conditional truthy
CMDS=<<EOF
% 1 ? 0xaa : 0xbb
EOF
EXPECT=<<EOF
int32 170
uint32 170
hex 0xaa
octal 0252
unit 170
segment 0000:00aa
string "\xaa"
fvalue 170.0
float 170.000000f
double 170.000000
binary 0b10101010
trits 0t20022
EOF
RUN
FILE=malloc://1024
NAME=% conditional comparison
CMDS=<<EOF
% 5 > 3 ? 100 : 200
EOF
EXPECT=<<EOF
EOF
RUN
FILE=malloc://1024
NAME=% conditional short-circuit
CMDS=<<EOF
% 1 ? 42 : 1 / 0
EOF
EXPECT=<<EOF
int32 42
uint32 42
hex 0x2a
octal 052
unit 42
segment 0000:002a
string "*"
fvalue 42.0
float 42.000000f
double 42.000000
binary 0b00101010
trits 0t1120
EOF
RUN