Hi,
Let me give you a bit of background; I hope that will explain.
Any C++ interpreter is not a compiler: compilers cannot deal with
// MyCode.cxx
int sum = 0;
printf("sum before: %d\n", sum);
for (int i = 0; i < 12; ++i) {
printf("%d\n", i);
sum += i;
}
sum
It can’t because
- printf is not declared (okay, details)
- that for loop is not legal on file scope.
- “sum”?!
So yes, we do have a few extensions - already making C++ code available to the compiler line by line is a huge extension: a compiler expects to see the “full thing” (which is so crucial that it even has its own name: the translation unit) in one go, and does certain operations only after having seen the whole translation unit.
We wrap expressions into functions to make them executable. This converts
printf("sum before: %d\n", sum);
into something similar to
void f() {
printf("sum before: %d\n", sum);
;
}
We then just-in-time compile f() and call it. There are a few tricks we need to play - e.g. naively the declaration of “int sum” would not be visible outside that wrapper function.
As you know, “sum” means “and print the value of sum”. But that’s of course ill formed:
void f() {
sum
}
so we just paste an extra ‘;’ after each input line; if the input ends with an empty statement like such:
void f() {
sum;
; // this is an empty statement
}
then we don’t print the value of sum, else we do. Now you see the connection of why that for-loop continuation breaks: we convert it into
void f() {
for(unsigned int i = 0; i < 10; i++)
;
}
and that’s perfectly legal code, no continuation needed. Indeed it’s a cling bug. If you have an idea how to detect / fix / circumvent this then let us know!
Note that the ‘;’ pasting happens before parsing, i.e. we do not know yet that we have a for loop - so while we could paste the ‘;’ conditionally based on the previous statement, we would have to do a string analysis of the input - which is pretty terrible and fragile; think of someone using a do-while loop, or the C++17 range-based for coming in, or fancy nested templates in the for expressions that disguise as comparisons, or preprocessor macros getting involved.
Sorry for the long post - but now we are all on the same page and we might hear your ideas to find a solution!
Cheers, Axel.