Optimizations for f-strings
This commit is contained in:
parent
195bd33a19
commit
8ec3cfe07d
@ -1770,8 +1770,11 @@ static void string(int type) {
|
|||||||
}
|
}
|
||||||
c += 2;
|
c += 2;
|
||||||
} else if (isFormat && *c == '{') {
|
} else if (isFormat && *c == '{') {
|
||||||
emitConstant(OBJECT_VAL(krk_copyString(stringBytes,stringLength)));
|
if (!atLeastOne || stringLength) { /* Make sure there's a string for coersion reasons */
|
||||||
if (atLeastOne) emitByte(OP_ADD);
|
emitConstant(OBJECT_VAL(krk_copyString(stringBytes,stringLength)));
|
||||||
|
if (atLeastOne) emitByte(OP_ADD);
|
||||||
|
atLeastOne = 1;
|
||||||
|
}
|
||||||
stringLength = 0;
|
stringLength = 0;
|
||||||
KrkScanner beforeExpression = krk_tellScanner();
|
KrkScanner beforeExpression = krk_tellScanner();
|
||||||
Parser parserBefore = parser;
|
Parser parserBefore = parser;
|
||||||
@ -1814,7 +1817,7 @@ static void string(int type) {
|
|||||||
error("Expected closing } after expression in f-string");
|
error("Expected closing } after expression in f-string");
|
||||||
goto _cleanupError;
|
goto _cleanupError;
|
||||||
}
|
}
|
||||||
emitByte(OP_ADD);
|
if (atLeastOne) emitByte(OP_ADD);
|
||||||
atLeastOne = 1;
|
atLeastOne = 1;
|
||||||
c++;
|
c++;
|
||||||
} else {
|
} else {
|
||||||
@ -1839,9 +1842,11 @@ static void string(int type) {
|
|||||||
emitConstant(OBJECT_VAL(bytes));
|
emitConstant(OBJECT_VAL(bytes));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
emitConstant(OBJECT_VAL(krk_copyString(stringBytes,stringLength)));
|
if (!isFormat || stringLength || !atLeastOne) {
|
||||||
|
emitConstant(OBJECT_VAL(krk_copyString(stringBytes,stringLength)));
|
||||||
|
if (atLeastOne) emitByte(OP_ADD);
|
||||||
|
}
|
||||||
FREE_ARRAY(char,stringBytes,stringCapacity);
|
FREE_ARRAY(char,stringBytes,stringCapacity);
|
||||||
if (isFormat && atLeastOne) emitByte(OP_ADD);
|
|
||||||
#undef PUSH_CHAR
|
#undef PUSH_CHAR
|
||||||
return;
|
return;
|
||||||
_cleanupError:
|
_cleanupError:
|
||||||
|
6
test/testFStrings.krk
Normal file
6
test/testFStrings.krk
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
print(f"Regular string")
|
||||||
|
print(f"{1 + 2}")
|
||||||
|
print(f"{1 + 2} with a string after")
|
||||||
|
print(f"with a string before {1 + 2}")
|
||||||
|
print(f"with {1+2}{object} nothing in between")
|
||||||
|
print(f"{1 + 2}{3 + 4}{[]}{{}} with some fun expressions")
|
6
test/testFStrings.krk.expect
Normal file
6
test/testFStrings.krk.expect
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
Regular string
|
||||||
|
3
|
||||||
|
3 with a string after
|
||||||
|
with a string before 3
|
||||||
|
with 3<type 'object'> nothing in between
|
||||||
|
37[]{} with some fun expressions
|
Loading…
Reference in New Issue
Block a user