Skip to content

Commit

Permalink
feat(objectionary#381): extract logs from TracedAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Aug 20, 2024
1 parent 79f97a0 commit 2fd1579
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eolang.opeo.ast.Opcode;
import org.eolang.opeo.ast.Root;
import org.eolang.opeo.decompilation.agents.AllAgents;
import org.eolang.opeo.decompilation.agents.TracedAgent;
import org.xembly.Directive;

/**
Expand Down Expand Up @@ -64,7 +65,7 @@ public DecompilerMachine() {
*
* @param args Arguments provided to decompiler.
*/
public DecompilerMachine(final Map<String, String> args) {
public DecompilerMachine(final Map<String, Object> args) {
this(new LocalVariables(), args);
}

Expand All @@ -74,10 +75,11 @@ public DecompilerMachine(final Map<String, String> args) {
* @param locals Local variables.
* @param arguments Arguments provided to decompiler.
*/
public DecompilerMachine(final LocalVariables locals, final Map<String, String> arguments) {
public DecompilerMachine(final LocalVariables locals, final Map<String, Object> arguments) {
this.locals = locals;
this.agents = new AllAgents(
"true".equals(arguments.getOrDefault("counting", "true"))
"true".equals(arguments.getOrDefault("counting", "true")),
TracedAgent.Output.class.cast(arguments.getOrDefault("output", new TracedAgent.Log()))
);
}

Expand Down
58 changes: 29 additions & 29 deletions src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,44 +50,44 @@ public final class AllAgents implements DecompilationAgent {
* Constructor.
*/
public AllAgents() {
this(false);
this(false, new TracedAgent.Log());
}

/**
* Constructor.
* @param counting Do we put numbers to opcodes?
*/
public AllAgents(final boolean counting) {
public AllAgents(final boolean counting, final TracedAgent.Output output) {
this(
new HashSet<>(
Arrays.asList(
new TracedAgent(new ConstAgent()),
new TracedAgent(new AddAgent()),
new TracedAgent(new SubAgent()),
new TracedAgent(new MulAgent()),
new TracedAgent(new IfAgent()),
new TracedAgent(new CastAgent()),
new TracedAgent(new LoadAgent()),
new TracedAgent(new StoreAgent()),
new TracedAgent(new StoreToArrayAgent()),
new TracedAgent(new NewArrayAgent()),
new TracedAgent(new CheckCastAgent()),
new TracedAgent(new NewAgent()),
new TracedAgent(new DupAgent()),
new TracedAgent(new BipushAgent()),
new TracedAgent(new InvokespecialAgent()),
new TracedAgent(new InvokevirtualAgent()),
new TracedAgent(new InvokestaticAgent()),
new TracedAgent(new InvokeinterfaceAgent()),
new TracedAgent(new InvokedynamicAgent()),
new TracedAgent(new GetFieldAgent()),
new TracedAgent(new PutFieldAgent()),
new TracedAgent(new GetStaticAgent()),
new TracedAgent(new LdcAgent()),
new TracedAgent(new PopAgent()),
new TracedAgent(new ReturnAgent()),
new TracedAgent(new LabelAgent()),
new TracedAgent(new UnimplementedAgent(counting))
new TracedAgent(new ConstAgent(), output),
new TracedAgent(new AddAgent(), output),
new TracedAgent(new SubAgent(), output),
new TracedAgent(new MulAgent(), output),
new TracedAgent(new IfAgent(), output),
new TracedAgent(new CastAgent(), output),
new TracedAgent(new LoadAgent(), output),
new TracedAgent(new StoreAgent(), output),
new TracedAgent(new StoreToArrayAgent(), output),
new TracedAgent(new NewArrayAgent(), output),
new TracedAgent(new CheckCastAgent(), output),
new TracedAgent(new NewAgent(), output),
new TracedAgent(new DupAgent(), output),
new TracedAgent(new BipushAgent(), output),
new TracedAgent(new InvokespecialAgent(), output),
new TracedAgent(new InvokevirtualAgent(), output),
new TracedAgent(new InvokestaticAgent(), output),
new TracedAgent(new InvokeinterfaceAgent(), output),
new TracedAgent(new InvokedynamicAgent(), output),
new TracedAgent(new GetFieldAgent(), output),
new TracedAgent(new PutFieldAgent(), output),
new TracedAgent(new GetStaticAgent(), output),
new TracedAgent(new LdcAgent(), output),
new TracedAgent(new PopAgent(), output),
new TracedAgent(new ReturnAgent(), output),
new TracedAgent(new LabelAgent(), output),
new TracedAgent(new UnimplementedAgent(counting), output)
)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void handle(final DecompilerState state) {
* Target for the output of the traced agent.
* @since 0.4
*/
private interface Output {
public interface Output {

/**
* Write a message.
Expand Down Expand Up @@ -154,7 +154,7 @@ public static final class Container implements Output {
/**
* Default constructor.
*/
Container() {
public Container() {
this(new LinkedList<>());
}

Expand All @@ -175,7 +175,7 @@ public void write(final String message) {
* Get all messages.
* @return All messages.
*/
Collection<String> messages() {
public Collection<String> messages() {
return Collections.unmodifiableCollection(this.queue);
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/test/java/it/AgentsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.jcabi.xml.XMLDocument;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -36,6 +37,7 @@
import org.eolang.opeo.OpcodeInstruction;
import org.eolang.opeo.ast.OpcodeName;
import org.eolang.opeo.decompilation.DecompilerMachine;
import org.eolang.opeo.decompilation.agents.TracedAgent;
import org.eolang.parser.xmir.Xmir;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -63,18 +65,24 @@ final class AgentsIT {
@ClasspathSource(value = "agents", glob = "**.yaml")
void verifiesAgents(final String yaml) {
final InstructionPack pack = new InstructionPack(yaml);
final TracedAgent.Container output = new TracedAgent.Container();
final Iterable<Directive> xmir =
new Directives().add("program")
.add("objects")

.append(
new DecompilerMachine().decompile(pack.instructions())
new DecompilerMachine(Collections.singletonMap("output", output))
.decompile(pack.instructions())

).up().up();
final String xml = new Xembler(xmir).xmlQuietly();
final String actual = new Xmir.Default(new XMLDocument(xml)).toEO();
final String expected = pack.expected();
System.out.println("EXPECTED");
final List<String> expectedAgents = pack.agents();
System.out.println(expectedAgents);
System.out.println("ACTUAL");
System.out.println(output.messages());
System.out.println("______");
MatcherAssert.assertThat(
"Agents should decompile instructions correctly, according to the YAML pack",
actual,
Expand Down

0 comments on commit 2fd1579

Please sign in to comment.