I've been given a task/assigment to write a program that reads a c-like code (with specified syntax, so I dont need to worry about code in comments etc) and removes unnecessary calculations from inside the loop body to the outside. It can create new variables. Like in this example:
Input:
for (i=1; i<100; i++)
{ b[i] = c[i] *a * 135.8; }
Output:
float __gen1 = a*135.8;
for (i=1; i<100; i++)
{ b[i] = c[i] *___gen1; }
I decided that the best aproach would be to run an 'optimize' recursive function, this way dealing with the nested loops would be easy. But how should I go about reading the code in the loop and deciding what to move out, and how to move it out outside the loop body?
I'm using C#.