Skip to content

axkr/java_codegen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

Repository files navigation

java_codegen - a simple preprocessor for Java source code.

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.

Simple Uppercase Example

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;	} 

Symja math expression code generation

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.

About

A preprocessor for Java source code. Easily extendable by implementing an interface method.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages