Support = to print f-string expression alongside value

This commit is contained in:
K. Lange 2022-05-23 08:42:40 +09:00
parent 78e85ce873
commit ece7da299a
3 changed files with 17 additions and 0 deletions

View File

@ -2521,6 +2521,7 @@ static void string(int exprType) {
if (atLeastOne) emitByte(OP_ADD);
atLeastOne = 1;
}
const char * start = c+1;
stringLength = 0;
KrkScanner beforeExpression = krk_tellScanner();
Parser parserBefore = parser;
@ -2537,6 +2538,15 @@ static void string(int exprType) {
parser = parserBefore;
c = inner.start;
KrkToken which = syntheticToken("str");
int hasEq = 0;
while (*c == ' ') c++;
if (*c == '=') {
c++;
while (*c == ' ') c++;
emitConstant(OBJECT_VAL(krk_copyString(start,c-start)));
emitByte(OP_SWAP);
hasEq = 1;
}
if (*c == '!') {
c++;
/* Conversion specifiers, must only be one */
@ -2563,6 +2573,7 @@ static void string(int exprType) {
error("Expected closing '}' after expression in f-string");
goto _cleanupError;
}
if (hasEq) emitByte(OP_ADD);
if (atLeastOne) emitByte(OP_ADD);
atLeastOne = 1;
c++;

View File

@ -0,0 +1,3 @@
print(f'{min([1,2,3]) = }')
print(f'{"test" = !r}')
print(f'abc {123 = } def {456 = } ghi')

View File

@ -0,0 +1,3 @@
min([1,2,3]) = 1
"test" = 'test'
abc 123 = 123 def 456 = 456 ghi