From efbc1594dffe17819af5d9c822fcf3778fe6b631 Mon Sep 17 00:00:00 2001 From: dorianreineccius Date: Fri, 22 May 2020 19:54:02 +0200 Subject: [PATCH 1/3] rho(rho(i)) works fine --- src/model/Compiler.java | 10 +-- src/model/VM.java | 2 +- src/rules/three.rules | 4 +- test/model/Compiler0AddressTest.java | 43 ++++++++++++ test/model/Compiler2AddressTest.java | 45 +++++++++++++ test/model/CompilerTest.java | 99 ++++++++++++++++++++++++++++ 6 files changed, 195 insertions(+), 8 deletions(-) create mode 100644 test/model/Compiler0AddressTest.java create mode 100644 test/model/Compiler2AddressTest.java create mode 100644 test/model/CompilerTest.java diff --git a/src/model/Compiler.java b/src/model/Compiler.java index 5022fa8..ee8cdf9 100644 --- a/src/model/Compiler.java +++ b/src/model/Compiler.java @@ -104,7 +104,7 @@ public Program compile(String _source) throws CompileException { inst.setInstructionIndex(t.getInstructionIndex()); System.out.println("OP | FLG1 | DEST | FLG2 | SRC1 | FLG2 | SRC2 |"); System.out.println( - inst.op + " | " + + inst.op + " | " + inst.flags[0]+ " | " + inst.addrs[0] + " | " + inst.flags[1]+ " | " + inst.addrs[1] + " | " + inst.flags[2]+ " | " + inst.addrs[2] ); @@ -120,7 +120,7 @@ public Program compile(String _source) throws CompileException { } } if (!mainReturn) { - throw new CompileException(prog.getEntryPoint(), "Main funktion has to end with return"); + throw new CompileException(prog.getEntryPoint(), "Main function has to end with return"); } return prog; @@ -199,7 +199,7 @@ private void checkAddresses(Token t) { return; } /*if(t.addrs[0] == null) { - break; + break; }*/ else if (t.addrs[0].charAt(0) == 'α') { if (registerLabelMap.get(t.addrs[0]) == null) { registerLabelMap.put(t.addrs[0], registerLabelIndex); //fügt das Pseudonym ggf hinzu @@ -294,7 +294,7 @@ private byte encodeOpCode(Token token) throws CompileException { opCode = Instruction.OP_RETURN; break; default: - throw new CompileException(token.getLineNumber(), "Crititcal Error: Unkown regular Expression"); + throw new CompileException(token.getLineNumber(), "Critical Error: Unknown regular Expression"); } return opCode; } @@ -364,7 +364,7 @@ private String[] addressLookup(Token token, int[] flags) { } System.out.println(convertMemoryAddress(token.addrs[i])); System.out.println(memoryLabelMap.get(convertMemoryAddress(token.addrs[i]))); - + addresses[i] = convertMemoryAddress(token.addrs[i]); break; case Instruction.FLAG_CONSTANT: diff --git a/src/model/VM.java b/src/model/VM.java index 719c735..1558be4 100644 --- a/src/model/VM.java +++ b/src/model/VM.java @@ -248,7 +248,7 @@ private int workingInstraktion(Instruction i) { this.memory.put(String.valueOf(register[Integer.parseInt(i.addrs[0])]), value); break; case Instruction.FLAG_IN_MEM_MEM: - this.memory.put(String.valueOf(memory.get(String.valueOf(register[Integer.parseInt(i.addrs[0])]))), value); + this.memory.put(String.valueOf(memory.get(i.addrs[0])), value); break; default: break; diff --git a/src/rules/three.rules b/src/rules/three.rules index 89d1700..3c14386 100644 --- a/src/rules/three.rules +++ b/src/rules/three.rules @@ -23,11 +23,11 @@ #1. load #(?α\d*|ρ[(](\w|(α\d*)|\d+|ρ[(](\w|\d+)[)])[)]):=(?(-\d+|\d+)|α\d*|ρ[(](\w|(α\d*)|\d+|ρ[(](\w|\d+)[)])[)]) -(?α\d*|ρ[(]([^\)]+)[)]):=(?(-\d+|\d+)|α\d*|ρ[(](ρ[(]([^\)]+)[)]|[^\)]+)[)]) +(?α\d*|ρ[(]([0-9a-zA-Z]+|ρ[(][0-9a-zA-Z]+[)])[)]):=(?(-\d+|\d+)|α\d*|ρ[(](ρ[(]([0-9a-zA-Z]+)[)]|[0-9a-zA-Z]+)[)]) #2. arithmetic operation #(?α\d*|ρ[(](\w|(α\d*)|\d+|ρ[(](\w|\d+)[)])[)]):=(?(-\d+|\d+)|α\d*|ρ[(](\w|(α\d*)|\d+|ρ[(](\w|\d+)[)])[)])(?[+*-÷%])(?(-\d+|\d+)|α\d*|ρ[(](\w|(α\d*)|\d+|ρ[(](\w|\d+)[)])[)]) -(?α\d*|ρ[(]([^\)]+)[)]):=(?(-\d+|\d+)|α\d*|ρ[(](ρ[(]([^\)]+)[)]|[^\)]+)[)])(?[+*-÷%])(?(-\d+|\d+)|α\d*|ρ[(](ρ[(]([^\)]+)[)]|[^\)]+)[)]) +(?α\d*|ρ[(]([0-9a-zA-Z]+)[)]):=(?(-\d+|\d+)|α\d*|ρ[(](ρ[(]([0-9a-zA-Z]+)[)]|[0-9a-zA-Z]+)[)])(?[+*-÷%])(?(-\d+|\d+)|α\d*|ρ[(](ρ[(]([0-9a-zA-Z]+)[)]|[0-9a-zA-Z]+)[)]) #3.conditional jump if(?α\d*)(?<|>|=)(?α\d*|\d+)thengoto(?\w+) diff --git a/test/model/Compiler0AddressTest.java b/test/model/Compiler0AddressTest.java new file mode 100644 index 0000000..cdd45ca --- /dev/null +++ b/test/model/Compiler0AddressTest.java @@ -0,0 +1,43 @@ +package model; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests for 0-Address expressions + * + * @author dorianreineccius + */ +public class Compiler0AddressTest { + Compiler c = new Compiler(); + + @Before + public void init() { + try { + c.load(getClass().getResourceAsStream("/rules/three.rules")); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * A little load-write test for 0-address expressions + */ + @Test + public void loadWriteTest() { + String source = "ρ(a) := 42\n" + + "α := ρ(a)\n" + + "ρ(b) := 42\n" + + "return"; + Program p = c.compile(source); + VM vm = new VM(p); + vm.loadProgam(); + for (int i = 0; i < 3; i++) { + vm.step(); + } + Assert.assertEquals(42, (int) vm.getMemory().get("a")); + Assert.assertEquals(42, (int) vm.getRegister()[0]); + Assert.assertEquals(42, (int) vm.getMemory().get("b")); + } +} diff --git a/test/model/Compiler2AddressTest.java b/test/model/Compiler2AddressTest.java new file mode 100644 index 0000000..3d17a6c --- /dev/null +++ b/test/model/Compiler2AddressTest.java @@ -0,0 +1,45 @@ +package model; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests for 2-Address expressions + * + * @author dorianreineccius + */ +public class Compiler2AddressTest { + Compiler c = new Compiler(); + + @Before + public void init() { + try { + c.load(getClass().getResourceAsStream("/rules/three.rules")); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * This tests ρ(ρ(i)) being written and read. + */ + @Test + public void compileIndirectExpressions() { + String source = "ρ(i) := 1\n" + + "ρ(j) := 2\n" + + "ρ(ρ(i)) := ρ(j)\n" + + "α := ρ(ρ(i))\n" + + "return"; + Program p = c.compile(source); + VM vm = new VM(p); + vm.loadProgam(); + vm.step(); + vm.step(); + vm.step(); + Assert.assertEquals(2, (int) vm.getMemory().get("1")); + vm.step(); + Assert.assertEquals(2, (int) vm.getRegister()[0]); + + } +} diff --git a/test/model/CompilerTest.java b/test/model/CompilerTest.java new file mode 100644 index 0000000..8f9ff36 --- /dev/null +++ b/test/model/CompilerTest.java @@ -0,0 +1,99 @@ +package model; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author dorianreineccius + */ +public class CompilerTest { + Compiler c = new Compiler(); + + @Before + public void init() { + try { + c.load(getClass().getResourceAsStream("/rules/three.rules")); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * This tests, whether alpha-expressions get parsed. + */ + @Test + public void compileAlphaExpressions() { + String source = "" + + "ρ(i):=0\nρ(j):=0#init them for testing purposes\n" + + "α:=0\n" + + "α:=α+0\n" + + "α:=ρ(i)\n" + + "α:=α+ρ(i)\n" + + "α:=ρ(i)+ρ(j)\n" + + "return"; + Program p = c.compile(source); + } + + /** + * This tests, whether rho-expressions get parsed. + */ + @Test + public void compileRhoExpressions() { + String source = "" + + "α:=0#init α for testing purposes\n" + + "ρ(i):=α\n" + + "ρ(j):=α+0\n" + + "ρ(i):=ρ(j)\n" + + "ρ(i):=ρ(j)+0\n" + + "ρ(i):=ρ(j)+α\n" + + "ρ(i):=ρ(ρ(j))\n" + + "ρ(ρ(i)):=ρ(j)\n" + + "return"; + Program p = c.compile(source); + } + + /** + * This tests, whether jumping statements get parsed. + */ + @Test + public void compileOtherExpressions() { + String source = "" + + "label: α:=0#init them for testing purposes\n" + + "if α = 0 then goto label\n" + + "goto label\n" + + "call label\n" + + "return"; + Program p = c.compile(source); + } + + /** + * This tests, whether the registers α,α0,α1,α2 works, if used all of them. + */ + @Test + public void registerConfusingTest() { + String source = "ρ(i) := 42\n" + + "ρ(j) := 42\n" + + "ρ(k) := 42\n" + + "ρ(l) := 42\n" + + "α2 := 2\n" + + "α1 := 1\n" + + "α0 := 0\n" + + "α := -1\n" + + "ρ(i) := α\n" + + "ρ(j) := α0\n" + + "ρ(k) := α1\n" + + "ρ(l) := α2\n" + + "return"; + Program p = c.compile(source); + VM vm = new VM(p); + vm.loadProgam(); + for (int i = 0; i < 12; i++) { + vm.step(); + } + Assert.assertEquals(-1, (int) vm.getMemory().get("i")); + Assert.assertEquals(0, (int) vm.getMemory().get("j")); + Assert.assertEquals(1, (int) vm.getMemory().get("k")); + Assert.assertEquals(2, (int) vm.getMemory().get("l")); + } +} From edcf29e22181dfe644d1263eeb0f78f6ea45123c Mon Sep 17 00:00:00 2001 From: dorianreineccius Date: Sat, 23 May 2020 16:35:18 +0200 Subject: [PATCH 2/3] Fixes #26 --- ...ButtonStepIntoControllerActionListener.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/controllers/Actionlisteners/JButtonStepIntoControllerActionListener.java b/src/controllers/Actionlisteners/JButtonStepIntoControllerActionListener.java index 53542fc..58a5b0a 100644 --- a/src/controllers/Actionlisteners/JButtonStepIntoControllerActionListener.java +++ b/src/controllers/Actionlisteners/JButtonStepIntoControllerActionListener.java @@ -4,6 +4,8 @@ import java.awt.event.ActionListener; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.Map.Entry; import model.JModel; @@ -27,18 +29,26 @@ public void actionPerformed(ActionEvent e) { System.out.println("Button Step-Into has been clicked. controller"); } model.Step(); - // gui.SetListMemory(model.getMemory()); - // gui.SetListRegister(model.getRegister()); - String[] memory = new String[this.model.getMemoryLabelMap().size()]; + HashMap memoryToList = new HashMap<>(); + for (Entry entry : this.model.getMemoryLabelMap().entrySet()) { + memoryToList.put(entry.getKey(),this.model.getMemory().get(entry.getKey())); + } + for (Entry entry : this.model.getMemory().entrySet()) { + memoryToList.putIfAbsent(entry.getKey(), entry.getValue()); + } + + String[] memory = new String[memoryToList.size()]; String[] register = new String[this.model.getRegisterLabelMap().size()]; int counter = 0; - for (Entry entry : this.model.getMemoryLabelMap().entrySet()) { + for (Entry entry : memoryToList.entrySet()) { memory[counter] = "ρ(" + entry.getKey() + ") := " + this.model.getMemory().get(entry.getKey()); counter++; } + System.out.println(this.model.getMemoryLabelMap()); + System.out.println(this.model.getMemory()); counter = 0; for (Entry entry : this.model.getRegisterLabelMap().entrySet()) { From 13d90d59b4fb7acd23b80f2a1ce8bec4786c9602 Mon Sep 17 00:00:00 2001 From: dorianreineccius Date: Sat, 23 May 2020 16:38:28 +0200 Subject: [PATCH 3/3] Deleted Debug outputs --- .../JButtonStepIntoControllerActionListener.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/controllers/Actionlisteners/JButtonStepIntoControllerActionListener.java b/src/controllers/Actionlisteners/JButtonStepIntoControllerActionListener.java index 58a5b0a..dc56128 100644 --- a/src/controllers/Actionlisteners/JButtonStepIntoControllerActionListener.java +++ b/src/controllers/Actionlisteners/JButtonStepIntoControllerActionListener.java @@ -47,8 +47,6 @@ public void actionPerformed(ActionEvent e) { + this.model.getMemory().get(entry.getKey()); counter++; } - System.out.println(this.model.getMemoryLabelMap()); - System.out.println(this.model.getMemory()); counter = 0; for (Entry entry : this.model.getRegisterLabelMap().entrySet()) {