Thanks to visit codestin.com
Credit goes to github.com

Skip to content

polystat/j2eo

Repository files navigation

J2EO: Java to EO Translator

eolang icon

Gradle Build LINE BRANCH COMPLEXITY

Hits-of-Code Lines of code

This is a translator of Java programming language to EOLANG programming language.

Usage

  1. Make sure you have installed:

    • Java 16+ (make sure command java -version shows 16+ version of Java in terminal if you have multiple Java version installed)
    • Maven 3.8+ to run tests (be aware of possible conflicts of the latest versions of Maven and Java on some OSs)
    • ANTLR4 4.9.3 (if you want to build the parser on your own. If you don't have ANTLR, you still can build project using bundled version of parser.)
  2. Clone the repo into your folder:

    HTTPS:

    git clone https://github.com/polystat/j2eo.git
    cd ./j2eo

    or SSH:

    git clone [email protected]:polystat/j2eo.git
    cd ./j2eo
  3. Build the project:

    ./build.sh
  4. After build process, j2eo.jar file will appear in the project root folder (./j2eo). With this file, is it possible to translate .java files or packages to .eo packages. Run:

    java -jar j2eo.jar <source .java file or directory with Java files> -o <output directory>

    to get the translation.

    For example,

    java -jar j2eo.jar src/test/resources/SimpleTest.java -o output_eo

    or

    java -jar j2eo.jar src/test/resources/polystat_tests/test1 -o output_eo

Running translator on Hadoop

Hadoop is a large Java project (contains ~1.8M lines of code as of time of writing this). We included it as a benchmark of the translator.

Repository contains a script to build J2EO, download Hadoop repo and run J2EO on it.

Usage:

./test-hadoop.sh

It will download zipped hadoop and unpack it (in a separate folder) into ../j2eo-data relative to the project's root. Next, it will put the If you no more need that folder, run

rm -rf ../j2eo-data

Motivation

This project is a part of Polystat project, the goal of which is to statically analyze different languages using EOLANG, the implementation of phi-calculus. In order to do that, the first step is to convert source language into EO. This particular repository contains translator from Java to EO.


Decisions

Q: Why do we implement yet another Java parser?

A: Publicly available parsers only support older versions of Java, while we aim to support the latest version ( currently 16). Thus, we had to create our own parser.

Also in recent versions, external Java grammar implemented in ANTLR was added as an alternative. It claims to support Java 17, and it does, as for our testing on big projects.


Q: Why do we implement EO AST?

A: Working with AST instead of raw strings allows utilization of Java compiler's type checking to minimize amount of bugs in our code. It is also much easier to work with abstraction layer than with strings.



How does it work?

  • First, the Java source code files are parsed recursively.
  • Then, for each file, translator converts Java AST to EO AST.
  • Then, EO AST is printed out as a source code to output directory in the same directory structure.

Troubleshooting

Java

Make sure you have these in sync (mentioning (not pointing to) the same jdk directory)

  • which java
  • which javac
    • configure alternatives in case of mismatch (link)
  • echo $JAVA_HOME
    • See how to set $JAVA_HOME (link)
    • If it still points to a wrong directory, see where you might have set it (link) and edit that place

Awesome Bugs translation

Awesome bugs repo

.java files:

Translations:


Examples of Java to EOLang translation

We use Java language specification document as a foundation for Java feature hierarchy.
Java 16 language specification: see .pdf file

###Ch. 4 - Types, Values, and Variables

  • Increment operator: Java to EO

###Ch. 5 - Conversions and Contexts

###Ch. 6 - Names

  • A simple declaration: Java to EO

###Ch. 7 - Packages and Modules WIP

###Ch. 8 - Classes

  • Method class member: Java to EO
  • Field initialization: Java to EO
  • Method declaration: Java to EO
  • Inner class: Java to EO

###Ch. 9 - Interfaces

###Ch. 10 - Arrays

  • Primitive array declaration: Java to EO

###Ch. 11 - Exceptions WIP

###Ch. 14 - Block Statements, and Patterns

###Ch. 15 - Expressions

  • Left-hand operands are evaluated first: Java to EO
  • Integer literal: Java to EO
  • Complex parenthesized expression: Java to EO
  • Creation of a simple integer array: Java to EO
  • Postfix increment: Java to EO
  • Unary plus operator: Java to EO
  • Multiplication operator: Java to EO
  • Variable right shift: Java to EO
  • Greater operator: Java to EO
  • Assignment operator: Java to EO

###Ch. 16 - Definite Assignments WIP

###Ch. 18 - Type inference WIP

About

Experimental Transpiler of Java Programs to EO Programming Language

Topics

Resources

License

Stars

Watchers

Forks

Contributors 13