feat: replace Wren Range class with native C object for performance
Remove the Range class definition from corelib.wren and implement it as a native C type with ObjRange struct, adding native methods for from, to, min, max, iterate, and iteratorValue. Move range operator handling from Num class to native num_dotDot and num_dotDotDot functions. Update VM to support OBJ_RANGE type in marking, printing, and type checking. Add comprehensive test suite for range operations including ordered, backwards, and exclusive ranges.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
// Ordered range.
|
||||
IO.print((2..5).from) // expect: 2
|
||||
IO.print((3..3).from) // expect: 3
|
||||
IO.print((0..3).from) // expect: 0
|
||||
IO.print((-5..3).from) // expect: -5
|
||||
IO.print((-5..-2).from) // expect: -5
|
||||
|
||||
// Backwards range.
|
||||
IO.print((5..2).from) // expect: 5
|
||||
IO.print((3..0).from) // expect: 3
|
||||
IO.print((3..-5).from) // expect: 3
|
||||
IO.print((-2..-5).from) // expect: -2
|
||||
|
||||
// Exclusive ordered range.
|
||||
IO.print((2...5).from) // expect: 2
|
||||
IO.print((3...3).from) // expect: 3
|
||||
IO.print((0...3).from) // expect: 0
|
||||
IO.print((-5...3).from) // expect: -5
|
||||
IO.print((-5...-2).from) // expect: -5
|
||||
|
||||
// Exclusive backwards range.
|
||||
IO.print((5...2).from) // expect: 5
|
||||
IO.print((3...0).from) // expect: 3
|
||||
IO.print((3...-5).from) // expect: 3
|
||||
IO.print((-2...-5).from) // expect: -2
|
||||
|
||||
// TODO: Test toString.
|
||||
// TODO: Non-number RHS.
|
||||
// TODO: Non-integer RHS.
|
||||
// TODO: Range iteration.
|
||||
// TODO: Empty or negative ranges.
|
||||
@@ -0,0 +1,8 @@
|
||||
var range = 1..3
|
||||
IO.print(range.iterate(null)) // expect: 1
|
||||
IO.print(range.iterate(1)) // expect: 2
|
||||
IO.print(range.iterate(2)) // expect: 3
|
||||
IO.print(range.iterate(3)) // expect: false
|
||||
IO.print(range.iterate(4)) // expect: false
|
||||
|
||||
// TODO: Negative and empty ranges.
|
||||
@@ -0,0 +1 @@
|
||||
(1..3).iterate("") // expect runtime error: Iterator must be a number.
|
||||
@@ -0,0 +1,11 @@
|
||||
var range = 1..3
|
||||
IO.print(range.iteratorValue(1)) // expect: 1
|
||||
IO.print(range.iteratorValue(2)) // expect: 2
|
||||
IO.print(range.iteratorValue(3)) // expect: 3
|
||||
|
||||
// Doesn't bother to bounds check.
|
||||
IO.print(range.iteratorValue(-2)) // expect: -2
|
||||
IO.print(range.iteratorValue(5)) // expect: 5
|
||||
|
||||
// Or type check.
|
||||
IO.print(range.iteratorValue("s")) // expect: s
|
||||
@@ -0,0 +1,25 @@
|
||||
// Ordered range.
|
||||
IO.print((2..5).max) // expect: 5
|
||||
IO.print((3..3).max) // expect: 3
|
||||
IO.print((0..3).max) // expect: 3
|
||||
IO.print((-5..3).max) // expect: 3
|
||||
IO.print((-5..-2).max) // expect: -2
|
||||
|
||||
// Backwards range.
|
||||
IO.print((5..2).max) // expect: 5
|
||||
IO.print((3..0).max) // expect: 3
|
||||
IO.print((3..-5).max) // expect: 3
|
||||
IO.print((-2..-5).max) // expect: -2
|
||||
|
||||
// Exclusive ordered range.
|
||||
IO.print((2...5).max) // expect: 4
|
||||
IO.print((3...3).max) // expect: 3
|
||||
IO.print((0...3).max) // expect: 2
|
||||
IO.print((-5...3).max) // expect: 2
|
||||
IO.print((-5...-2).max) // expect: -3
|
||||
|
||||
// Exclusive backwards range.
|
||||
IO.print((5...2).max) // expect: 5
|
||||
IO.print((3...0).max) // expect: 3
|
||||
IO.print((3...-5).max) // expect: 3
|
||||
IO.print((-2...-5).max) // expect: -2
|
||||
@@ -0,0 +1,26 @@
|
||||
// Ordered range.
|
||||
IO.print((2..5).min) // expect: 2
|
||||
IO.print((3..3).min) // expect: 3
|
||||
IO.print((0..3).min) // expect: 0
|
||||
IO.print((-5..3).min) // expect: -5
|
||||
IO.print((-5..-2).min) // expect: -5
|
||||
|
||||
// Backwards range.
|
||||
IO.print((5..2).min) // expect: 2
|
||||
IO.print((3..0).min) // expect: 0
|
||||
IO.print((3..-5).min) // expect: -5
|
||||
IO.print((-2..-5).min) // expect: -5
|
||||
|
||||
// Exclusive ordered range.
|
||||
IO.print((2...5).min) // expect: 2
|
||||
IO.print((3...3).min) // expect: 2
|
||||
IO.print((0...3).min) // expect: 0
|
||||
IO.print((-5...3).min) // expect: -5
|
||||
IO.print((-5...-2).min) // expect: -5
|
||||
|
||||
// Exclusive backwards range.
|
||||
// TODO: Is this what we want? "..." always means "subtract 1"?
|
||||
IO.print((5...2).min) // expect: 1
|
||||
IO.print((3...0).min) // expect: -1
|
||||
IO.print((3...-5).min) // expect: -6
|
||||
IO.print((-2...-5).min) // expect: -6
|
||||
@@ -0,0 +1,25 @@
|
||||
// Ordered range.
|
||||
IO.print((2..5).to) // expect: 5
|
||||
IO.print((3..3).to) // expect: 3
|
||||
IO.print((0..3).to) // expect: 3
|
||||
IO.print((-5..3).to) // expect: 3
|
||||
IO.print((-5..-2).to) // expect: -2
|
||||
|
||||
// Backwards range.
|
||||
IO.print((5..2).to) // expect: 2
|
||||
IO.print((3..0).to) // expect: 0
|
||||
IO.print((3..-5).to) // expect: -5
|
||||
IO.print((-2..-5).to) // expect: -5
|
||||
|
||||
// Exclusive ordered range.
|
||||
IO.print((2...5).to) // expect: 4
|
||||
IO.print((3...3).to) // expect: 2
|
||||
IO.print((0...3).to) // expect: 2
|
||||
IO.print((-5...3).to) // expect: 2
|
||||
IO.print((-5...-2).to) // expect: -3
|
||||
|
||||
// Exclusive backwards range.
|
||||
IO.print((5...2).to) // expect: 1
|
||||
IO.print((3...0).to) // expect: -1
|
||||
IO.print((3...-5).to) // expect: -6
|
||||
IO.print((-2...-5).to) // expect: -6
|
||||
@@ -0,0 +1,6 @@
|
||||
var range = 2..5
|
||||
|
||||
IO.print(range is Range) // expect: true
|
||||
IO.print(range is Object) // expect: true
|
||||
IO.print(range is String) // expect: false
|
||||
IO.print(range.type == Range) // expect: true
|
||||
Reference in New Issue
Block a user