Skip to content

Commit

Permalink
update numeric cast syntax; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfvi committed Jun 3, 2024
1 parent 44dd3ff commit 6f46678
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 23 deletions.
46 changes: 23 additions & 23 deletions num/src/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@ bi_enum! {
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum NumTag {
"to_u24": U24 = 0x00,
"to_i24": I24 = 0x01,
"to_f24": F24 = 0x02,
" ": Sym = 0x03,
"+": Add = 0x04,
"-": Sub = 0x05,
":-": SubS = 0x06,
"*": Mul = 0x07,
"/": Div = 0x08,
":/": DivS = 0x09,
"%": Rem = 0x0a,
":%": RemS = 0x0b,
"&": And = 0x0c,
"|": Or = 0x0d,
"^": Xor = 0x0e,
"<<": Shl = 0x0f,
":<<": ShlS = 0x10,
">>": Shr = 0x11,
":>>": ShrS = 0x12,
"=": Eq = 0x13,
"!": Ne = 0x14,
"<": Lt = 0x15,
">": Gt = 0x16,
"u24": U24 = 0x00,
"i24": I24 = 0x01,
"f24": F24 = 0x02,
" ": Sym = 0x03,
"+": Add = 0x04,
"-": Sub = 0x05,
":-": SubS = 0x06,
"*": Mul = 0x07,
"/": Div = 0x08,
":/": DivS = 0x09,
"%": Rem = 0x0a,
":%": RemS = 0x0b,
"&": And = 0x0c,
"|": Or = 0x0d,
"^": Xor = 0x0e,
"<<": Shl = 0x0f,
":<<": ShlS = 0x10,
">>": Shr = 0x11,
":>>": ShrS = 0x12,
"=": Eq = 0x13,
"!": Ne = 0x14,
"<": Lt = 0x15,
">": Gt = 0x16,
}
}

Expand Down
45 changes: 45 additions & 0 deletions tests/programs/numeric_casts.hvm
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@main = x & @tu0 ~ (* x)

// casting to u24
@tu0 = (* {n x}) & @tu1 ~ (* x) & 0 ~ $([u24] n) // 0
@tu1 = (* {n x}) & @tu2 ~ (* x) & 1234 ~ $([u24] n) // 1234
@tu2 = (* {n x}) & @tu3 ~ (* x) & +4321 ~ $([u24] n) // 4321
@tu3 = (* {n x}) & @tu4 ~ (* x) & -5678 ~ $([u24] n) // 16771538 (reinterprets bits)
@tu4 = (* {n x}) & @tu5 ~ (* x) & 2.8 ~ $([u24] n) // 2 (rounds to zero)
@tu5 = (* {n x}) & @tu6 ~ (* x) & -12.5 ~ $([u24] n) // 0 (saturates)
@tu6 = (* {n x}) & @tu7 ~ (* x) & 16777216.0 ~ $([u24] n) // 16777215 (saturates)
@tu7 = (* {n x}) & @tu8 ~ (* x) & +inf ~ $([u24] n) // 16777215 (saturates)
@tu8 = (* {n x}) & @tu9 ~ (* x) & -inf ~ $([u24] n) // 0 (saturates)
@tu9 = (* {n x}) & @ti0 ~ (* x) & +NaN ~ $([u24] n) // 0

// casting to i24
@ti0 = (* {n x}) & @ti1 ~ (* x) & 0 ~ $([i24] n) // +0
@ti1 = (* {n x}) & @ti2 ~ (* x) & 1234 ~ $([i24] n) // +1234
@ti2 = (* {n x}) & @ti3 ~ (* x) & +4321 ~ $([i24] n) // +4321
@ti3 = (* {n x}) & @ti4 ~ (* x) & -5678 ~ $([i24] n) // -5678
@ti4 = (* {n x}) & @ti5 ~ (* x) & 2.8 ~ $([i24] n) // +2 (rounds to zero)
@ti5 = (* {n x}) & @ti6 ~ (* x) & -12.7 ~ $([i24] n) // -12 (rounds to zero)
@ti6 = (* {n x}) & @ti7 ~ (* x) & 8388610.0 ~ $([i24] n) // +8388607 (saturates)
@ti7 = (* {n x}) & @ti8 ~ (* x) & -8388610.0 ~ $([i24] n) // -8388608 (saturates)
@ti8 = (* {n x}) & @ti9 ~ (* x) & +inf ~ $([i24] n) // +8388607 (saturates)
@ti9 = (* {n x}) & @ti10 ~ (* x) & -inf ~ $([i24] n) // -8388608 (saturates)
@ti10 = (* {n x}) & @tf0 ~ (* x) & +NaN ~ $([i24] n) // +0

