The axkr/java_codegen Github project contains a simple preprocessor tool to expand ordinary strings into Java source code.
The strings are included into Java line comments opened by // [$ ... $]
or //[$ ... $]
and closed by // $$
or //$$
. The preprocessor program takes this prepared files and substitutes the comments by generated source code.
There is a simple example org.matheclipse.tools.HelloWorldExample which converts the following lines:
public class HelloWorldExample extends AbstractCodeGenerator { public final static String HELLO_WORLD1 = // // [$ hello world 1 $] "test 1"; // $$; public final static String HELLO_WORLD2 = // // [$ hello world 2 $] "test 2"; // $$;
into upper case strings in these lines:
public class HelloWorldExample extends AbstractCodeGenerator { public final static String HELLO_WORLD1 = // // [$ hello world 1 $] "HELLO WORLD 1"; // $$; public final static String HELLO_WORLD2 = // // [$ hello world 2 $] "HELLO WORLD 2"; // $$;
Note: the ;
character behind the closing // $$
tag will be appended to the generated source code.
After running the HelloWorldExample
you can use the Eclipse menu "Copy qualified name" for the file which you would like to convert:
Input qualified Java file name for converting text to upper case strings ▶ /java_codegen/src/org/matheclipse/tools/HelloWorldExample.java
For implementing your own conversion you have to implement the abstract apply()
method:
public boolean apply(String command, StringBuilder buf) { command = command.trim(); buf.append("\"" + command.toUpperCase() + "\""); return true; }
There is already a more sophisticated tool ExprPreprocessor in the Symja project, which converts math expressions into Java source code. Because Java doesn't support operator overloading the tool simplifies the maintenance of large math expressions.
In the example from the FunctionExpand function the Symja expression Gamma(1+x)
MATCHER.caseOf(Factorial(x_), // // [$ Gamma(1+x) $] F.Null); // $$);
will be converted into special Symja Java source code
MATCHER.caseOf(Factorial(x_), // // [$ Gamma(1+x) $] F.Gamma(F.Plus(F.C1, x))); // $$);
Note: the );
characters behind the closing // $$
tag will be appended to the generated source code.