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

bugfix/#26-new-memory-cells-not-listed #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,14 +29,20 @@ 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<String, Integer> memoryToList = new HashMap<>();
for (Entry<String, Integer> entry : this.model.getMemoryLabelMap().entrySet()) {
memoryToList.put(entry.getKey(),this.model.getMemory().get(entry.getKey()));
}
for (Entry<String, Integer> 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<String, Integer> entry : this.model.getMemoryLabelMap().entrySet()) {
for (Entry<String, Integer> entry : memoryToList.entrySet()) {
memory[counter] = "ρ(" + entry.getKey() + ") := "
+ this.model.getMemory().get(entry.getKey());
counter++;
Expand Down
10 changes: 5 additions & 5 deletions src/model/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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] );
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/model/VM.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/rules/three.rules
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

#1. load
#(?<adr1>α\d*|ρ[(](\w|(α\d*)|\d+|ρ[(](\w|\d+)[)])[)]):=(?<adr2>(-\d+|\d+)|α\d*|ρ[(](\w|(α\d*)|\d+|ρ[(](\w|\d+)[)])[)])
(?<adr1>α\d*|ρ[(]([^\)]+)[)]):=(?<adr2>(-\d+|\d+)|α\d*|ρ[(](ρ[(]([^\)]+)[)]|[^\)]+)[)])
(?<adr1>α\d*|ρ[(]([0-9a-zA-Z]+|ρ[(][0-9a-zA-Z]+[)])[)]):=(?<adr2>(-\d+|\d+)|α\d*|ρ[(](ρ[(]([0-9a-zA-Z]+)[)]|[0-9a-zA-Z]+)[)])

#2. arithmetic operation
#(?<adr1>α\d*|ρ[(](\w|(α\d*)|\d+|ρ[(](\w|\d+)[)])[)]):=(?<adr2>(-\d+|\d+)|α\d*|ρ[(](\w|(α\d*)|\d+|ρ[(](\w|\d+)[)])[)])(?<op>[+*-÷%])(?<adr3>(-\d+|\d+)|α\d*|ρ[(](\w|(α\d*)|\d+|ρ[(](\w|\d+)[)])[)])
(?<adr1>α\d*|ρ[(]([^\)]+)[)]):=(?<adr2>(-\d+|\d+)|α\d*|ρ[(](ρ[(]([^\)]+)[)]|[^\)]+)[)])(?<op>[+*-÷%])(?<adr3>(-\d+|\d+)|α\d*|ρ[(](ρ[(]([^\)]+)[)]|[^\)]+)[)])
(?<adr1>α\d*|ρ[(]([0-9a-zA-Z]+)[)]):=(?<adr2>(-\d+|\d+)|α\d*|ρ[(](ρ[(]([0-9a-zA-Z]+)[)]|[0-9a-zA-Z]+)[)])(?<op>[+*-÷%])(?<adr3>(-\d+|\d+)|α\d*|ρ[(](ρ[(]([0-9a-zA-Z]+)[)]|[0-9a-zA-Z]+)[)])

#3.conditional jump
if(?<adr1>α\d*)(?<op><|>|=)(?<adr2>α\d*|\d+)thengoto(?<adr3>\w+)
Expand Down
43 changes: 43 additions & 0 deletions test/model/Compiler0AddressTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
45 changes: 45 additions & 0 deletions test/model/Compiler2AddressTest.java
Original file line number Diff line number Diff line change
@@ -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]);

}
}
99 changes: 99 additions & 0 deletions test/model/CompilerTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}