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(#376): Towards Agent Base Acrhitecture #379

Merged
merged 11 commits into from
Aug 13, 2024
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ SOFTWARE.
<limit>
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>7</maximum>
<maximum>8</maximum>
</limit>
</limits>
</rule>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/eolang/opeo/SelectiveDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.stream.Collectors;
import org.eolang.opeo.decompilation.Decompiler;
import org.eolang.opeo.decompilation.WithoutAliases;
import org.eolang.opeo.decompilation.handlers.RouterHandler;
import org.eolang.opeo.decompilation.agents.AllAgents;
import org.eolang.opeo.jeo.JeoDecompiler;
import org.eolang.opeo.storage.FileStorage;
import org.eolang.opeo.storage.Storage;
Expand All @@ -41,7 +41,7 @@
* Selective decompiler.
* Decompiler that decompiles ONLY fully understandable methods.
* These methods contain only instructions that are
* supported by {@link org.eolang.opeo.decompilation.handlers.RouterHandler}.
* supported by {@link AllAgents}.
*
* @since 0.1
*/
Expand Down Expand Up @@ -69,7 +69,7 @@ public final class SelectiveDecompiler implements Decompiler {
* @param modified Folder where to save the modified XMIRs.
*/
public SelectiveDecompiler(final Path input, final Path output, final Path modified) {
this(input, output, modified, new RouterHandler(false).supportedOpcodes());
this(input, output, modified, new AllAgents(false).supportedOpcodes());
}

/**
Expand Down Expand Up @@ -97,7 +97,7 @@ public SelectiveDecompiler(
public SelectiveDecompiler(
final Storage storage, final Storage modified
) {
this(storage, modified, new RouterHandler(false).supportedOpcodes());
this(storage, modified, new AllAgents(false).supportedOpcodes());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*/
@EqualsAndHashCode
@ToString
public final class Add implements AstNode, Typed {
public final class Addition implements AstNode, Typed {

/**
* Left operand.
Expand All @@ -58,7 +58,7 @@ public final class Add implements AstNode, Typed {
* @param node XML node
* @param parser Parser
*/
public Add(final XmlNode node, final Function<XmlNode, AstNode> parser) {
public Addition(final XmlNode node, final Function<XmlNode, AstNode> parser) {
this(
parser.apply(node.children().collect(Collectors.toList()).get(0)),
parser.apply(node.children().collect(Collectors.toList()).get(1))
Expand All @@ -70,7 +70,7 @@ public Add(final XmlNode node, final Function<XmlNode, AstNode> parser) {
* @param left Left operand
* @param right Right operand
*/
public Add(final AstNode left, final AstNode right) {
public Addition(final AstNode left, final AstNode right) {
this.left = left;
this.right = right;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/eolang/opeo/ast/Labeled.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.eolang.jeo.representation.xmir.XmlNode;
import org.eolang.opeo.decompilation.agents.InvokespecialAgent;
import org.objectweb.asm.Type;
import org.xembly.Directive;
import org.xembly.Directives;
Expand All @@ -39,7 +40,7 @@
* This class needed to avoid considering labels as separate nodes.
* Maybe it's wrong to do so, but it's easier to implement this way, at least for now.
* Pay attention, that {@link Labeled} class violates class hierarchy.
* It is the most visible within {@link org.eolang.opeo.decompilation.handlers.InvokespecialHandler}
* It is the most visible within {@link InvokespecialAgent}
* implementation.
* @since 0.2
*/
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/org/eolang/opeo/ast/Opcode.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eolang.jeo.representation.xmir.XmlInstruction;
import org.eolang.jeo.representation.xmir.XmlNode;
import org.eolang.jeo.representation.xmir.XmlOperand;
import org.eolang.opeo.Instruction;
import org.xembly.Directive;

/**
Expand Down Expand Up @@ -119,6 +120,10 @@ public Opcode(final XmlInstruction instruction) {
);
}

public Opcode(final Instruction instruction) {
this(instruction.opcode(), instruction.operands());
}

/**
* Constructor.
* @param bytecode Bytecode
Expand All @@ -141,6 +146,40 @@ public List<AstNode> opcodes() {
return Arrays.asList(this);
}

/**
* Opcode number.
* @return Opcode number.
*/
public int opcode() {
return this.bytecode;
}

/**
* Opcode operands.
* @return Opcode operands.
*/
public List<Object> params() {
return this.operands;
}

/**
* Instruction operand.
* @param index Operand index.
* @return Instruction operand.
*/
public Object operand(final int index) {
if (this.operands.size() <= index) {
throw new IllegalStateException(
String.format(
"Instruction '%s' doesn't have operand at index '%d'",
this,
index
)
);
}
return this.operands.get(index);
}

/**
* Disable opcodes counting.
* It is useful for tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.nio.file.Path;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.eolang.opeo.decompilation.handlers.RouterHandler;
import org.eolang.opeo.decompilation.agents.AllAgents;
import org.eolang.opeo.storage.CompilationStorage;
import org.eolang.opeo.storage.Storage;
import org.eolang.opeo.storage.XmirEntry;
Expand Down Expand Up @@ -65,7 +65,7 @@ public SelectiveCompiler(final Path xmirs, final Path output) {
*/
public SelectiveCompiler(final Storage storage) {
this.storage = storage;
this.supported = new RouterHandler(false).supportedOpcodes();
this.supported = new AllAgents(false).supportedOpcodes();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/eolang/opeo/compilation/XmirParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.List;
import java.util.stream.Collectors;
import org.eolang.jeo.representation.xmir.XmlNode;
import org.eolang.opeo.ast.Add;
import org.eolang.opeo.ast.Addition;
import org.eolang.opeo.ast.ArrayConstructor;
import org.eolang.opeo.ast.AstNode;
import org.eolang.opeo.ast.Attributes;
Expand Down Expand Up @@ -136,7 +136,7 @@ public AstNode parse(final XmlNode node) {
} else if ("duplicated".equals(base)) {
result = new Duplicate(this.parse(node.firstChild()));
} else if (".plus".equals(base)) {
result = new Add(node, this::parse);
result = new Addition(node, this::parse);
} else if (".minus".equals(base)) {
result = new Substraction(node, this::parse);
} else if ("cast".equals(base)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
package org.eolang.opeo.decompilation;

/**
* Instruction handler.
* An agent that tries to understand the current decompilation state and apply some changes to it.
* @since 0.1
*/
@FunctionalInterface
public interface InstructionHandler {
public interface DecompilationAgent {

/**
* Handle instruction.
* @param state Current instruction to handle together with operand stack and variables.
* Handle the current state.
* @param state Current state to handle together with operand stack and variables.
*/
void handle(DecompilerState state);
}
22 changes: 15 additions & 7 deletions src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.stream.Collectors;
import org.cactoos.list.ListOf;
import org.eolang.opeo.Instruction;
import org.eolang.opeo.ast.Opcode;
import org.eolang.opeo.ast.Root;
import org.eolang.opeo.decompilation.handlers.RouterHandler;
import org.eolang.opeo.decompilation.agents.AllAgents;
import org.xembly.Directive;

/**
Expand All @@ -47,7 +50,7 @@ public final class DecompilerMachine {
/**
* Handler that redirects instructions.
*/
private final RouterHandler router;
private final AllAgents agents;

/**
* Constructor.
Expand All @@ -73,7 +76,7 @@ public final class DecompilerMachine {
*/
public DecompilerMachine(final LocalVariables locals, final Map<String, String> arguments) {
this.locals = locals;
this.router = new RouterHandler(
this.agents = new AllAgents(
"true".equals(arguments.getOrDefault("counting", "true"))
);
}
Expand All @@ -85,10 +88,15 @@ public DecompilerMachine(final LocalVariables locals, final Map<String, String>
* @return Decompiled instructions.
*/
public Iterable<Directive> decompile(final Instruction... instructions) {
final DecompilerState state = new DecompilerState(this.locals);
Arrays.stream(instructions)
.forEach(inst -> this.router.handle(state.next(inst)));
return new Root(new ListOf<>(state.stack().descendingIterator())).toXmir();
final DecompilerState initial = new DecompilerState(
Arrays.stream(instructions)
.map(Opcode::new)
.collect(Collectors.toCollection(LinkedList::new)),
new OperandStack(),
this.locals
);
this.agents.handle(initial);
return new Root(new ListOf<>(initial.stack().descendingIterator())).toXmir();
}
}

Loading