A example what i want to achieve, but i’m stucked.
Any help would be appreciated.
A example what i want to achieve, but i’m stucked.
Any help would be appreciated.
You do realize that to do this, you’d have to basically build a full parser that understand the C-code, and can analyze and reason about the code’s flow, and then rebuild the same flow using the state-machine you want. It has to manage things like break;
, goto
and continue
in the loops. It’s really simple to do for the case you proposed, by try to do it for a a REAL piece of code that actually does something more complex than produce a constant of 5050 in s
(your first example will compile to “nothing” if you don’t add a something that uses s
, and if you add printf("%d\n", s);
, it will calculate 5050 as a compile-time constant, because clang understands "calculate the sum of all numbers from 1…n and performas n*(n-1)/2 as a constant calculation - even if you use a unknown value to the compiler, it just does that simple math).
And for C++ with scoping and destruction, you’d also have to deal with keeping track of the life-time of variables with non-trivial constructor/destructors (such as std::lock
)