SevenBobcat-Java2CppTranslator

The translator supports the translation of a restricted version of Java to C++.

The source language is a restricted version of Java without advanced features like nested classes, anonymous classes, interfaces, enums, annotations, generics, the enhanced for loop, varargs, boxing/unboxing, abstract classes, synchronized methods and statements, strictfp, transient, volatile, lambdas, etc. though. The target language is a restricted version of C++ without virtual methods, inheritance, templates, lambda abstractions, auto, decltype, etc.

The translator is primarily interested in modeling basic translation and support key features of OOP like dynamic dispatch and method overloading. It does not make use of C++’s inheritance to mimic compiler’s implementation of inheretance.

Features

Usage

Call sbt from the terminal in the translator directory and run the following code:

   runxtc -runTranslator src/test/java/inputs/testxxx/Testxxx.java

Replace the xxx in “testxxx” and “Testxxx.java” with the number of the Java class file you want to run. The output.h, output.cpp, and main.cpp in the output folder will then be modified accordingly

Run   formatc to format the C++ files in the output folder to allman style

Run   compilec to compile the C++ files in the output folder

Run   execc to execute the C++ files in the output folder (does NOT support command line options, need to manually execuate output/a.out if command line options are needed)

Run   cleanc to clean the executable file previously generated in the output folder

All supported commands are

Tests

Test Java codes are located in the folder src/test/java/inputs. A collection of 50 standard test cases named test000-test050 are provided. The translator has full support for all of them except test028-test031 due to lack of support to array class method calls and multi-dimension arrays. A single test named test is included to show the most advanced features that the translator supports.

Unit tests are located in the folder src/test/java/edu/nyu/oop.

Design and Implementation

Implementation

It supports inheritance and dynamic dispatch with virtual class table, method overloading with method name mangling, auto memory management with smart pointers using reference counting, imitated Java object initilization process with translated constructor scheme, array translation with custom array templates, and Java package import with modified xtc-demo’s import parser.

Phased Approach

Design Patterns

Design patterns are intensively used throughout the poject. A few representatives are:

It is intensively used for extensible operations to carry our translation. Tree nodes in ASTs are dynamically dispatched according to their types, and different operations are carried on accordingly.

For example, Translator.java provides an interface of using the translator, and delegates actual work to the other constituting parts of the translator, including import parser, Java AST mutator, inheritance relation resolver, C++ printer and others.

It is used naturally by our phrased approach and each phrase is implemented by a few files which take the corresponding responsibility.

Team

Our team consists of Zhanghao Chen, Yiqin Qiu, Hannah Kelly, Zishi Deng, and Pyay Aung San.