feat: rename IO.write to IO.print and add Unicode escape support in strings

Rename the IO.write method to IO.print, which now prints without a trailing newline, and add a new IO.print method that appends a newline after output. Update all benchmark, example, and test files to use IO.print instead of IO.write. Additionally, implement Unicode escape sequence parsing in the compiler's string tokenizer, supporting \uXXXX and \u{...} syntax with proper UTF-8 encoding for code points up to U+10FFFF.
This commit is contained in:
Bob Nystrom
2014-01-05 20:27:12 +00:00
parent e23e0ce6a3
commit c72db7d594
159 changed files with 1690 additions and 1599 deletions
+4 -4
View File
@@ -1,10 +1,10 @@
var a = []
a.add(1)
IO.write(a) // expect: [1]
IO.print(a) // expect: [1]
a.add(2)
IO.write(a) // expect: [1, 2]
IO.print(a) // expect: [1, 2]
a.add(3)
IO.write(a) // expect: [1, 2, 3]
IO.print(a) // expect: [1, 2, 3]
// Returns added element.
IO.write(a.add(4)) // expect: 4
IO.print(a.add(4)) // expect: 4
+3 -3
View File
@@ -1,7 +1,7 @@
var a = [1, 2, 3]
a.clear
IO.write(a) // expect: []
IO.write(a.count) // expect: 0
IO.print(a) // expect: []
IO.print(a.count) // expect: 0
// Returns null.
IO.write([1, 2].clear) // expect: null
IO.print([1, 2].clear) // expect: null
+3 -3
View File
@@ -1,3 +1,3 @@
IO.write([].count) // expect: 0
IO.write([1].count) // expect: 1
IO.write([1, 2, 3, 4].count) // expect: 4
IO.print([].count) // expect: 0
IO.print([1].count) // expect: 1
IO.print([1, 2, 3, 4].count) // expect: 4
+10 -10
View File
@@ -1,41 +1,41 @@
// Add to empty list.
var a = []
a.insert(1, 0)
IO.write(a) // expect: [1]
IO.print(a) // expect: [1]
// Normal indices.
var b = [1, 2, 3]
b.insert(4, 0)
IO.write(b) // expect: [4, 1, 2, 3]
IO.print(b) // expect: [4, 1, 2, 3]
var c = [1, 2, 3]
c.insert(4, 1)
IO.write(c) // expect: [1, 4, 2, 3]
IO.print(c) // expect: [1, 4, 2, 3]
var d = [1, 2, 3]
d.insert(4, 2)
IO.write(d) // expect: [1, 2, 4, 3]
IO.print(d) // expect: [1, 2, 4, 3]
var e = [1, 2, 3]
e.insert(4, 3)
IO.write(e) // expect: [1, 2, 3, 4]
IO.print(e) // expect: [1, 2, 3, 4]
// Negative indices.
var f = [1, 2, 3]
f.insert(4, -4)
IO.write(f) // expect: [4, 1, 2, 3]
IO.print(f) // expect: [4, 1, 2, 3]
var g = [1, 2, 3]
g.insert(4, -3)
IO.write(g) // expect: [1, 4, 2, 3]
IO.print(g) // expect: [1, 4, 2, 3]
var h = [1, 2, 3]
h.insert(4, -2)
IO.write(h) // expect: [1, 2, 4, 3]
IO.print(h) // expect: [1, 2, 4, 3]
var i = [1, 2, 3]
i.insert(4, -1)
IO.write(i) // expect: [1, 2, 3, 4]
IO.print(i) // expect: [1, 2, 3, 4]
// Returns.inserted value.
IO.write([1, 2].insert(3, 0)) // expect: 3
IO.print([1, 2].insert(3, 0)) // expect: 3
+6 -6
View File
@@ -1,7 +1,7 @@
var a = ["one", "two", "three", "four"]
IO.write(a.iterate(null)) // expect: 0
IO.write(a.iterate(0)) // expect: 1
IO.write(a.iterate(1)) // expect: 2
IO.write(a.iterate(2)) // expect: 3
IO.write(a.iterate(3)) // expect: false
IO.write(a.iterate(-1)) // expect: false
IO.print(a.iterate(null)) // expect: 0
IO.print(a.iterate(0)) // expect: 1
IO.print(a.iterate(1)) // expect: 2
IO.print(a.iterate(2)) // expect: 3
IO.print(a.iterate(3)) // expect: false
IO.print(a.iterate(-1)) // expect: false
+4 -4
View File
@@ -1,5 +1,5 @@
var a = ["one", "two", "three", "four"]
IO.write(a.iteratorValue(0)) // expect: one
IO.write(a.iteratorValue(1)) // expect: two
IO.write(a.iteratorValue(2)) // expect: three
IO.write(a.iteratorValue(3)) // expect: four
IO.print(a.iteratorValue(0)) // expect: one
IO.print(a.iteratorValue(1)) // expect: two
IO.print(a.iteratorValue(2)) // expect: three
IO.print(a.iteratorValue(3)) // expect: four
+4 -4
View File
@@ -8,7 +8,7 @@ var list = [
]
IO.write(list[0]) // expect: a
IO.write(list[1]) // expect: b
IO.write(list[2]) // expect: c
IO.write(list[3]) // expect: d
IO.print(list[0]) // expect: a
IO.print(list[1]) // expect: b
IO.print(list[2]) // expect: c
IO.print(list[3]) // expect: d
+7 -7
View File
@@ -1,27 +1,27 @@
var a = [1, 2, 3]
a.removeAt(0)
IO.write(a) // expect: [2, 3]
IO.print(a) // expect: [2, 3]
var b = [1, 2, 3]
b.removeAt(1)
IO.write(b) // expect: [1, 3]
IO.print(b) // expect: [1, 3]
var c = [1, 2, 3]
c.removeAt(2)
IO.write(c) // expect: [1, 2]
IO.print(c) // expect: [1, 2]
// Index backwards from end.
var d = [1, 2, 3]
d.removeAt(-3)
IO.write(d) // expect: [2, 3]
IO.print(d) // expect: [2, 3]
var e = [1, 2, 3]
e.removeAt(-2)
IO.write(e) // expect: [1, 3]
IO.print(e) // expect: [1, 3]
var f = [1, 2, 3]
f.removeAt(-1)
IO.write(f) // expect: [1, 2]
IO.print(f) // expect: [1, 2]
// Return the removed value.
IO.write([3, 4, 5].removeAt(1)) // expect: 4
IO.print([3, 4, 5].removeAt(1)) // expect: 4
+8 -8
View File
@@ -1,12 +1,12 @@
// Returns elements.
var list = ["a", "b", "c", "d"]
IO.write(list[0]) // expect: a
IO.write(list[1]) // expect: b
IO.write(list[2]) // expect: c
IO.write(list[3]) // expect: d
IO.print(list[0]) // expect: a
IO.print(list[1]) // expect: b
IO.print(list[2]) // expect: c
IO.print(list[3]) // expect: d
// Allows indexing backwards from the end.
IO.write(list[-4]) // expect: a
IO.write(list[-3]) // expect: b
IO.write(list[-2]) // expect: c
IO.write(list[-1]) // expect: d
IO.print(list[-4]) // expect: a
IO.print(list[-3]) // expect: b
IO.print(list[-2]) // expect: c
IO.print(list[-1]) // expect: d
+3 -3
View File
@@ -4,13 +4,13 @@
list[0] = 5
list[1] = 6
list[2] = 7
IO.write(list) // expect: [5, 6, 7]
IO.print(list) // expect: [5, 6, 7]
}
// Returns right-hand side.
{
var list = [1, 2, 3]
IO.write(list[1] = 5) // expect: 5
IO.print(list[1] = 5) // expect: 5
}
// Negative indices.
@@ -19,7 +19,7 @@
list[-1] = 5
list[-2] = 6
list[-3] = 7
IO.write(list) // expect: [7, 6, 5]
IO.print(list) // expect: [7, 6, 5]
}
// TODO: Handle out of range.
+4 -4
View File
@@ -1,17 +1,17 @@
// Handle empty list.
IO.write([].toString) // expect: []
IO.print([].toString) // expect: []
// Does not quote strings.
IO.write([1, "2", true].toString) // expect: [1, 2, true]
IO.print([1, "2", true].toString) // expect: [1, 2, true]
// Nested lists.
IO.write([1, [2, [3], 4], 5]) // expect: [1, [2, [3], 4], 5]
IO.print([1, [2, [3], 4], 5]) // expect: [1, [2, [3], 4], 5]
// Calls toString on elements.
class Foo {
toString { return "Foo.toString" }
}
IO.write([1, new Foo, 2]) // expect: [1, Foo.toString, 2]
IO.print([1, new Foo, 2]) // expect: [1, Foo.toString, 2]
// TODO: Handle lists that contain themselves.
+4 -4
View File
@@ -1,4 +1,4 @@
IO.write([] is List) // expect: true
IO.write([] is Object) // expect: true
IO.write([] is Bool) // expect: false
IO.write([].type == List) // expect: true
IO.print([] is List) // expect: true
IO.print([] is Object) // expect: true
IO.print([] is Bool) // expect: false
IO.print([].type == List) // expect: true