XTRAN Example — Match & Replace C, C++, Java, or C# Expressions

In the rules below:

Note that XTRAN was told, using a rules language recursive iterator statement, to automatically apply the rules below recursively to every expression in the code being re-engineered.

Process Flowchart

Here is a flowchart for this process, in which the elements are color coded:

process flowchart

XTRAN rules:

The following rules effectively convert infix expression notation to prefix notation.

Original expression Replacement expression
 <expr1> + <expr2>   add(<expr1>, <expr2>) 
 <expr1> - <expr2>   sub(<expr1>, <expr2>) 
 <expr1> * <expr2>   mul(<expr1>, <expr2>) 
 <expr1> / <expr2>   div(<expr1>, <expr2>) 
 <expr1> = <expr2>   copy(&<expr1>, <expr2>) 

Original code:

     a = b + (c - d) * (e - f) / g;

Re-engineered code:

     copy(&a, add(b, div(mul(sub(c, d), sub(e, f)), g)));

XTRAN rule:

In the following rule, the match of a "wildcard" pattern element has a qualifying condition; if that condition evaluates to false, the match fails at that element.

Original expression Replacement expression   Qualifying condition
 copy(<expr1>, <expr2>)   *<expr1> = <expr2>   <expr2> is not an operator expression 

Original code:

     copy(p_var1, *p_var2);
     copy(p_var3, var4);
     copy(p_var5, var6 + var7);
     copy(p_var8 + 3, var9);

Re-engineered code:

     copy(p_var1, *p_var2);
     *p_var3 = var4;
     copy(p_var5, var6 + var7);
     *(p_var8 + 3) = var9;