Skip to content

Commit a9597ce

Browse files
committed
FEAT: added infix shift operators: << and >>
1 parent 1c5924f commit a9597ce

File tree

5 files changed

+3843
-1
lines changed

5 files changed

+3843
-1
lines changed

src/boot/natives.r

+3
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,9 @@ shift: native [
823823

824824
;-- New, hackish stuff:
825825

826+
;shift-left: ;@@ defined in n-math.c
827+
;shift-right: ;@@ defined in n-math.c
828+
826829
++: native [
827830
{Increment an integer or series index. Return its prior value.}
828831
'word [word!] "Integer or series variable"

src/boot/ops.r

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ REBOL [
3535
and and~
3636
or or~
3737
xor xor~
38+
<< shift-left
39+
>> shift-right

src/core/n-math.c

+40
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,46 @@ enum {SINE, COSINE, TANGENT};
450450
#pragma warning (default : 4146)
451451
#endif
452452

453+
/***********************************************************************
454+
**
455+
*/ REBNATIVE(shift_left)
456+
/*
457+
// shift-left: native [
458+
// "Shift bits to the left (unsigned)."
459+
// data [integer!]
460+
// bits [integer!]
461+
// ]
462+
***********************************************************************/
463+
{
464+
REBVAL *a = D_ARG(1);
465+
REBVAL *b = D_ARG(2);
466+
467+
if (!IS_INTEGER(a)) Trap2(RE_EXPECT_VAL, Get_Type_Word(REB_INTEGER), a);
468+
if (!IS_INTEGER(b)) Trap2(RE_EXPECT_VAL, Get_Type_Word(REB_INTEGER), b);
469+
if (VAL_INT64(b) > (uint)0L) VAL_INT64(a) <<= VAL_INT64(b);
470+
return R_ARG1;
471+
}
472+
473+
/***********************************************************************
474+
**
475+
*/ REBNATIVE(shift_right)
476+
/*
477+
// shift-right: native [
478+
// "Shift bits to the right (unsigned)."
479+
// data [integer!]
480+
// bits [integer!]
481+
// ]
482+
***********************************************************************/
483+
{
484+
REBVAL *a = D_ARG(1);
485+
REBVAL *b = D_ARG(2);
486+
487+
if (!IS_INTEGER(a)) Trap2(RE_EXPECT_VAL, Get_Type_Word(REB_INTEGER), a);
488+
if (!IS_INTEGER(b)) Trap2(RE_EXPECT_VAL, Get_Type_Word(REB_INTEGER), b);
489+
if (VAL_INT64(b) > (uint)0L) VAL_INT64(a) >>= VAL_INT64(b);
490+
return R_ARG1;
491+
}
492+
453493

454494
/***********************************************************************
455495
**

src/tests/run-tests.r3

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dt [ ;- delta time
1111
wrap load %units/lexer-test.r3
1212
wrap load %units/enbase-test.r3
1313
wrap load %units/map-test.r3
14-
;wrap load %units/integer-test.r3
14+
wrap load %units/integer-test.r3
1515
wrap load %units/power-test.r3
1616
wrap load %units/mezz-crypt-test.r3
1717
wrap load %units/rc4-test.r3

0 commit comments

Comments
 (0)