Skip to content

Commit

Permalink
Merge pull request #19 from polystat/develop
Browse files Browse the repository at this point in the history
Working version
  • Loading branch information
IamMaxim authored Oct 19, 2021
2 parents 7346153 + 197d8f0 commit 776acd2
Show file tree
Hide file tree
Showing 246 changed files with 3,710 additions and 2,949 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ out
.gradle
.idea
build
compile_and_run_tests.bat
j2eo.jar
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ bugs in our code. It is also much easier to work with abstraction layer than wit

## Usage

> ⚠️ NOTE: for now, the entire pipeline is not available. Project is Work In Progress.
To build, run:

```shell
java -jar j2eo.jar <source package directory> -o <output directory>
./build.sh
```

To execute, run:

```shell
java -jar j2eo.jar <source .java file> -o <output .eo file>
```

---
Expand All @@ -50,6 +56,10 @@ java -jar j2eo.jar <source package directory> -o <output directory>
- 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.

## What's inside



## NOT covered Java features list

- Type Erasure
Expand All @@ -64,5 +74,9 @@ java -jar j2eo.jar <source package directory> -o <output directory>
- ``try``/``catch`` blocks
- ``yeild`` feature
- Threads and Locks
- Generics (all kinds of them)
- Native methods
- break and continue statements
- RTTI (instanceof operator) ??????

In general, we cover **91 feature of 112** described in the Java language specification.
108 changes: 67 additions & 41 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import org.apache.tools.ant.taskdefs.condition.Os
import java.security.MessageDigest
import org.gradle.jvm.tasks.Jar


plugins {
Expand Down Expand Up @@ -27,8 +28,29 @@ repositories {
}

dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.0")
implementation("commons-cli:commons-cli:1.4")

testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
}


val fatJar = task("fatJar", type = Jar::class) {
// baseName = "${project.name}-fat"
// manifest Main-Class attribute is optional.
// (Used only to provide default main class for executable jar)
manifest {
attributes["Main-Class"] = "main.Main" // fully qualified class name of default main class
}
// Include dependencies
from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) })
with(tasks["jar"] as CopySpec)
}

tasks {
"build" {
dependsOn(fatJar)
}
}

