Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#329): Print Java Streams as PHI Expressions #340

Merged
merged 21 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0d04ba7
feat(#329): add Streams -> PHI integration test
volodya-lombrozo Jul 9, 2024
491fece
feat(#329): make 'streams' test pass
volodya-lombrozo Jul 9, 2024
5b4e635
feat(#329): change the code in 'streams' integration test
volodya-lombrozo Jul 9, 2024
31a800f
feat(#329): add Return ast node
volodya-lombrozo Jul 9, 2024
2faffec
feat(#329): apply new Return AST node to ReturnHandler
volodya-lombrozo Jul 9, 2024
7175e64
feat(#329): add Typed interface for Multiplication node
volodya-lombrozo Jul 9, 2024
6c72bdb
feat(#329): Add CheckCast ast node
volodya-lombrozo Jul 9, 2024
7e7917a
feat(#329): integrate CheckCast handler
volodya-lombrozo Jul 9, 2024
fc1ffa1
feat(#329): add Handle and DynamicInvocation classes
volodya-lombrozo Jul 10, 2024
e4c20f5
feat(#329): convert DynamicInvocation back to opcodes
volodya-lombrozo Jul 10, 2024
b42815e
feat(#329): fix all unit tests
volodya-lombrozo Jul 10, 2024
7a4d61b
feat(#329): fix small problems with CheckCast and ArrayConstructor types
volodya-lombrozo Jul 10, 2024
9a930f9
feat(#329): identify the problem with decompilation/compilation of IN…
volodya-lombrozo Jul 11, 2024
72d4a11
feat(#329): fix bug with CheckCast parsing
volodya-lombrozo Jul 11, 2024
59a043b
feat(#329): enable 'streams' integration test
volodya-lombrozo Jul 11, 2024
bbff597
feat(#329): repair all the tests including unit tests and integration…
volodya-lombrozo Jul 11, 2024
5100308
feat(#329): add one more puzzle to solve
volodya-lombrozo Jul 11, 2024
a1ee4f2
feat(#329): add javadocs
volodya-lombrozo Jul 11, 2024
b8d9990
feat(#329): fix all linter offences
volodya-lombrozo Jul 11, 2024
3b86ff8
feat(#329): improve 'streams' pom.xml formatting
volodya-lombrozo Jul 11, 2024
7d0623d
feat(#329): rename MulTest -> MultiplicationTest
volodya-lombrozo Jul 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/it/streams/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## Streams to PHI

This integration tests verifies how the java streams are transformed into the
PHI expressions.
To run only this test, use the following command:

```shell
mvn clean integration-test -Dinvoker.test=streams -DskipTests
```

The discussion related to this test is presented in the
GitHub [issue](https://github.com/objectionary/opeo-maven-plugin/issues/329).
So, if you have any questions, fill free to ask them there or raise a new issue.

_____

Here we are tying to verify how the Java Streams are transformed into the PHI
expressions, particularly the following code:

```java
String[] strings = new String[10];
for (int i = 0; i < strings.length; i++) {
strings[i] = String.valueOf(i);
}
int sum = Arrays.stream(strings)
.filter(s -> !s.equals(""))
.mapToInt(s -> Integer.parseInt(s))
.sum();
System.out.println(sum);
```

You can find the original code in the
[Main.java](src/main/java/org/eolang/streams/Main.java) class
1 change: 1 addition & 0 deletions src/it/streams/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=verify
149 changes: 149 additions & 0 deletions src/it/streams/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License (MIT)

Copyright (c) 2016-2023 Objectionary.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.eolang</groupId>
<artifactId>benchmark</artifactId>
<packaging>jar</packaging>
<version>0.0.0</version>
<name>benchmark</name>
<url>https://github.com/objectionary/benchmark</url>
<description>
Integration test that checks java streams transformation to PHI expressions.
To run this test, use the following command:
"mvn clean integration-test -Dinvoker.test=streams -DskipTests"
</description>
<inceptionYear>2024</inceptionYear>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<jeo.version>0.5.0</jeo.version>
<opeo.version>@project.version@</opeo.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.eolang</groupId>
<artifactId>jeo-maven-plugin</artifactId>
<version>${jeo.version}</version>
<executions>
<execution>
<id>bytecode-to-eo</id>
<goals>
<goal>disassemble</goal>
</goals>
<configuration>
<outputDir>${project.build.directory}/generated-sources/jeo-decompile-xmir</outputDir>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eolang</groupId>
<artifactId>opeo-maven-plugin</artifactId>
<version>@project.version@</version>
<!-- <configuration>-->
<!-- <disabled>true</disabled>-->
<!--</configuration>-->
<executions>
<execution>
<id>opeo-decompile</id>
<goals>
<goal>decompile</goal>
</goals>
<configuration>
<sourcesDir>${project.build.directory}/generated-sources/jeo-decompile-xmir</sourcesDir>
<outputDir>${project.build.directory}/generated-sources/opeo-decompile-xmir</outputDir>
<modifiedDir>${project.build.directory}/generated-sources/opeo-decompile-modified-xmir</modifiedDir>
</configuration>
</execution>
<execution>
<id>opeo-compile</id>
<phase>generate-test-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourcesDir>${project.build.directory}/generated-sources/opeo-decompile-xmir</sourcesDir>
<outputDir>${project.build.directory}/generated-sources/opeo-compile-xmir</outputDir>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eolang</groupId>
<artifactId>eo-maven-plugin</artifactId>
<version>0.38.4</version>
<executions>
<execution>
<id>convert-xmir-to-phi</id>
<phase>process-classes</phase>
<goals>
<goal>xmir-to-phi</goal>
</goals>
<configuration>
<phiInputDir>${project.build.directory}/generated-sources/opeo-decompile-xmir</phiInputDir>
<phiOutputDir>${project.build.directory}/generated-sources/phi-expressions</phiOutputDir>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>jeo-to-bytecode</id>
<phase>generate-test-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mvn</executable>
<arguments combine.children="append">
<argument>org.eolang:jeo-maven-plugin:${jeo.version}:assemble</argument>
<argument>-Djeo.assemble.sourcesDir=${project.build.directory}/generated-sources/opeo-compile-xmir</argument>
<argument>-e</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>run-app</id>
<phase>verify</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>org.eolang.streams.Main</mainClass>
<arguments>28</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
42 changes: 42 additions & 0 deletions src/it/streams/src/main/java/org/eolang/streams/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2023-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.streams;

import java.util.Arrays;
import java.util.stream.IntStream;

public class Main {
public static void main(String... args) {
long start = System.currentTimeMillis();
// String[] strings = IntStream.range(0, 10)
// .mapToObj(i -> String.valueOf(i))
// .toArray(String[]::new);
// int sum = Arrays.stream(strings)
// .filter(s -> Boolean.valueOf(s.equals("")).equals(false))
// .mapToInt(s -> Integer.parseInt(s))
// .sum();
int sum = 10;
System.out.printf("sum=%d time=%d\n", sum, System.currentTimeMillis() - start);
}
}
39 changes: 39 additions & 0 deletions src/it/streams/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* MIT License
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// Check logs first.
String log = new File(basedir, 'build.log').text;
// Check success.
assert log.contains("BUILD SUCCESS")
assert log.contains("sum=")
assert log.contains("time=")
// Check all files.
// Decompilation output.
assert new File(basedir, 'target/generated-sources/opeo-decompile-xmir/org/eolang/streams/Main.xmir').exists()
// Compilation output.
assert new File(basedir, 'target/generated-sources/opeo-compile-xmir/org/eolang/streams/Main.xmir').exists()
// Phi expressions output.
//assert new File(basedir, 'target/generated-sources/phi-expressions/org/eolang/streams/Main.phi').exists()
// Check that we honestly decompiled and compiled the same file.
//assert new File(basedir, 'target/generated-sources/opeo-decompile-modified-xmir/org/eolang/streams/Main.xmir').exists()
true
15 changes: 10 additions & 5 deletions src/main/java/org/eolang/opeo/ast/ArrayConstructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eolang.jeo.representation.xmir.XmlNode;
import org.eolang.opeo.compilation.Parser;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.xembly.Directive;
import org.xembly.Directives;

Expand All @@ -42,7 +43,7 @@
*/
@ToString
@EqualsAndHashCode
public final class ArrayConstructor implements AstNode {
public final class ArrayConstructor implements AstNode, Typed {

/**
* Array size.
Expand All @@ -52,7 +53,7 @@ public final class ArrayConstructor implements AstNode {
/**
* Array type.
*/
private final String type;
private final String atype;

/**
* Constructor.
Expand All @@ -73,15 +74,15 @@ public ArrayConstructor(final XmlNode node, final Parser parser) {
*/
public ArrayConstructor(final AstNode size, final String type) {
this.size = size;
this.type = type;
this.atype = type;
}

@Override
public Iterable<Directive> toXmir() {
final Directives directives = new Directives();
directives.add("o")
.attr("base", ".array-node")
.append(new DirectivesData(this.type))
.append(new DirectivesData(this.atype))
.append(this.size.toXmir());
return directives.up();
}
Expand All @@ -90,8 +91,12 @@ public Iterable<Directive> toXmir() {
public List<AstNode> opcodes() {
final List<AstNode> res = new ArrayList<>(0);
res.addAll(this.size.opcodes());
res.add(new Opcode(Opcodes.ANEWARRAY, this.type));
res.add(new Opcode(Opcodes.ANEWARRAY, this.atype));
return res;
}

@Override
public Type type() {
return Type.getType(String.format("[L%s;", this.atype));
}
}
8 changes: 7 additions & 1 deletion src/main/java/org/eolang/opeo/ast/AstNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.Collections;
import java.util.List;
import org.objectweb.asm.Type;
import org.xembly.Directive;

/**
Expand All @@ -43,7 +44,7 @@ public interface AstNode extends Xmir {
* Empty node that does nothing.
* @since 0.2
*/
final class Empty implements AstNode {
final class Empty implements AstNode, Typed {

@Override
public List<AstNode> opcodes() {
Expand All @@ -54,5 +55,10 @@ public List<AstNode> opcodes() {
public Iterable<Directive> toXmir() {
return Collections.emptyList();
}

@Override
public Type type() {
return Type.VOID_TYPE;
}
}
}
Loading
Loading