Skip to content

Commit

Permalink
FEAT: VECTOR compact construction
Browse files Browse the repository at this point in the history
Implements: metaeducation/rebol-issues#2396

Example:
```
>> #[ui8! [1 2 3]]
== make vector! [unsigned integer! 8 3 [1 2 3]]

```
  • Loading branch information
Oldes committed Jan 29, 2020
1 parent 65a0685 commit 545dd90
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/boot/words.r
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ any-string!
any-object!
any-block!

;- numeric types (used for "short" vector! construction)
;- like: #[ui8 4] which is same like: make vector! [unsigned integer! 8 4]
; these numeric files are used in l-types.c and t-vector.c sources
; and can be used for future struct! and routine! implementations
si8!
si16!
si32!
si64!

ui8!
ui16!
ui32!
ui64!

f32!
f64!
;@@ O: Should be supported also longer variants like uint8! etc?
; end of numeric types

datatypes

native
Expand Down
3 changes: 3 additions & 0 deletions src/core/l-types.c
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,9 @@ bad_hex: Trap0(RE_INVALID_CHARS);
return TRUE;

default:
if (type >= SYM_SI8X && type <= SYM_F64X) {
if (MT_Vector(value, val, REB_VECTOR)) return TRUE;
}
return FALSE;
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/core/t-vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,17 @@ void Set_Vector_Row(REBSER *ser, REBVAL *blk)
// SIGNED / UNSIGNED
if (IS_WORD(bp)) {
switch (VAL_WORD_CANON(bp)) {
case SYM_SI8X: type = 0; sign = 0; bits = 8; bp++; goto size_spec;
case SYM_UI8X: type = 0; sign = 1; bits = 8; bp++; goto size_spec;
case SYM_SI16X: type = 0; sign = 0; bits = 16; bp++; goto size_spec;
case SYM_UI16X: type = 0; sign = 1; bits = 16; bp++; goto size_spec;
case SYM_SI32X: type = 0; sign = 0; bits = 32; bp++; goto size_spec;
case SYM_UI32X: type = 0; sign = 1; bits = 32; bp++; goto size_spec;
case SYM_SI64X: type = 0; sign = 0; bits = 64; bp++; goto size_spec;
case SYM_UI64X: type = 0; sign = 1; bits = 64; bp++; goto size_spec;
case SYM_F32X: type = 1; sign = 0; bits = 32; bp++; goto size_spec;
case SYM_F64X: type = 1; sign = 0; bits = 64; bp++; goto size_spec;

case SYM_UNSIGNED: sign = 1; bp++; break;
case SYM_SIGNED: sign = 0; bp++; break;
}
Expand Down Expand Up @@ -518,6 +529,8 @@ void Set_Vector_Row(REBSER *ser, REBVAL *blk)
else return 0;
} else return 0;

size_spec:

