Simple string interpolation.

This allows "%(...)" inside a string literal to interpolate the
stringified result of an expression.

It doesn't support custom interpolators or format strings, but we can
consider extending that later.
This commit is contained in:
Bob Nystrom
2015-11-11 07:55:48 -08:00
parent 71575d9179
commit 78655c68b0
41 changed files with 321 additions and 129 deletions
+6 -7
View File
@@ -26,8 +26,8 @@ var stretchDepth = maxDepth + 1
var start = System.clock
System.print("stretch tree of depth " + stretchDepth.toString + " check: " +
Tree.new(0, stretchDepth).check.toString)
System.print("stretch tree of depth %(stretchDepth) check: " +
"%(Tree.new(0, stretchDepth).check)")
var longLivedTree = Tree.new(0, maxDepth)
@@ -44,12 +44,11 @@ while (depth < stretchDepth) {
check = check + Tree.new(i, depth).check + Tree.new(-i, depth).check
}
System.print((iterations * 2).toString + " trees of depth " +
depth.toString + " check: " + check.toString)
System.print("%(iterations * 2) trees of depth %(depth) check: %(check)")
iterations = iterations / 4
depth = depth + 2
}
System.print("long lived tree of depth " + maxDepth.toString + " check: " +
longLivedTree.check.toString)
System.print("elapsed: " + (System.clock - start).toString)
System.print(
"long lived tree of depth %(maxDepth) check: %(longLivedTree.check)")
System.print("elapsed: %(System.clock - start)")
+6 -7
View File
@@ -26,8 +26,8 @@ var stretchDepth = maxDepth + 1
var start = System.clock
System.print("stretch tree of depth " + stretchDepth.toString + " check: " +
Tree.new(0, stretchDepth).check.toString)
System.print("stretch tree of depth %(stretchDepth) check: " +
"%(Tree.new(0, stretchDepth).check)")
for (i in 1...1000) System.gc()
var longLivedTree = Tree.new(0, maxDepth)
@@ -45,16 +45,15 @@ while (depth < stretchDepth) {
check = check + Tree.new(i, depth).check + Tree.new(-i, depth).check
}
System.print((iterations * 2).toString + " trees of depth " +
depth.toString + " check: " + check.toString)
System.print("%(iterations * 2) trees of depth %(depth) check: %(check)")
for (i in 1...1000) System.gc()
iterations = iterations / 4
depth = depth + 2
}
System.print("long lived tree of depth " + maxDepth.toString + " check: " +
longLivedTree.check.toString)
System.print(
"long lived tree of depth %(maxDepth) check: %(longLivedTree.check)")
for (i in 1...1000) System.gc()
System.print("elapsed: " + (System.clock - start).toString)
System.print("elapsed: %(System.clock - start)")
+1 -1
View File
@@ -696,4 +696,4 @@ for (i in 0...40) {
}
System.print(total)
System.print("elapsed: " + (System.clock - start).toString)
System.print("elapsed: %(System.clock - start)")
+1 -1
View File
@@ -9,4 +9,4 @@ var start = System.clock
for (i in 1..5) {
System.print(Fib.get(28))
}
System.print("elapsed: " + (System.clock - start).toString)
System.print("elapsed: %(System.clock - start)")
+1 -1
View File
@@ -13,4 +13,4 @@ for (i in 0...100000) {
fibers[0].call()
System.print(sum)
System.print("elapsed: " + (System.clock - start).toString)
System.print("elapsed: %(System.clock - start)")
+1 -1
View File
@@ -7,4 +7,4 @@ var sum = 0
for (i in list) sum = sum + i
System.print(sum)
System.print("elapsed: " + (System.clock - start).toString)
System.print("elapsed: %(System.clock - start)")
+1 -1
View File
@@ -16,4 +16,4 @@ for (i in 1..1000000) {
map.remove(i)
}
System.print("elapsed: " + (System.clock - start).toString)
System.print("elapsed: %(System.clock - start)")
+1 -1
View File
@@ -97,4 +97,4 @@ for (key in keys) {
}
System.print(sum)
System.print("elapsed: " + (System.clock - start).toString)
System.print("elapsed: %(System.clock - start)")
+1 -1
View File
@@ -65,4 +65,4 @@ for (i in 0...n) {
}
System.print(ntoggle.value)
System.print("elapsed: " + (System.clock - start).toString)
System.print("elapsed: %(System.clock - start)")
+1 -1
View File
@@ -18,7 +18,7 @@ for i in range(0, 1000000):
count = count + 1
if "abc" == "abcd":
count = count + 1
if "changed one character" == "changed %ne character":
if "changed one character" == "changed !ne character":
count = count + 1
if "123" == 123: count = count + 1
if "a slightly longer string" == \
+2 -2
View File
@@ -10,7 +10,7 @@ for (i in 1..1000000) {
if ("" == "abc") count = count + 1
if ("abc" == "abcd") count = count + 1
if ("changed one character" == "changed %ne character") count = count + 1
if ("changed one character" == "changed !ne character") count = count + 1
if ("123" == 123) count = count + 1
if ("a slightly longer string" ==
"a slightly longer string!") count = count + 1
@@ -21,4 +21,4 @@ for (i in 1..1000000) {
}
System.print(count)
System.print("elapsed: " + (System.clock - start).toString)
System.print("elapsed: %(System.clock - start)")