Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/vm/wren_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -2435,31 +2435,35 @@ static void literal(Compiler* compiler, bool canAssign)
// ["a ", b + c, " d"].join()
static void stringInterpolation(Compiler* compiler, bool canAssign)
{
// Instantiate a new list.
loadCoreVariable(compiler, "List");
callMethod(compiler, 0, "new()", 5);

bool first = true;
do
{
// The opening string part.
literal(compiler, false);
callMethod(compiler, 1, "addCore_(_)", 11);
if (AS_STRING(compiler->parser->previous.value)->length != 0)
{
literal(compiler, false);
if (!first) callMethod(compiler, 1, "+(_)", 4);
first = false;
}

// The interpolated expression.
ignoreNewlines(compiler);
expression(compiler);
callMethod(compiler, 1, "addCore_(_)", 11);
callMethod(compiler, 0, "toString", 8);
if (!first) callMethod(compiler, 1, "+(_)", 4);
first = false;

ignoreNewlines(compiler);
} while (match(compiler, TOKEN_INTERPOLATION));

// The trailing string part.
consume(compiler, TOKEN_STRING, "Expect end of string interpolation.");
literal(compiler, false);
callMethod(compiler, 1, "addCore_(_)", 11);

// The list of interpolated parts.
callMethod(compiler, 0, "join()", 6);
if (AS_STRING(compiler->parser->previous.value)->length != 0)
{
literal(compiler, false);
ASSERT(!first, "Lexer error: invalid string interpolation token chain");
callMethod(compiler, 1, "+(_)", 4);
}
}

static void super_(Compiler* compiler, bool canAssign)
Expand Down
3 changes: 3 additions & 0 deletions src/vm/wren_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,9 @@ DEF_PRIMITIVE(string_startsWith)
DEF_PRIMITIVE(string_plus)
{
if (!validateString(vm, args[1], "Right operand")) return false;

if (AS_STRING(args[0])->length == 0) RETURN_VAL(args[1]);
if (AS_STRING(args[1])->length == 0) RETURN_VAL(args[0]);
RETURN_VAL(wrenStringFormat(vm, "@@", args[0], args[1]));
}

Expand Down
26 changes: 26 additions & 0 deletions test/benchmark/string_interpolation.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var start = System.clock

var string1 = "some random string value"
var string2 = "some random string value"
for (i in 1..100000) {
"%(string1)"
"%(string1) "
" %(string1)"
" %(string1) "
"%(string2)"
"%(string2) "
" %(string2)"
" %(string2) "

"%(string1)%(string2)"
"%(string1)%(string2) "
"%(string1) %(string2)"
"%(string1) %(string2) "
" %(string1)%(string2)"
" %(string1)%(string2) "
" %(string1) %(string2)"
" %(string1) %(string2) "
}

System.print() // Make benchmark.py happy
System.print("elapsed: %(System.clock - start)")
28 changes: 28 additions & 0 deletions test/benchmark/string_interpolation_gc.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var start = System.clock

var string1 = "some random string value"
var string2 = "some random string value"
for (i in 1..100000) {
"%(string1)"
"%(string1) "
" %(string1)"
" %(string1) "
"%(string2)"
"%(string2) "
" %(string2)"
" %(string2) "

"%(string1)%(string2)"
"%(string1)%(string2) "
"%(string1) %(string2)"
"%(string1) %(string2) "
" %(string1)%(string2)"
" %(string1)%(string2) "
" %(string1) %(string2)"
" %(string1) %(string2) "

System.gc()
}

System.print() // Make benchmark.py happy
System.print("elapsed: %(System.clock - start)")
4 changes: 4 additions & 0 deletions util/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def BENCHMARK(name, pattern):

BENCHMARK("string_equals", r"""3000000""")

BENCHMARK("string_interpolation", "")

BENCHMARK("string_interpolation_gc", "")

LANGUAGES = [
("wren", [os.path.join(WREN_BIN, 'wren_test')], ".wren"),
("dart", ["fletch", "run"], ".dart"),
Expand Down