feat: add bitwise XOR, left shift, and right shift operators to Wren language

Add three new bitwise operators (^, <<, >>) to the Wren compiler and runtime,
including tokenization, parsing with correct precedence, and native method
implementations operating on 32-bit unsigned integers. New test files verify
correct behavior for edge cases including max u32 values and operand type errors.
This commit is contained in:
Bob Nystrom
2015-02-18 15:55:38 +00:00
8 changed files with 110 additions and 3 deletions
+15
View File
@@ -0,0 +1,15 @@
IO.print(0 << 0) // expect: 0
IO.print(1 << 0) // expect: 1
IO.print(0 << 1) // expect: 0
IO.print(1 << 1) // expect: 2
IO.print(2863311530 << 1) // expect: 1431655764
IO.print(4042322160 << 1) // expect: 3789677024
// Max u32 value.
IO.print(4294967295 << 1) // expect: 4294967294
// Past max u32 value.
IO.print(4294967296 << 1) // expect: 0
// TODO: Negative numbers.
// TODO: Floating-point numbers.
@@ -0,0 +1 @@
1 << false // expect runtime error: Right operand must be a number.
+15
View File
@@ -0,0 +1,15 @@
IO.print(0 >> 0) // expect: 0
IO.print(1 >> 0) // expect: 1
IO.print(0 >> 1) // expect: 0
IO.print(1 >> 1) // expect: 0
IO.print(2863311530 >> 1) // expect: 1431655765
IO.print(4042322160 >> 1) // expect: 2021161080
// Max u32 value.
IO.print(4294967295 >> 1) // expect: 2147483647
// Past max u32 value.
IO.print(4294967296 >> 1) // expect: 0
// TODO: Negative numbers.
// TODO: Floating-point numbers.
@@ -0,0 +1 @@
1 >> false // expect runtime error: Right operand must be a number.
+15
View File
@@ -0,0 +1,15 @@
IO.print(0 ^ 0) // expect: 0
IO.print(1 ^ 1) // expect: 0
IO.print(0 ^ 1) // expect: 1
IO.print(1 ^ 0) // expect: 1
IO.print(2863311530 ^ 1431655765) // expect: 4294967295
IO.print(4042322160 ^ 1010580540) // expect: 3435973836
// Max u32 value.
IO.print(4294967295 ^ 4294967295) // expect: 0
// Past max u32 value.
IO.print(4294967296 ^ 4294967296) // expect: 0
// TODO: Negative numbers.
// TODO: Floating-point numbers.
@@ -0,0 +1 @@
1 ^ false // expect runtime error: Right operand must be a number.