Skip to content

Commit

Permalink
feat(#284): fix all qulice suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed May 29, 2024
1 parent 94bc527 commit baf33a4
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 61 deletions.
1 change: 0 additions & 1 deletion src/main/java/org/eolang/opeo/CompileMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eolang.opeo.compilation.Compiler;
import org.eolang.opeo.compilation.DefaultCompiler;
import org.eolang.opeo.compilation.DummyCompiler;
import org.eolang.opeo.compilation.SelectiveCompiler;

Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/eolang/opeo/SelectiveDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void decompile() {
entry -> {
final XmirEntry res;
final List<String> opcodes = entry.xpath(this.unsupportedOpcodes());
final List<String> trycatches = entry.xpath(this.trycatches());
final List<String> trycatches = entry.xpath(SelectiveDecompiler.trycatches());
if (opcodes.isEmpty() && trycatches.isEmpty()) {
res = entry.transform(
xml -> new JeoDecompiler(xml, entry.relative()).decompile()
Expand Down Expand Up @@ -173,9 +173,12 @@ private String unsupportedOpcodes() {
/**
* Xpath to find all try-catch blocks.
* @return Xpath.
* !todo!why?
* @todo #284:90min Decompile try-catch blocks.
* Currently we skip decompilation of methods that contain try-catch blocks.
* We need to implement decompilation of try-catch blocks.
* Don't forget to add tests for the new functionality.
*/
private String trycatches() {
private static String trycatches() {
return "//o[@base='tuple' and @name='trycatchblocks']/@name";
}
}
11 changes: 7 additions & 4 deletions src/main/java/org/eolang/opeo/ast/If.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import lombok.ToString;
import org.eolang.jeo.representation.HexData;
import org.eolang.jeo.representation.xmir.AllLabels;
import org.eolang.jeo.representation.xmir.HexString;
import org.eolang.jeo.representation.xmir.XmlNode;
import org.objectweb.asm.Opcodes;
import org.xembly.Directive;
Expand All @@ -43,6 +42,7 @@
*/
@ToString
@EqualsAndHashCode
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class If implements AstNode {

/**
Expand All @@ -63,8 +63,9 @@ public final class If implements AstNode {
/**
* Constructor.
* @param node XMIR node.
* @param search Search function.
*/
public If(final XmlNode node, Function<XmlNode, AstNode> search) {
public If(final XmlNode node, final Function<XmlNode, AstNode> search) {
this(If.xfirst(node, search), If.xsecond(node, search), If.xtarget(node));
}

Expand Down Expand Up @@ -121,18 +122,20 @@ public Iterable<Directive> toXmir() {
/**
* Extracts the first value.
* @param node XMIR node where to extract the value.
* @param search Search function to parse child nodes.
* @return Value.
*/
private static AstNode xfirst(final XmlNode node, Function<XmlNode, AstNode> search) {
private static AstNode xfirst(final XmlNode node, final Function<XmlNode, AstNode> search) {
return search.apply(node.child("base", ".gt").firstChild());
}

/**
* Extracts the second value.
* @param node XMIR node where to extract the value.
* @param search Search function to parse child nodes.
* @return Value.
*/
private static AstNode xsecond(final XmlNode node, Function<XmlNode, AstNode> search) {
private static AstNode xsecond(final XmlNode node, final Function<XmlNode, AstNode> search) {
final List<XmlNode> children = node.child("base", ".gt").children()
.collect(Collectors.toList());
return search.apply(children.get(children.size() - 1));
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/eolang/opeo/ast/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ public List<AstNode> opcodes() {
return Collections.singletonList(this);
}

public org.objectweb.asm.Label toAsmLabel() {
/**
* Convert to ASM label.
* @return ASM label.
*/
org.objectweb.asm.Label toAsmLabel() {
return new AllLabels().label(this.identifier);
}

public String identifier(){
return identifier;
}
}
54 changes: 35 additions & 19 deletions src/main/java/org/eolang/opeo/ast/Labeled.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,13 @@ public final class Labeled implements AstNode, Typed {
*/
private final Label label;

public Labeled(final XmlNode node, Function<XmlNode, AstNode> search) {
this(xnode(node, search), xlabel(node));
}

private static AstNode xnode(final XmlNode root, final Function<XmlNode, AstNode> search) {
if (root.children().count() > 1) {
return search.apply(root.firstChild());
} else {
return new Empty();
}
}

private static Label xlabel(final XmlNode root) {
final List<XmlNode> all = root.children().collect(Collectors.toList());
final XmlNode last = all.get(all.size() - 1);
return new Label(last);
/**
* Constructor.
* @param node Original node to parse.
* @param search Search function to parse child nodes.
*/
public Labeled(final XmlNode node, final Function<XmlNode, AstNode> search) {
this(Labeled.xnode(node, search), Labeled.xlabel(node));
}

/**
Expand All @@ -95,12 +86,10 @@ public AstNode origin() {

@Override
public List<AstNode> opcodes() {
final List<AstNode> re = Stream.concat(
return Stream.concat(
this.node.opcodes().stream(),
this.label.opcodes().stream()
).collect(Collectors.toList());

return re;
}

@Override
Expand All @@ -120,4 +109,31 @@ public Type type() {
throw new IllegalStateException(String.format("Node '%s' is not typed", this.node));
}
}

/**
* Parse original node from XMIR.
* @param root Root node where the original node is placed.
* @param search Search function to parse child nodes.
* @return Original node.
*/
private static AstNode xnode(final XmlNode root, final Function<XmlNode, AstNode> search) {
final AstNode result;
if (root.children().count() > 1) {
result = search.apply(root.firstChild());
} else {
result = new Empty();
}
return result;
}

/**
* Parse label from XMIR.
* @param root Root node where the label is placed.
* @return Label.
*/
private static Label xlabel(final XmlNode root) {
final List<XmlNode> all = root.children().collect(Collectors.toList());
final XmlNode last = all.get(all.size() - 1);
return new Label(last);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/eolang/opeo/ast/Mul.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class Mul implements AstNode {
* @param node XMIR node where to extract the value.
* @param search Search function.
*/
public Mul(final XmlNode node, Function<XmlNode, AstNode> search) {
public Mul(final XmlNode node, final Function<XmlNode, AstNode> search) {
this(Mul.xleft(node, search), xright(node, search));
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/eolang/opeo/compilation/JeoCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
import com.jcabi.xml.XML;
import org.eolang.jeo.representation.xmir.AllLabels;
import org.eolang.jeo.representation.xmir.XmlClass;
import org.eolang.jeo.representation.xmir.XmlInstruction;
import org.eolang.jeo.representation.xmir.XmlMethod;
import org.eolang.jeo.representation.xmir.XmlNode;
import org.eolang.jeo.representation.xmir.XmlProgram;
import org.objectweb.asm.Opcodes;

/**
* Compiler of high-level EO programs to low-level EO suitable for jeo-maven-plugin.
Expand Down Expand Up @@ -76,6 +74,7 @@ public XML compile() {
* Compiles a single method.
*
* @param method The method to compile.
* @param pckg The package of the method.
* @return The compiled method.
* @todo #229:90min Refactor {@link #compile} method to handle exceptions appropriately.
* The method {@link #compile} is catching generic exceptions which is bad.
Expand All @@ -99,16 +98,18 @@ public XML compile() {
@SuppressWarnings({"PMD.AvoidCatchingGenericException", "PMD.IdenticalCatchBranches"})
private static XmlMethod compile(final XmlMethod method, final String pckg) {
try {
final XmlMethod result;
new AllLabels().clearCache();
if (pckg.contains("org.eolang")) {
return method.withoutMaxs().withInstructions(
result = method.withoutMaxs().withInstructions(
new XmirParser(method.nodes()).toJeoNodes().toArray(XmlNode[]::new)
);
} else {
return method.withInstructions(
result = method.withInstructions(
new XmirParser(method.nodes()).toJeoNodes().toArray(XmlNode[]::new)
);
}
return result;
} catch (final ClassCastException exception) {
throw new IllegalArgumentException(
String.format(
Expand Down
55 changes: 53 additions & 2 deletions src/main/java/org/eolang/opeo/compilation/SelectiveCompiler.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/*
* 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.
*/
package org.eolang.opeo.compilation;

import com.jcabi.log.Logger;
Expand All @@ -9,15 +32,37 @@
import org.eolang.opeo.storage.Storage;
import org.eolang.opeo.storage.XmirEntry;

/**
* Selective compiler.
* Compiles only those sources that were previously decompiled.
* All the rest are skipped and copied as is without any changes.
* @since 0.2
*/
public final class SelectiveCompiler implements Compiler {

/**
* Storage.
*/
private final Storage storage;

/**
* Supported opcodes.
*/
private final String[] supported;

/**
* Constructor.
* @param xmirs XMIRs to compile directory.
* @param output Output directory
*/
public SelectiveCompiler(final Path xmirs, final Path output) {
this(new CompilationStorage(xmirs, output));
}

/**
* Constructor.
* @param storage Storage.
*/
public SelectiveCompiler(final Storage storage) {
this.storage = storage;
this.supported = new RouterHandler(false).supportedOpcodes();
Expand All @@ -35,9 +80,15 @@ public void compile() {
);
}

/**
* Compile the entry.
* @param entry Entry to compile.
* @return One if compiled, zero otherwise.
*/
private int compile(final XmirEntry entry) {
final XmirEntry res;
if (entry.xpath(this.unsupportedOpcodes()).isEmpty() || entry.xpath(this.trycatches())
if (entry.xpath(this.unsupportedOpcodes()).isEmpty()
|| entry.xpath(SelectiveCompiler.trycatches())
.isEmpty()) {
res = entry.transform(xml -> new JeoCompiler(xml).compile());
} else {
Expand Down Expand Up @@ -67,7 +118,7 @@ private String unsupportedOpcodes() {
* Xpath to find all try-catch blocks.
* @return Xpath.
*/
private String trycatches() {
private static String trycatches() {
return "//o[@base='tuple' and @name='trycatchblocks']/@name";
}
}
11 changes: 1 addition & 10 deletions src/main/java/org/eolang/opeo/jeo/JeoDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,7 @@ private void decompile(final XmlMethod method, final String clazz) {
).children().collect(Collectors.toList()).toArray(XmlNode[]::new)
);
}
} catch (final ClassCastException exception) {
throw new IllegalStateException(
String.format(
"Failed to decompile method '%s' from the following XMIR: '%s'",
method,
this.prog
),
exception
);
} catch (final IllegalStateException exception) {
} catch (final ClassCastException | IllegalStateException exception) {
throw new IllegalStateException(
String.format(
"Failed to decompile method '%s' from the following XMIR: '%s'",
Expand Down
23 changes: 13 additions & 10 deletions src/test/java/org/eolang/opeo/ast/IfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* Test case for {@link If}.
* @since 0.2
*/
class IfTest {
final class IfTest {

/**
* Xmir for the 'if' statement.
Expand Down Expand Up @@ -74,15 +74,18 @@ void convertsIfStatementToXmir() throws ImpossibleModificationException {
void createsIfStatementFromXmir() {
MatcherAssert.assertThat(
"Can create 'if' statement from XMIR",
new If(new XmlNode(IfTest.XMIR), xml -> {
final AstNode result;
if (xml.text().contains("1")) {
result = new Literal(1);
} else {
result = new Literal(2);
new If(
new XmlNode(IfTest.XMIR),
xml -> {
final AstNode result;
if (xml.text().contains("1")) {
result = new Literal(1);
} else {
result = new Literal(2);
}
return result;
}
return result;
}),
),
Matchers.equalTo(
new If(
new Literal(1),
Expand Down Expand Up @@ -110,4 +113,4 @@ void convertsToOpcodes() {
)
);
}
}
}
Loading

5 comments on commit baf33a4

@0pdd
Copy link

@0pdd 0pdd commented on baf33a4 May 29, 2024

Choose a reason for hiding this comment

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

Puzzle 229-b768fbb7 disappeared from src/main/java/org/eolang/opeo/compilation/JeoCompiler.java), that's why I closed #282. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link

@0pdd 0pdd commented on baf33a4 May 29, 2024

Choose a reason for hiding this comment

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

Puzzle 229-ddf067cd discovered in src/main/java/org/eolang/opeo/compilation/JeoCompiler.java) and submitted as #286. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on baf33a4 May 29, 2024

Choose a reason for hiding this comment

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

Puzzle 284-f828f7b9 discovered in src/main/java/org/eolang/opeo/SelectiveDecompiler.java) and submitted as #287. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on baf33a4 May 29, 2024

Choose a reason for hiding this comment

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

Puzzle 284-538bc9b3 discovered in src/main/java/org/eolang/opeo/ast/Mul.java) and submitted as #288. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on baf33a4 May 29, 2024

Choose a reason for hiding this comment

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

Puzzle 284-dfed10f3 discovered in src/test/java/it/JeoAndOpeoTest.java) and submitted as #289. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.