feat: implement string interpolation with %(expr) syntax in compiler and core library

Add lexer support for `TOKEN_INTERPOLATION` to split string literals at interpolation points, introduce `MAX_INTERPOLATION_NESTING` limit of 8, and compile interpolated strings by emitting calls to `String.interpolate_()`. Optimize list and map literal construction with new `addCore_` primitives to reduce stack churn. Update `String`, `List`, and `Map` `toString` methods in core library to use interpolation syntax, and migrate all benchmark and test files from explicit concatenation to interpolated strings.
This commit is contained in:
Bob Nystrom
2015-11-11 15:55:48 +00:00
parent a8961f817e
commit 920b3e2a32
41 changed files with 321 additions and 129 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
class Foo {
construct new() {}
method(a, b) { "method " + a + " " + b }
[a, b] { "subscript " + a + " " + b }
method(a, b) { "method %(a) %(b)" }
[a, b] { "subscript %(a) %(b)" }
}
var foo = Foo.new()
+14 -14
View File
@@ -1,20 +1,20 @@
class Foo {
construct new() {}
+(other) { "infix + " + other }
-(other) { "infix - " + other }
*(other) { "infix * " + other }
/(other) { "infix / " + other }
%(other) { "infix % " + other }
<(other) { "infix < " + other }
>(other) { "infix > " + other }
<=(other) { "infix <= " + other }
>=(other) { "infix >= " + other }
==(other) { "infix == " + other }
!=(other) { "infix != " + other }
&(other) { "infix & " + other }
|(other) { "infix | " + other }
is(other) { "infix is " + other }
+(other) { "infix + %(other)" }
-(other) { "infix - %(other)" }
*(other) { "infix * %(other)" }
/(other) { "infix / %(other)" }
%(other) { "infix \% %(other)" }
<(other) { "infix < %(other)" }
>(other) { "infix > %(other)" }
<=(other) { "infix <= %(other)" }
>=(other) { "infix >= %(other)" }
==(other) { "infix == %(other)" }
!=(other) { "infix != %(other)" }
&(other) { "infix & %(other)" }
|(other) { "infix | %(other)" }
is(other) { "infix is %(other)" }
! { "prefix !" }
~ { "prefix ~" }
+2 -2
View File
@@ -3,8 +3,8 @@ class Foo {
bar { "on instance" }
static bar { "on metaclass" }
bar(arg) { "on instance " + arg }
static bar(arg) { "on metaclass " + arg }
bar(arg) { "on instance %(arg)" }
static bar(arg) { "on metaclass %(arg)" }
}
System.print(Foo.new().bar) // expect: on instance
@@ -1,11 +1,11 @@
class Foo {
construct new() {}
[a] { "1-subscript " + a }
[a, b] { "2-subscript " + a + " " + b }
[a, b, c] { "3-subscript " + a + " " + b + " " + c }
[a]=(value) { "1-subscript setter " + a + " = " + value }
[a, b]=(value) { "2-subscript setter " + a + " " + b + " = " + value }
[a, b, c]=(value) { "3-subscript setter " + a + " " + b + " " + c + " = " + value }
[a] { "1-subscript %(a)" }
[a, b] { "2-subscript %(a) %(b)" }
[a, b, c] { "3-subscript %(a) %(b) %(c)" }
[a]=(value) { "1-subscript setter %(a) = %(value)" }
[a, b]=(value) { "2-subscript setter %(a) %(b) = %(value)" }
[a, b, c]=(value) { "3-subscript setter %(a) %(b) %(c) = %(value)" }
}
var foo = Foo.new()