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

Unroll PHI/UNPHI Transformations #720

Merged
merged 19 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f9fdcd2
feat(#705): add CanonicalXmir
volodya-lombrozo Sep 23, 2024
766aa0d
feat(#705): setup all the steps to transfrom canonical XMIR back to p…
volodya-lombrozo Sep 23, 2024
75d6567
feat(#705): add XSL transformations to make unroll
volodya-lombrozo Sep 23, 2024
71438bb
feat(#705): fix all the code offences
volodya-lombrozo Sep 23, 2024
5727ab4
Merge branch 'master' into 705_enable_phi_unphi
volodya-lombrozo Sep 24, 2024
b4d21f3
feat(#705): add Equals and HashCode annotations to all domain classes
volodya-lombrozo Sep 24, 2024
97c89e5
feat(#705): use Lists in BytecodeClass implementation
volodya-lombrozo Sep 24, 2024
a6f7c9b
feat(#705): add one more puzzle to enable CanonicalXmirTest
volodya-lombrozo Sep 24, 2024
945be17
feat(#705): add UnrollMojo
volodya-lombrozo Sep 24, 2024
fa02c15
feat(#705): update 'phi-unphi' integration test
volodya-lombrozo Sep 24, 2024
13d6aa4
feat(#705): 'phi-unphi' integration test: use unrolled xmir for assem…
volodya-lombrozo Sep 24, 2024
a0a31c9
feat(#705): don't remove files from the integration test
volodya-lombrozo Sep 24, 2024
a0434a5
feat(#705): remove redundant lines
volodya-lombrozo Sep 24, 2024
a117ad1
feat(#705): update roll-data.xsl transformation according with this c…
volodya-lombrozo Sep 24, 2024
8ca4f7b
feat(#705): add file name for CanonicalXmir
volodya-lombrozo Sep 24, 2024
7827d4c
feat(#705): fix all the code offences
volodya-lombrozo Sep 24, 2024
f40b5d5
feat(#705): fix xml offences in transformations
volodya-lombrozo Sep 24, 2024
85a1d26
feat(#705): add Shift type for TrJoined
volodya-lombrozo Sep 24, 2024
23abedb
feat(#705): remove the puzzle for #705 issue
volodya-lombrozo Sep 24, 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
14 changes: 13 additions & 1 deletion src/it/phi-unphi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ SOFTWARE.
<jeo.disassemble>${project.build.directory}/generated-sources/jeo-disassemble</jeo.disassemble>
<eo.phi>${project.build.directory}/generated-sources/eo-phi</eo.phi>
<eo.unphi>${project.build.directory}/generated-sources/eo-unphi</eo.unphi>
<jeo.unroll>${project.build.directory}/generated-sources/jeo-unroll</jeo.unroll>
</properties>
<build>
<plugins>
Expand All @@ -61,10 +62,21 @@ SOFTWARE.
</goals>
</execution>
<execution>
<id>xmir-to-bytecode</id>
<id>uroll-phi</id>
<phase>prepare-package</phase>
<configuration>
<sourcesDir>${eo.unphi}</sourcesDir>
<outputDir>${jeo.unroll}</outputDir>
</configuration>
<goals>
<goal>unroll-phi</goal>
</goals>
</execution>
<execution>
<id>xmir-to-bytecode</id>
<phase>package</phase>
<configuration>
<sourcesDir>${jeo.unroll}</sourcesDir>
</configuration>
<goals>
<goal>assemble</goal>
Expand Down
13 changes: 7 additions & 6 deletions src/it/phi-unphi/verify.groovy
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.nio.file.Files

/*
* MIT License
*
Expand Down Expand Up @@ -35,12 +37,10 @@ private String assertionMessage(String message) {

private void generateGitHubIssue() {
new File(basedir, 'build.log').text

copy('target/generated-sources/jeo-disassemble/org/eolang/hone/App.xmir', 'App.xmir.disassemble.txt')
copy('target/generated-sources/eo-phi/org/eolang/hone/App.phi', 'App.phi.txt')
copy('target/generated-sources/eo-unphi/org/eolang/hone/App.xmir', 'App.xmir.unphi.txt')


copy('target/generated-sources/jeo-unroll/org/eolang/hone/App.xmir', 'App.xmir.unroll.txt')
def issue = new File(basedir, "issue.md")
issue.text = '''I run the following [integration test](https://github.com/objectionary/jeo-maven-plugin/tree/master/src/it/phi-unphi):

Expand Down Expand Up @@ -91,9 +91,10 @@ gh issue create \\
}

private void copy(final String source, final String destination) {
def sourceFile = new File(basedir, source)
def destinationFile = new File(basedir, destination)
if (sourceFile.renameTo(destinationFile)) {
def sourceFile = new File(basedir, source).toPath()
def destinationFile = new File(basedir, destination).toPath()
Files.copy(sourceFile, destinationFile);
if (Files.exists(destinationFile)) {
println "'${source}' moved and renamed to '${destination}'!"
} else {
println "Failed to move the '${source}' file to '${destination}'."
Expand Down
134 changes: 134 additions & 0 deletions src/main/java/org/eolang/jeo/UnrollMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-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.jeo;

import com.jcabi.log.Logger;
import com.jcabi.xml.XMLDocument;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eolang.jeo.representation.CanonicalXmir;

/**
* This mojo unrolls all the changes made by PHI/UNPHI transformations.
* In other words, it makes XMIR understandable by jeo-maven-plugin after PHI/UNPHI transformations.
* @since 0.6
*/
@Mojo(name = "unroll-phi", defaultPhase = LifecyclePhase.PROCESS_CLASSES)
public final class UnrollMojo extends AbstractMojo {

/**
* Source directory.
*
* @since 0.6
* @checkstyle MemberNameCheck (6 lines)
*/
@Parameter(
property = "jeo.unroll-phi.sourcesDir",
defaultValue = "${project.build.directory}/generated-sources/jeo-xmir"
)
private File sourcesDir;

/**
* Target directory.
*
* @since 0.6
* @checkstyle MemberNameCheck (6 lines)
*/
@Parameter(
property = "jeo.unroll-phi.outputDir",
defaultValue = "${project.build.directory}/generated-sources/jeo-unrolled"
)
private File outputDir;

@Override
public void execute() throws MojoFailureException {
try (Stream<Path> xmirs = Files.walk(this.sourcesDir.toPath())) {
xmirs.filter(UnrollMojo::isXmir).forEach(this::unroll);
} catch (final IOException exception) {
throw new MojoFailureException(
String.format(
"Failed to unroll XMIR files from '%s' directory to '%s'",
this.sourcesDir,
this.outputDir
),
exception
);
}
}

/**
* Unrolls the XMIR file.
* @param path The path to the XMIR file.
*/
private void unroll(final Path path) {
try {
final Path output = this.outputDir.toPath()
.resolve(this.sourcesDir.toPath().relativize(path));
Logger.info(this, "Unrolling XMIR file '%s' to '%s'", path, output);
Files.createDirectories(output.getParent());
Files.write(
output,
new CanonicalXmir(new XMLDocument(path))
.plain()
.toString()
.getBytes(StandardCharsets.UTF_8)
);
} catch (final FileNotFoundException exception) {
throw new IllegalStateException(
String.format(
"Failed to unroll XMIR file '%s', most probably the file does not exist",
path
),
exception
);
} catch (final IOException exception) {
throw new IllegalStateException(
String.format(
"Failed to unroll XMIR file '%s', most probably an I/O error occurred",
path
),
exception
);
}
}

/**
* Checks if the file is an XMIR file.
* @param path The path to the file.
* @return True if the file is an XMIR file, false otherwise.
*/
private static boolean isXmir(final Path path) {
return Files.isRegularFile(path) && path.toString().endsWith(".xmir");
}
}
120 changes: 120 additions & 0 deletions src/main/java/org/eolang/jeo/representation/CanonicalXmir.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-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.jeo.representation;

import com.jcabi.xml.XML;
import com.yegor256.xsline.StClasspath;
import com.yegor256.xsline.StEndless;
import com.yegor256.xsline.TrClasspath;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.TrJoined;
import com.yegor256.xsline.Xsline;
import java.io.IOException;
import org.cactoos.io.InputOf;
import org.eolang.parser.EoSyntax;
import org.eolang.parser.xmir.Xmir;

/**
* Canonical XMIR.
* The purpose of this class is to restore the original XMIR format after PHI/UNPHI transformations.
* You can read more about it
* <a href="https://github.com/objectionary/eo/issues/3373#issuecomment-2361337359">here</a>.
* @since 0.6
*/
public final class CanonicalXmir {

/**
* Canonical XMIR after "phi/unphi" transformations.
*/
private final XML canonical;

/**
* Constructor.
* @param canonical Significantly modified XMIR after "phi/unphi".
*/
public CanonicalXmir(final XML canonical) {
this.canonical = canonical;
}

/**
* Convert canonical XMIR to plain XMIR.
* @return Plain XMIR.
*/
public XML plain() {
try {
return CanonicalXmir.unroll(CanonicalXmir.parse(this.toEo()));
} catch (final IOException exception) {
throw new IllegalStateException(
"Can't parse the canonical XMIR",
exception
);
}
}

/**
* Unroll all the changes made by the "phi/unphi" transformations.
* @param parsed Parsed XMIR.
* @return Unrolled XMIR.
*/
private static XML unroll(final XML parsed) {
return new Xsline(
new TrJoined<>(
new TrClasspath<>(
"/org/eolang/parser/wrap-method-calls.xsl"
).back(),
new TrDefault<>(
new StEndless(
new StClasspath(
"/org/eolang/parser/roll-bases.xsl"
)
)
),
new TrClasspath<>(
"/org/eolang/parser/add-refs.xsl",
"/org/eolang/parser/vars-float-down.xsl",
"/org/eolang/parser/roll-data.xsl"
).back()
)
).pass(parsed);
}

/**
* Convert XMIR to EO.
* @return EO.
*/
private String toEo() {
return new Xmir.Default(this.canonical).toEO();
}

/**
* Parse XMIR.
* @param eoprog Eo program.
* @return Parsed XMIR.
* @throws IOException If fails.
*/
private static XML parse(final String eoprog) throws IOException {
return new EoSyntax("name", new InputOf(eoprog)).parsed();
Copy link
Member

@maxonfjvipon maxonfjvipon Sep 24, 2024

Choose a reason for hiding this comment

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

@volodya-lombrozo I think this is the problem of objectionary/eo#3373 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

@maxonfjvipon Please, read this issue objectionary/eo#3380 it's a bug in eo

Copy link
Member

@maxonfjvipon maxonfjvipon Sep 24, 2024

Choose a reason for hiding this comment

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

@volodya-lombrozo I think the default constructor won't help you here because the name should taken from somewhere. Can't you set it manually from the file name?

Copy link
Member Author

Choose a reason for hiding this comment

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

@maxonfjvipon Please, extend this issue with your knowledge about file names objectionary/eo#3380

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private static BytecodeClass clazz(final ClassNode node) {
* @param node Asm field node.
* @return Domain field.
*/
private static Collection<BytecodeField> fields(final ClassNode node) {
private static List<BytecodeField> fields(final ClassNode node) {
return node.fields.stream().map(AsmProgram::field).collect(Collectors.toList());
}

Expand All @@ -191,7 +191,7 @@ private static BytecodeField field(final FieldNode node) {
* @param node Asm class node.
* @return Domain methods.
*/
private static Collection<BytecodeMethod> methods(final ClassNode node) {
private static List<BytecodeMethod> methods(final ClassNode node) {
return node.methods.stream().map(AsmProgram::method).collect(Collectors.toList());
}

Expand Down Expand Up @@ -301,7 +301,7 @@ private static BytecodeEntry tryblock(final TryCatchBlockNode node) {
* @param node Asm class node.
* @return Domain attributes.
*/
private static Collection<BytecodeAttribute> innerClasses(final ClassNode node) {
private static List<BytecodeAttribute> innerClasses(final ClassNode node) {
return node.innerClasses.stream()
.map(AsmProgram::innerClass)
.collect(Collectors.toList());
Expand Down
Loading
Loading