Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Dec 20, 2023
2 parents 5b825c6 + 57c0e68 commit 54baefb
Show file tree
Hide file tree
Showing 17 changed files with 538 additions and 50 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ SOFTWARE.
<!-- version from the parent pom -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<!-- version from the parent pom -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
Expand All @@ -158,8 +164,8 @@ SOFTWARE.
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-matchers</artifactId>
<!-- version from the parent pom -->
<scope>test</scope>
</dependency>
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/org/eolang/opeo/Instruction.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.eolang.opeo;

import java.util.Collections;
import java.util.List;
import lombok.ToString;

Expand All @@ -43,7 +44,7 @@ public final class Instruction {
/**
* Operands.
*/
private final List<Object> operands;
private final List<Object> arguments;

/**
* Constructor.
Expand All @@ -61,7 +62,7 @@ public Instruction(final int code, final Object... args) {
*/
private Instruction(final int code, final List<Object> args) {
this.opcode = code;
this.operands = args;
this.arguments = args;
}

/**
Expand All @@ -78,6 +79,14 @@ public int code() {
* @return Operand
*/
public Object operand(final int index) {
return this.operands.get(index);
return this.arguments.get(index);
}

/**
* Operands.
* @return Operands.
*/
public List<Object> operands() {
return Collections.unmodifiableList(this.arguments);
}
}
14 changes: 8 additions & 6 deletions src/main/java/org/eolang/opeo/ast/AstNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package org.eolang.opeo.ast;

import org.xembly.Directive;

/**
* Abstract syntax tree node.
* @since 0.1
Expand All @@ -32,15 +34,15 @@ public interface AstNode {
/**
* Print ast node and all it's children.
* @return String output.
* @todo #8:90min Print output in XMIR format.
* Currently we just print decompilation output as a string.
* We need to print the output into XMIR format directly.
* By using EO XML we will be able to integrate
* this code with other plugins and, which is also important,
* we won't need to format the output manually.
*/
String print();

/**
* Convert node to XMIR.
* @return XMIR XML.
*/
Iterable<Directive> toXmir();

/**
* Node id.
* @return Node id.
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/eolang/opeo/ast/Constructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
*/
package org.eolang.opeo.ast;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.eolang.opeo.vmachine.ObjectReference;
import org.xembly.Directive;
import org.xembly.Directives;

/**
* Constructor output node.
Expand All @@ -47,6 +51,18 @@ public final class Constructor implements AstNode {
*/
private final List<AstNode> arguments;

/**
* Constructor.
* @param type Constructor type
* @param arguments Constructor arguments
*/
public Constructor(
final String type,
final AstNode... arguments
) {
this(type, new ObjectReference(type).toString(), Arrays.asList(arguments));
}

/**
* Constructor.
* @param type Constructor type
Expand All @@ -72,6 +88,16 @@ public String print() {
);
}

@Override
public Iterable<Directive> toXmir() {
final Directives directives = new Directives();
directives.add("o")
.attr("base", ".new")
.add("o").attr("base", this.type).up();
this.arguments.stream().map(AstNode::toXmir).forEach(directives::append);
return directives.up();
}

@Override
public String identifier() {
return this.reference;
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/eolang/opeo/ast/Invocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
*/
package org.eolang.opeo.ast;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import org.xembly.Directive;
import org.xembly.Directives;

/**
* Invocation output node.
Expand All @@ -48,6 +51,20 @@ public final class Invocation implements AstNode {
*/
private final List<AstNode> arguments;

/**
* Constructor.
* @param source Source or target on which the invocation is performed
* @param method Method name
* @param args Arguments
*/
public Invocation(
final AstNode source,
final String method,
final AstNode... args
) {
this(source, method, Arrays.asList(args));
}