// casting to f24
@tf0 = (* {n x}) & @tf1 ~ (* x) & +NaN ~ $([f24] n) // +NaN
@tf1 = (* {n x}) & @tf2 ~ (* x) & +inf ~ $([f24] n) // +inf
@tf2 = (* {n x}) & @tf3 ~ (* x) & -inf ~ $([f24] n) // -inf
@tf3 = (* {n x}) & @tf4 ~ (* x) & 2.15 ~ $([f24] n) // 2.15
@tf4 = (* {n x}) & @tf5 ~ (* x) & -2.15 ~ $([f24] n) // -2.15
@tf5 = (* {n x}) & @tf6 ~ (* x) & 0.15 ~ $([f24] n) // 0.15
@tf6 = (* {n x}) & @tf7 ~ (* x) & -1234 ~ $([f24] n) // -1234.0
@tf7 = (* {n x}) & @tf8 ~ (* x) & +1234 ~ $([f24] n) // +1234.0
@tf8 = (* {n x}) & @tf9 ~ (* x) & 123456 ~ $([f24] n) // 123456.0
@tf9 = (* {n x}) & @tp0 ~ (* x) & 16775982 ~ $([f24] n) // 16775936.0

// printing
@tp0 = (* {n x}) & @tp1 ~ (* x) & n ~ [u24] // [u24]
@tp1 = (* {n x}) & @tp2 ~ (* x) & n ~ [i24] // [i24]
@tp2 = (* {n x}) & @t ~ (* x) & n ~ [f24] // [f24]

@t = *
20 changes: 20 additions & 0 deletions tests/snapshots/pre_reduce_run@tests::programs::numeric_casts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: tests/tests.rs
expression: output
input_file: tests/programs/numeric_casts.hvm
---
{0 {1234 {4321 {16771538 {2 {0 {16777215 {16777215 {0 {0 {+0 {+1234 {+4321 {-5678 {+2 {-12 {+8388607 {-8388608 {+8388607 {-8388608 {+0 {+NaN {+inf {-inf {2.1500244 {-2.1500244 {0.15000153 {-1234.0 {1234.0 {123456.0 {16775936.0 {[u24] {[i24] {[f24] *}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
pre-reduce:
RWTS : 167
- ANNI : 33
- COMM : 1
- ERAS : 34
- DREF : 68
- OPER : 31
run:
RWTS : 75
- ANNI : 1
- COMM : 0
- ERAS : 1
- DREF : 73
- OPER : 0
12 changes: 12 additions & 0 deletions tests/snapshots/run@tests::programs::numeric_casts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: tests/tests.rs
expression: output
input_file: tests/programs/numeric_casts.hvm
---
{0 {1234 {4321 {16771538 {2 {0 {16777215 {16777215 {0 {0 {+0 {+1234 {+4321 {-5678 {+2 {-12 {+8388607 {-8388608 {+8388607 {-8388608 {+0 {+NaN {+inf {-inf {2.1500244 {-2.1500244 {0.15000153 {-1234.0 {1234.0 {123456.0 {16775936.0 {[u24] {[i24] {[f24] *}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
RWTS : 207
- ANNI : 34
- COMM : 1
- ERAS : 35
- DREF : 106
- OPER : 31

0 comments on commit 6f46678

Please sign in to comment.