tasks.withType(JavaCompile::class).configureEach {
Expand Down Expand Up @@ -72,45 +94,49 @@ fun createOutDirs() {
* Runs Bison using OS-specific shell command.
*/
fun runBison() =
when {
Os.isFamily(Os.FAMILY_WINDOWS) ->
exec {
workingDir = File(".")
executable = "bin/win_bison.exe"
args = mutableListOf(
"--report=states,lookaheads",
// "-r", "all",
// "--debug", "--help", "--stacktrace",
"--report-file=${reportFilePath}",
"--output=${javaParserFilePath}",
javaGrammarFilePath
)
}
Os.isFamily(Os.FAMILY_MAC) ->
exec {
workingDir = File(".")
executable = "bin/bison_mac"
args = mutableListOf(
"-r", "all",
"--report-file=${reportFilePath}",
"--output=${javaParserFilePath}",
javaGrammarFilePath
)
}
Os.isFamily(Os.FAMILY_UNIX) ->
exec {
workingDir = File(".")
executable = "bison"
args = mutableListOf(
"-r", "all",
"--report-file=${reportFilePath}",
"--output=${javaParserFilePath}",
javaGrammarFilePath
)
}
else ->
throw kotlin.UnsupportedOperationException("Your OS is not yet supported. File a GitHub or issue or " +
"provide a Pull Request with support for Bison execution for your OS.")
try {
when {
Os.isFamily(Os.FAMILY_WINDOWS) ->
exec {
workingDir = File(".")
executable = "bin/win_bison.exe"
args = mutableListOf(
"--report=states,lookaheads",
// "-r", "all",
// "--debug", "--help", "--stacktrace",
"--report-file=${reportFilePath}",
"--output=${javaParserFilePath}",
javaGrammarFilePath
)
}
Os.isFamily(Os.FAMILY_MAC) ->
exec {
workingDir = File(".")
executable = "bin/bison_mac"
args = mutableListOf(
"--report=states,lookaheads",
"--report-file=${reportFilePath}",
"--output=${javaParserFilePath}",
javaGrammarFilePath
)
}
Os.isFamily(Os.FAMILY_UNIX) ->
exec {
workingDir = File(".")
executable = "bison"
args = mutableListOf(
"--report=states,lookaheads",
"--report-file=${reportFilePath}",
"--output=${javaParserFilePath}",
javaGrammarFilePath
)
}
else ->
throw kotlin.UnsupportedOperationException("Your OS is not yet supported. File a GitHub or issue or " +
"provide a Pull Request with support for Bison execution for your OS.")
}
} catch (e: Exception) {
e.printStackTrace()
}


Expand Down
4 changes: 4 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#/bin/sh

./gradlew fatJar
cp build/libs/* j2eo.jar
15 changes: 14 additions & 1 deletion src/main/java/eotree/EOBndExpr.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package eotree;

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class EOBndExpr extends EOBnd {
public EOBndName bndName;

Expand All @@ -10,6 +14,15 @@ public EOBndExpr(EOExpr expr, EOBndName bndName) {

@Override
public String generateEO(int indent) {
return expr.generateEO(indent) + " > " + bndName.generateEO(0);
var lines = expr.generateEO(indent).split("\n");
// Append attribute name to the first line
return Stream.concat(
Arrays.stream(lines)
.findFirst()
.map(line -> line + " > " + bndName.generateEO(0))
.stream(),
Arrays.stream(lines)
.skip(1)
).collect(Collectors.joining("\n"));
}
}
2 changes: 1 addition & 1 deletion src/main/java/eotree/EOMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* EBNF representation:
* <code>
* '+' name '' ANY EOL
* '+' name ' ' ANY EOL
* </code>
*/
public class EOMeta extends EONode {
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/eotree/EOObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ public class EOObject extends EOExpr {
public List<EOBndName> freeAttrs;
public Optional<EOBndName> varargAttr;
public List<EOBndExpr> bndAttrs;
public Optional<String> name;

public EOObject(List<EOBndName> freeAttrs,
Optional<EOBndName> varargAttr,
List<EOBndExpr> bndAttrs,
Optional<String> name) {
List<EOBndExpr> bndAttrs) {
this.freeAttrs = freeAttrs;
this.varargAttr = varargAttr;
this.bndAttrs = bndAttrs;
this.name = name;
}

@Override
Expand All @@ -29,11 +26,7 @@ public String generateEO(int indent) {
(varargAttr
.map(attr -> " " + attr.generateEO(indent) + "...")
.orElse("")) +
"]" +
(name
.map(n -> " > " + name.get())
.orElse("")
) + "\n" +
"]\n" +
bndAttrs.stream()
.map(attr -> attr.generateEO(indent + 1))
.collect(Collectors.joining("\n"));
Expand Down
56 changes: 51 additions & 5 deletions src/main/java/lexer/Scanner.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package lexer;

import parser.JavaParser;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Scanner
public class Scanner implements JavaParser.Lexer
{
private static String sourcePath;
private static char[] sourceText;
Expand Down Expand Up @@ -47,6 +49,32 @@ private static void forgetChar()
currentChar = '\0';
}

// PUBLIC SCANNER INTERFACE /////////////////////////////

private Token lastToken;

public int yylex()
{
lastToken = getToken();

System.out.println(lastToken.code);

return lastToken.code.value();
}

public Token getLVal()
{
return lastToken;
}

@Override
public void yyerror(String msg) {
System.out.println(msg);
}

/////////////////////////////////////////////////////////////


// Detecting the current token //////////////////////////////

private static Token currentToken;
Expand Down Expand Up @@ -79,6 +107,23 @@ private static Token getToken()
}
switch ( ch )
{
case '"': // String literal
forgetChar();
String str = "";
while ( true )
{
ch = getChar();
if ( ch == '"' )
{
forgetChar();
break;
}
str += ""+ch;
forgetChar();
}
code = TokenCode.StringLiteral;
image = str;
break;
case ':' : // : ::
forgetChar(); ch = getChar();
if ( ch == ':' ) { forgetChar(); code = TokenCode.DoubleColon; image = "::"; }
Expand Down Expand Up @@ -106,7 +151,7 @@ private static Token getToken()
case ']': forgetChar(); code = TokenCode.RightBracket; image = "]"; break;
case '{': forgetChar(); code = TokenCode.LeftBrace ; image = "{"; break;
case '}': forgetChar(); code = TokenCode.RightBrace; image = "}"; break;
case '@': forgetChar(); code = TokenCode.Ampersand; image = "@"; break;
case '@': forgetChar(); code = TokenCode.At; image = "@"; break;
case '~': forgetChar(); code = TokenCode.Tilde; image = "~"; break;

case '*': // * *=
Expand Down Expand Up @@ -303,12 +348,11 @@ private static TokenCode detectKeyword(String identifier)
case "boolean" : return TokenCode.Boolean;
case "break" : return TokenCode.Break;
case "byte" : return TokenCode.Byte;
case "base" : return TokenCode.Base;
case "catch" : return TokenCode.Catch;
case "case" : return TokenCode.Case;
case "char" : return TokenCode.Char;
case "class" : return TokenCode.Class;
case "const" : return TokenCode.Const; // not actually used
// case "const" : return TokenCode.Const; // not actually used
case "continue" : return TokenCode.Continue;
case "default" : return TokenCode.Default;
case "do" : return TokenCode.Do;
Expand All @@ -320,7 +364,7 @@ private static TokenCode detectKeyword(String identifier)
case "finally" : return TokenCode.Finally;
case "float" : return TokenCode.Float;
case "for" : return TokenCode.For;
case "goto" : return TokenCode.Goto; // not actually used
// case "goto" : return TokenCode.Goto; // not actually used
case "if" : return TokenCode.If;
case "implements": return TokenCode.Implements;
case "import" : return TokenCode.Import;
Expand Down Expand Up @@ -354,6 +398,8 @@ private static TokenCode detectKeyword(String identifier)
case "false" : return TokenCode.False;
case "null" : return TokenCode.Null;
case "var" : return TokenCode.Var;
case "yield" : return TokenCode.Yield;
case "record" : return TokenCode.Record;

/*
open,
Expand Down
Loading

1 comment on commit 776acd2

@0pdd
Copy link
Member

@0pdd 0pdd commented on 776acd2 Oct 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to retrieve PDD puzzles from the code base and submit them to GitHub. If you think that it's a bug on our side, please submit it to yegor256/0pdd:

set -x && set -e && set -o pipefail && cd /tmp/0pdd20210801-12-f1ew7f/polystat/j2eo && pdd -v -f /tmp/20211019-30326-144pebo [1]: + set -e + set -o pipefail + cd /tmp/0pdd20210801-12-f1ew7f/polystat/j2eo + pdd -v -f /tmp/20211019-30326-144pebo My version is 0.20.6 Ruby version is 2.6.0 at...

Please, copy and paste this stack trace to GitHub:

UserError
set -x && set -e && set -o pipefail && cd /tmp/0pdd20210801-12-f1ew7f/polystat/j2eo && pdd -v -f /tmp/20211019-30326-144pebo [1]:
+ set -e
+ set -o pipefail
+ cd /tmp/0pdd20210801-12-f1ew7f/polystat/j2eo
+ pdd -v -f /tmp/20211019-30326-144pebo

My version is 0.20.6
Ruby version is 2.6.0 at x86_64-linux
Reading /tmp/0pdd20210801-12-f1ew7f/polystat/j2eo
695 file(s) found, 553 excluded
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/bin/custom_build_rules/docs/Verbosity.png is a binary file (29722 bytes)
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/bin/custom_build_rules/docs/FlexProperties.png is a binary file (26767 bytes)
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/bin/custom_build_rules/docs/3.png is a binary file (7316 bytes)
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/bin/custom_build_rules/docs/6.png is a binary file (15611 bytes)
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/bin/custom_build_rules/docs/BisonProperties.png is a binary file (27186 bytes)
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/bin/custom_build_rules/docs/2.png is a binary file (11445 bytes)
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/bin/custom_build_rules/docs/Flex_debuging.png is a binary file (27219 bytes)
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/bin/custom_build_rules/docs/Properties.png is a binary file (31446 bytes)
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/bin/custom_build_rules/docs/4.png is a binary file (12213 bytes)
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/bin/custom_build_rules/docs/1.png is a binary file (31654 bytes)
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/bin/custom_build_rules/docs/5.png is a binary file (11534 bytes)
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/bin/win_bison.exe is a binary file (665600 bytes)
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/bin/win_flex.exe is a binary file (569344 bytes)
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/bin/bison_mac is a binary file (487680 bytes)
/tmp/0pdd20210801-12-f1ew7f/polystat/j2eo/gradle/wrapper/gradle-wrapper.jar is a binary file (59203 bytes)
Reading bin/changelog.md...
Reading bin/custom_build_rules/README.md...
Reading bin/custom_build_rules/win_flex_only/win_flex_custom_build.targets...
Reading bin/custom_build_rules/win_flex_only/win_flex_custom_build.props...
Reading bin/custom_build_rules/win_flex_only/win_flex_custom_build.xml...
Reading bin/custom_build_rules/win_bison_only/win_bison_custom_build.xml...
Reading bin/custom_build_rules/win_bison_only/win_bison_custom_build.props...
Reading bin/custom_build_rules/win_bison_only/win_bison_custom_build.targets...
Reading bin/custom_build_rules/win_flex_bison/win_flex_bison_custom_build.props...
Reading bin/custom_build_rules/win_flex_bison/win_flex_bison_custom_build.targets...
Reading bin/custom_build_rules/win_flex_bison/win_flex_bison_custom_build.xml...
Reading bin/data/local.mk...
Reading bin/data/README.md...
Reading bin/data/bison-default.css...
Reading bin/data/m4sugar/m4sugar.m4...
Reading bin/data/m4sugar/foreach.m4...
Reading bin/data/skeletons/java-skel.m4...
Reading bin/data/skeletons/location.cc...
Reading bin/data/skeletons/lalr1.java...
Reading bin/data/skeletons/c++.m4...
Reading bin/data/skeletons/traceon.m4...
Reading bin/data/skeletons/glr.cc...
Reading bin/data/skeletons/glr.c...
Reading bin/data/skeletons/c++-skel.m4...
Reading bin/data/skeletons/d.m4...
ERROR: bin/data/skeletons/d.m4; puzzle at line #298; TODO found, but puzzle can't be parsed, most probably because TODO is not followed by a puzzle marker, as this page explains: https://github.com/yegor256/pdd#how-to-format
If you can't understand the cause of this issue or you don't know how to fix it, please submit a GitHub issue, we will try to help you: https://github.com/yegor256/pdd/issues. This tool is still in its beta version and we will appreciate your feedback. Here is where you can find more documentation: https://github.com/yegor256/pdd/blob/master/README.md.
Exit code is 1

/app/objects/git_repo.rb:66:in `rescue in block in xml'
/app/objects/git_repo.rb:63:in `block in xml'
/app/vendor/ruby-2.6.0/lib/ruby/2.6.0/tempfile.rb:295:in `open'
/app/objects/git_repo.rb:62:in `xml'
/app/objects/puzzles.rb:36:in `deploy'
/app/objects/job.rb:38:in `proceed'
/app/objects/job_starred.rb:33:in `proceed'
/app/objects/job_recorded.rb:32:in `proceed'
/app/objects/job_emailed.rb:35:in `proceed'
/app/objects/job_commiterrors.rb:36:in `proceed'
/app/objects/job_detached.rb:48:in `exclusive'
/app/objects/job_detached.rb:36:in `block in proceed'
/app/objects/job_detached.rb:36:in `fork'
/app/objects/job_detached.rb:36:in `proceed'
/app/0pdd.rb:357:in `block in <top (required)>'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1675:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1675:in `block in compile!'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1013:in `block (3 levels) in route!'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1032:in `route_eval'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1013:in `block (2 levels) in route!'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1061:in `block in process_route'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1059:in `catch'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1059:in `process_route'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1011:in `block in route!'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1008:in `each'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1008:in `route!'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1129:in `block in dispatch!'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1101:in `block in invoke'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1101:in `catch'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1101:in `invoke'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1124:in `dispatch!'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:939:in `block in call!'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1101:in `block in invoke'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1101:in `catch'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1101:in `invoke'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:939:in `call!'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:929:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/rack-protection-2.1.0/lib/rack/protection/xss_header.rb:18:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/rack-protection-2.1.0/lib/rack/protection/path_traversal.rb:16:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/rack-protection-2.1.0/lib/rack/protection/json_csrf.rb:26:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/rack-protection-2.1.0/lib/rack/protection/base.rb:50:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/rack-protection-2.1.0/lib/rack/protection/base.rb:50:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/rack-protection-2.1.0/lib/rack/protection/frame_options.rb:31:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/rack-2.2.3/lib/rack/logger.rb:17:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/rack-2.2.3/lib/rack/common_logger.rb:38:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:253:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:246:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/rack-2.2.3/lib/rack/head.rb:12:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/rack-2.2.3/lib/rack/method_override.rb:24:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:216:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1991:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1542:in `block in call'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1769:in `synchronize'
/app/vendor/bundle/ruby/2.6.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1542:in `call'
/app/vendor/bundle/ruby/2.6.0/gems/rack-2.2.3/lib/rack/handler/webrick.rb:95:in `service'
/app/vendor/ruby-2.6.0/lib/ruby/2.6.0/webrick/httpserver.rb:140:in `service'
/app/vendor/ruby-2.6.0/lib/ruby/2.6.0/webrick/httpserver.rb:96:in `run'
/app/vendor/ruby-2.6.0/lib/ruby/2.6.0/webrick/server.rb:307:in `block in start_thread'

Please sign in to comment.