/**
* Constructor.
* @param source Source or target on which the invocation is performed
Expand All @@ -74,6 +91,16 @@ public String print() {
);
}

@Override
public Iterable<Directive> toXmir() {
final Directives directives = new Directives();
directives.add("o")
.attr("base", String.format(".%s", this.method))
.append(this.source.toXmir());
this.arguments.stream().map(AstNode::toXmir).forEach(directives::append);
return directives.up();
}

@Override
public String identifier() {
return UUID.randomUUID().toString();
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/eolang/opeo/ast/Literal.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
package org.eolang.opeo.ast;

import java.util.UUID;
import org.eolang.jeo.representation.directives.DirectivesData;
import org.xembly.Directive;

/**
* Literal output.
Expand All @@ -44,6 +46,11 @@ public Literal(final Object value) {
this.object = value;
}

@Override
public Iterable<Directive> toXmir() {
return new DirectivesData(this.object);
}

@Override
public String print() {
final String result;
Expand Down
84 changes: 84 additions & 0 deletions src/main/java/org/eolang/opeo/ast/Opcode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.ast;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import org.eolang.jeo.representation.directives.DirectivesInstruction;
import org.eolang.parser.XMIR;
import org.xembly.Directive;
import org.xembly.Transformers;
import org.xembly.Xembler;

/**
* Opcode output node.
* @since 0.1
*/
public final class Opcode implements AstNode {

/**
* Opcode.
*/
private final int bytecode;

/**
* Opcode operands.
*/
private final List<Object> operands;

/**
* Constructor.
* @param opcode Opcode
* @param operands Opcode operands
*/
public Opcode(final int opcode, final Object... operands) {
this(opcode, Arrays.asList(operands));
}

/**
* Constructor.
* @param opcode Opcode
* @param operands Opcode operands
*/
public Opcode(final int opcode, final List<Object> operands) {
this.bytecode = opcode;
this.operands = operands;
}

@Override
public String print() {
return new XMIR(new Xembler(this.toXmir(), new Transformers.Node()).xmlQuietly()).toEO();
}

@Override
public Iterable<Directive> toXmir() {
return new DirectivesInstruction(this.bytecode, this.operands.toArray());
}

@Override
public String identifier() {
return UUID.randomUUID().toString();
}
}
18 changes: 18 additions & 0 deletions src/main/java/org/eolang/opeo/ast/Root.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
*/
package org.eolang.opeo.ast;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import org.xembly.Directive;
import org.xembly.Directives;

/**
* Root node.
Expand All @@ -52,6 +55,21 @@ public String print() {
return this.children.stream().map(AstNode::print).collect(Collectors.joining("\n"));
}

@Override
public Iterable<Directive> toXmir() {
final Iterable<Directive> result;
if (this.children.isEmpty()) {
result = Collections.emptyList();
} else {
final Directives directives = new Directives();
directives.add("o").attr("base", "tuple");
this.children.stream().map(AstNode::toXmir).forEach(directives::append);
directives.up();
result = directives;
}
return result;
}

/**
* Find child by ID.
* @param identifier ID
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/eolang/opeo/vmachine/DecompilerMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import org.eolang.opeo.ast.AstNode;
import org.eolang.opeo.ast.Constructor;
import org.eolang.opeo.ast.Invocation;
import org.eolang.opeo.ast.Keyword;
import org.eolang.opeo.ast.Literal;
import org.eolang.opeo.ast.Opcode;
import org.eolang.opeo.ast.Root;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
Expand Down Expand Up @@ -188,7 +188,9 @@ public void handle(final Instruction instruction) {
private class ReturnHandler implements InstructionHandler {
@Override
public void handle(final Instruction instruction) {
DecompilerMachine.this.root.append(new Keyword("return"));
DecompilerMachine.this.root.append(
new Opcode(instruction.code(), instruction.operands())
);
}
}

Expand Down Expand Up @@ -254,8 +256,9 @@ public void handle(final Instruction instruction) {
private class UnimplementedHandler implements InstructionHandler {
@Override
public void handle(final Instruction instruction) {
DecompilerMachine.this.root
.append(new Keyword(String.format("Unimplemented %s", instruction)));
DecompilerMachine.this.root.append(
new Opcode(instruction.code(), instruction.operands())
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public final class ObjectReference {
* Constructor.
* @param type Object type
*/
ObjectReference(final String type) {
public ObjectReference(final String type) {
this(type, ObjectReference.GLOBAL);
}

Expand Down
Loading

1 comment on commit 54baefb

@0pdd
Copy link

@0pdd 0pdd commented on 54baefb Dec 20, 2023

Choose a reason for hiding this comment

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

Puzzle 8-06fcf714 disappeared from src/main/java/org/eolang/opeo/ast/AstNode.java), that's why I closed #12. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.