Make Range a native object type.

Still need to implement better semantics, but this is an 
important first step. It's much faster now too, which is good.
This commit is contained in:
Bob Nystrom
2014-01-20 08:45:15 -08:00
parent d40d04f0c5
commit 97bf314f4b
15 changed files with 291 additions and 88 deletions
-14
View File
@@ -1,14 +0,0 @@
var inclusive = 2..5
IO.print(inclusive is Range) // expect: true
IO.print(inclusive.min) // expect: 2
IO.print(inclusive.max) // expect: 5
var exclusive = 2...5
IO.print(exclusive is Range) // expect: true
IO.print(exclusive.min) // expect: 2
IO.print(exclusive.max) // expect: 4
// TODO: Non-number RHS.
// TODO: Non-integer RHS.
// TODO: Range iteration.
// TODO: Empty or negative ranges.
+31
View File
@@ -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.
+8
View File
@@ -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.
+1
View File
@@ -0,0 +1 @@
(1..3).iterate("") // expect runtime error: Iterator must be a number.
+11
View File
@@ -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
+25
View File
@@ -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
+26
View File
@@ -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
+25
View File
@@ -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
+6
View File
@@ -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