// SIZE
if (IS_INTEGER(bp)) {
size = Int32(bp);
Expand Down
60 changes: 60 additions & 0 deletions src/tests/units/vector-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,66 @@ Rebol [

===end-group===

===start-group=== "VECTOR compact construction"
;@@ https://github.com/rebol/rebol-issues/issues/2396
--test-- "Compact construction syntax (empty)"
--assert (mold #[si8!]) = "make vector! [integer! 8 0 []]"
--assert (mold #[si16!]) = "make vector! [integer! 16 0 []]"
--assert (mold #[si32!]) = "make vector! [integer! 32 0 []]"
--assert (mold #[si64!]) = "make vector! [integer! 64 0 []]"
--assert (mold #[ui8!]) = "make vector! [unsigned integer! 8 0 []]"
--assert (mold #[ui16!]) = "make vector! [unsigned integer! 16 0 []]"
--assert (mold #[ui32!]) = "make vector! [unsigned integer! 32 0 []]"
--assert (mold #[ui64!]) = "make vector! [unsigned integer! 64 0 []]"
--assert (mold #[f32!]) = "make vector! [decimal! 32 0 []]"
--assert (mold #[f64!]) = "make vector! [decimal! 64 0 []]"

--test-- "Compact construction syntax (size)"
--assert (mold #[si8! 3]) = "make vector! [integer! 8 3 [0 0 0]]"
--assert (mold #[si16! 3]) = "make vector! [integer! 16 3 [0 0 0]]"
--assert (mold #[si32! 3]) = "make vector! [integer! 32 3 [0 0 0]]"
--assert (mold #[si64! 3]) = "make vector! [integer! 64 3 [0 0 0]]"
--assert (mold #[ui8! 3]) = "make vector! [unsigned integer! 8 3 [0 0 0]]"
--assert (mold #[ui16! 3]) = "make vector! [unsigned integer! 16 3 [0 0 0]]"
--assert (mold #[ui32! 3]) = "make vector! [unsigned integer! 32 3 [0 0 0]]"
--assert (mold #[ui64! 3]) = "make vector! [unsigned integer! 64 3 [0 0 0]]"
--assert (mold #[f32! 3]) = "make vector! [decimal! 32 3 [0.0 0.0 0.0]]"
--assert (mold #[f64! 3]) = "make vector! [decimal! 64 3 [0.0 0.0 0.0]]"

--test-- "Compact construction syntax (data)"
--assert (mold #[si8! [1 2]]) = "make vector! [integer! 8 2 [1 2]]"
--assert (mold #[si16! [1 2]]) = "make vector! [integer! 16 2 [1 2]]"
--assert (mold #[si32! [1 2]]) = "make vector! [integer! 32 2 [1 2]]"
--assert (mold #[si64! [1 2]]) = "make vector! [integer! 64 2 [1 2]]"
--assert (mold #[ui8! [1 2]]) = "make vector! [unsigned integer! 8 2 [1 2]]"
--assert (mold #[ui16! [1 2]]) = "make vector! [unsigned integer! 16 2 [1 2]]"
--assert (mold #[ui32! [1 2]]) = "make vector! [unsigned integer! 32 2 [1 2]]"
--assert (mold #[ui64! [1 2]]) = "make vector! [unsigned integer! 64 2 [1 2]]"
--assert (mold #[f32! [1 2]]) = "make vector! [decimal! 32 2 [1.0 2.0]]"
--assert (mold #[f64! [1 2]]) = "make vector! [decimal! 64 2 [1.0 2.0]]"

--test-- "Compact construction syntax (data with index)"
--assert (mold v: #[si8! [1 2] 2]) = "make vector! [integer! 8 1 [2]]"
--assert 2 = index? v
--assert (mold v: #[si16! [1 2] 2]) = "make vector! [integer! 16 1 [2]]"
--assert 2 = index? v
--assert (mold v: #[si32! [1 2] 2]) = "make vector! [integer! 32 1 [2]]"
--assert 2 = index? v
--assert (mold v: #[si64! [1 2] 2]) = "make vector! [integer! 64 1 [2]]"
--assert 2 = index? v
--assert (mold v: #[ui8! [1 2] 2]) = "make vector! [unsigned integer! 8 1 [2]]"
--assert 2 = index? v
--assert (mold v: #[ui16! [1 2] 2]) = "make vector! [unsigned integer! 16 1 [2]]"
--assert 2 = index? v
--assert (mold v: #[ui32! [1 2] 2]) = "make vector! [unsigned integer! 32 1 [2]]"
--assert 2 = index? v
--assert (mold v: #[ui64! [1 2] 2]) = "make vector! [unsigned integer! 64 1 [2]]"
--assert 2 = index? v
--assert (mold v: #[f32! [1 2] 2]) = "make vector! [decimal! 32 1 [2.0]]"
--assert 2 = index? v
--assert (mold v: #[f64! [1 2] 2]) = "make vector! [decimal! 64 1 [2.0]]"
--assert 2 = index? v


===start-group=== "VECTOR math"

Expand Down

0 comments on commit 545dd90

Please sign in to comment.