diff --git a/output.txt b/output.txt
new file mode 100644
index 0000000..e69de29
diff --git a/pom.xml b/pom.xml
index 267e826..2dbdca8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -21,8 +21,8 @@
- 11
- 11
+ 17
+ 17
UTF-8
@@ -87,4 +87,4 @@
-
\ No newline at end of file
+
diff --git a/src/main/java/org/mindustack/minterpreter/Executor.java b/src/main/java/org/mindustack/minterpreter/Executor.java
deleted file mode 100644
index 4be326d..0000000
--- a/src/main/java/org/mindustack/minterpreter/Executor.java
+++ /dev/null
@@ -1,164 +0,0 @@
-package org.mindustack.minterpreter;
-
-import java.io.PrintStream;
-import java.util.ArrayList;
-import java.util.HashMap;
-
-public class Executor {
- final Variable constReg = new Variable("const");
- private final Variable stepper;
- HashMap registers = new HashMap<>();
- Variable counter;
- HashMap memories;
- Module module;
-
- ArrayList instructionInvokers = new ArrayList<>();
- boolean jumped = false;
- int steps;
- private PrintStream PrintStream;
-
- Executor(Module module, PrintStream ps) {
- this.PrintStream = ps;
- counter = new Variable("@counter");
- registers.put("@counter", counter);
- stepper = new Variable("@steper");
- stepper.setValue(1);
- // registers.put("@stepper", stepper);
- memories = new HashMap<>();
- this.module = module;
- instructionInvokers.add(new opInstInvoker());
- instructionInvokers.add(new jumpInstInvoker());
- instructionInvokers.add(new setInstInvoker());
- instructionInvokers.add(new readInstInvoker());
- instructionInvokers.add(new writeInstInvoker());
-
- instructionInvokers.add(new stopInstInvoker());
-
- counter.setValue(0);
- steps = 0;
-
- }
-
- public Memory getMemory(String name) {
- if (memories.containsKey(name)) {
- return memories.get(name);
- } else {
- Memory memory = new Memory(name);
- memories.put(name, memory);
- return memory;
- }
-
- }
-
- Variable getRegister(String name) {
- Variable result;
-
- try {
- var v = Double.parseDouble(name);
-
- result = constReg.setValue(v);
- } catch (NumberFormatException e) {
- if (registers.containsKey(name)) {
- result = registers.get(name);
- } else {
- result = new Variable(name);
- registers.put(name, result);
- }
-
- }
-
- return result;
- }
-
- public void jump(String des) {
-
- counter.setValue(module.labels.get(des));
- jumped = true;
- }
-
- public Executor run() {
-
- run(1048576);
- return this;
- }
-
- public Executor run(int steps) {
- PrintStream.println(" * :excuted");
-
- this.steps = steps;
- while (this.steps > 0) {
- execute();
- }
-
- return this;
-
- }
-
- void execute() {
-
- if (counter.value >= module.insts.size()) {
- counter.setValue(0);
- }
- int start = (int) Math.round(counter.value);
-
- String[] inst = module.insts.get(start);
-
- // }
- counter.value++;
-
-
- for (InstructionInvoker instructionInvoker : this.instructionInvokers) {
- if (instructionInvoker.check(inst)) {
- instructionInvoker.execute(this);
- break;
- }
- }
-
-
- PrintStream.println(dump(start));
-
-
- stepper.value++;
- steps--;
- }
-
- String dump(int base) {
-
- var stringBuilder = new StringBuilder();
-
- stringBuilder.append("------------------------------------------------------------------------------------<")
-
- .append(((int) stepper.value)).append(">\n");
- ArrayList insts = module.insts;
- for (int i = Integer.max(0, base - 5), instsSize = insts.size(); (i < instsSize) && (i < base + 5); i++) {
- String[] inst = insts.get(i);
-
- stringBuilder.append(i).append("\t");
- if (i == base) {
- stringBuilder.append("*");
- for (String s : inst) {
- stringBuilder.append(s).append(" ");
- }
-
- stringBuilder.append('\n');
- for (Variable variable : this.registers.values()) {
- stringBuilder.append("\t\t\t\t\t\t\t\t\t|").append(variable.name).append(':').append(variable.value)
- .append('\n');
- }
- } else {
- stringBuilder.append(' ');
- for (String s : inst) {
- stringBuilder.append(s).append(" ");
- }
- stringBuilder.append('\n');
- }
-
- }
-
- return stringBuilder.toString();
- }
-
- public void stop() {
- this.steps = 0;
- }
-}
diff --git a/src/main/java/org/mindustack/minterpreter/Instruction.java b/src/main/java/org/mindustack/minterpreter/Instruction.java
index ff539d2..620002f 100644
--- a/src/main/java/org/mindustack/minterpreter/Instruction.java
+++ b/src/main/java/org/mindustack/minterpreter/Instruction.java
@@ -5,83 +5,182 @@
*/
package org.mindustack.minterpreter;
-public class Instruction {
- public Variable r2;
- public Variable r1;
- public Variable ret;
- public static final String AddOperation = "add";
- public static final String SubOperation = "sub";
- public static final String MulOperation = "mul";
-
- public static final String OrOperation = "or";
- public static final String XorOperation = "xor";
- public static final String ShiftLeftOperation = "shl";// 移位
- public static final String ShiftRightOperation = "shr";// too
- public static final String DivOperation = "div";
- public static final String ModOpertion = "mod";
-
- public static final String LessThanOperation = "lessThan";
- public static final String GreaterThanOperation = "greaterThan";
- public static final String LessThanEqOperation = "lessThanEq";
- public static final String GreaterThanEqOperation = "greaterThanEq";
- public static final String EqualOperation = "equal";
- public static final String NotEqualOperation = "notEqual";
-
- public Instruction() {
-
- }
-}
-
-class ALUinst extends Instruction {
- public String op;
+public abstract class Instruction {
+ public Variable r2;
+ public Variable r1;
+ public Variable ret;
- public ALUinst(String op, Variable ret, Variable r1, Variable r2) {
+ abstract void execute(Minterpreter env);
- this.op = op;
- this.ret = ret;
- this.r1 = r1;
- this.r2 = r2;
+}
- }
+class ALUinst extends Instruction {
+ public String op;
+
+ public ALUinst(String op, Variable ret, Variable r1, Variable r2) {
+
+ this.op = op;
+ this.ret = ret;
+ this.r1 = r1;
+ this.r2 = r2;
+
+ }
+
+ @Override
+ void execute(Minterpreter env) {
+ switch (this.op) {
+
+ case "add" -> {
+ // System.out.println(r1.value);
+ ret.value = r1.value + r2.value;
+ }
+ case "sub" -> {
+
+ ret.value = r1.value - r2.value;
+ }
+ case "mul" -> {
+ ret.value = r1.value * r2.value;
+ }
+ case "div" -> {
+ ret.value = r1.value / r2.value;
+ }
+ case "mod" -> {
+ ret.value = r1.value % r2.value;
+ }
+ case "equal" -> {
+ ret.value = (double) (r1.value == r2.value ? 1 : 0);
+ }
+ case "notequal" -> {
+ ret.value = (double) (r1.value != r2.value ? 1 : 0);
+ }
+ case "lessThan" -> {
+ ret.value = (double) (r1.value < r2.value ? 1 : 0);
+ }
+ case "greaterThan" -> {
+ ret.value = (double) (r1.value > r2.value ? 1 : 0);
+ }
+ case "lessThanEq" -> {
+ ret.value = (double) (r1.value <= r2.value ? 1 : 0);
+ }
+ case "greaterThanEq" -> {
+ ret.value = (double) (r1.value >= r2.value ? 1 : 0);
+ }
+ case "and" -> {
+ ret.value = (double) ((int) r1.value & (int) r2.value);
+ }
+ case "or" -> {
+ ret.value = (double) ((int) r1.value | (int) r2.value);
+ }
+ case "xor" -> {
+ ret.value = (double) ((int) r1.value ^ (int) r2.value);
+ }
+ default -> {
+ throw new RuntimeException("invalid operation:" + op);
+ }
+ }
+ }
}
class SetInst extends Instruction {
- public SetInst(Variable ret, Variable r1) {
- this.ret = ret;
- this.r2 = r1;
+ public SetInst(Variable ret, Variable r1) {
+ this.ret = ret;
+ this.r1 = r1;
+
+ }
- }
+ @Override
+ void execute(Minterpreter env) {
+ this.ret.value = r1.value;
+ }
}
class StopInst extends Instruction {
- public StopInst() {
- }
+ public StopInst() {
+ }
+
+ @Override
+ void execute(Minterpreter env) {
+ (env.counter.value)--;
+ }
}
class JmpInst extends Instruction {
- public String label;
- public String op;
-
- public JmpInst(String label, String op, Variable r1, Variable r2) {
- this.label = label;
- this.op = op;
- this.r1 = r2;
- this.r2 = r2;
- }
+ public String label;
+ public String op;
+
+ public JmpInst(String label, String op, Variable r1, Variable r2) {
+ this.label = label;
+ this.op = op;
+ this.r1 = r2;
+ this.r2 = r2;
+ }
+
+ @Override
+ void execute(Minterpreter env) {
+ boolean jmp = false;
+ switch (this.op) {
+ case "always" -> {
+ jmp = true;
+ }
+ case "equal" -> {
+ jmp = (r1.value == r2.value);
+ }
+ case "notequal" -> {
+ jmp = (r1.value != r2.value);
+ }
+ case "lessThan" -> {
+ jmp = (r1.value < r2.value);
+ }
+ case "greaterThan" -> {
+ jmp = (r1.value > r2.value);
+ }
+ case "lessThanEq" -> {
+ jmp = (r1.value <= r2.value);
+ }
+ case "greaterThanEq" -> {
+ jmp = (r1.value >= r2.value);
+ }
+ case "and" -> {
+ jmp = (r1.value * r2.value == 0);
+ }
+ case "or" -> {
+ jmp = (!(r1.value + r2.value == 0));
+ }
+ case "xor" -> {
+ jmp = (r1.value + r2.value == 1);
+ }
+ default -> {
+ throw new RuntimeException("invalid condition:" + op);
+ }
+ }
+ if (jmp)
+ env.counter.value = env.instructions.indexOf(env.labels.get(this.label)) - 1;
+ }
}
class ReadInst extends Instruction {
- public ReadInst(Variable data, Variable memory, Variable index) {
- this.ret = data;
- this.r1 = memory;
- this.r2 = index;
- }
+ public ReadInst(Variable data, Variable memory, Variable index) {
+ this.ret = data;
+ this.r1 = memory;
+ this.r2 = index;
+ }
+
+ @Override
+ void execute(Minterpreter env) {
+ env.memFctr.getMem(r1).read(ret, r2);
+ //System.out.println(r1.value + "r" + r2.value);
+ }
}
class WriteInst extends Instruction {
- public WriteInst(Variable ret, Variable memory, Variable index) {
- this.ret = ret;
- this.r1 = memory;
- this.r2 = index;
- }
+ public WriteInst(Variable ret, Variable memory, Variable index) {
+ this.ret = ret;
+ this.r1 = memory;
+ this.r2 = index;
+ }
+
+ @Override
+ void execute(Minterpreter env) {
+ env.memFctr.getMem(r1).write(ret, r2);
+ }
}
diff --git a/src/main/java/org/mindustack/minterpreter/InstructionInvoker.java b/src/main/java/org/mindustack/minterpreter/InstructionInvoker.java
deleted file mode 100644
index e9ac376..0000000
--- a/src/main/java/org/mindustack/minterpreter/InstructionInvoker.java
+++ /dev/null
@@ -1,245 +0,0 @@
-package org.mindustack.minterpreter;
-
-import java.util.ArrayList;
-
-public abstract class InstructionInvoker {
- public static final String AddOperation = "add";
- public static final String SubOperation = "sub";
- public static final String MulOperation = "mul";
-
-
- public static final String OrOperation = "or";
- public static final String XorOperation = "xor";
- public static final String ShiftLeftOperation = "shl";//移位
- public static final String ShiftRightOperation = "shr";//too
- public static final String DivOperation = "div";
- public static final String ModOpertion = "mod";
-
-
- public static final String LessThanOperation = "lessThan";
- public static final String GreaterThanOperation = "greaterThan";
- public static final String LessThanEqOperation = "lessThanEq";
- public static final String GreaterThanEqOperation = "greaterThanEq";
- public static final String EqualOperation = "equal";
- public static final String NotEqualOperation = "notEqual";
-
- protected static String[] instruction;
- private final String Identifier;
-
- public static ArrayList instructionInvokers=new ArrayList<>();
- protected InstructionInvoker(String identifier) {
- this. Identifier = identifier;
- instructionInvokers.add(this);
- }
-
- boolean check(String[] inst) {
- if (inst[0].equals(Identifier)) {
- instruction = inst;
- return true;
- }
- return false;
-
- }
-
- abstract void execute(Executor executer);
-
-}
-
-class setInstInvoker extends InstructionInvoker {
-
-
- setInstInvoker() {
- super("set");
- }
-
- @Override
- void execute(Executor executer) {
-
- var r1 = executer.getRegister(instruction[1]);
- var r2 = executer.getRegister(instruction[2]);
- r1.setValue(r2);
- }
-}
-class stopInstInvoker extends InstructionInvoker {
-
-
- stopInstInvoker() {
- super("stop");
- }
-
- @Override
- void execute(Executor executer) {
-
- executer.stop();
- }
-}
-class opInstInvoker extends InstructionInvoker {
-
-
- opInstInvoker() {
- super("op");
- }
-
- @Override
- void execute(Executor executer) {
-
-
- String op = instruction[1];
- var ret = executer.getRegister(instruction[2]);
- var r1 = executer.getRegister(instruction[3]);
- var r2 = executer.getRegister(instruction[4]);
-
- switch (op) {
- case AddOperation:
- ret.setValue(r1.value + r2.value);
- break;
-
- case SubOperation:
- ret.setValue(r1.value - r2.value);
- break;
-
- case MulOperation:
- ret.setValue(r1.value * r2.value);
- break;
-
- case DivOperation:
- ret.setValue(r1.value / r2.value);
- break;
-
- case ModOpertion:
- ret.setValue(r1.value % r2.value);
- break;
-
- case LessThanOperation:
- ret.setValue(r1.value < r2.value);
- break;
-
- case LessThanEqOperation:
- ret.setValue(r1.value <= r2.value);
- break;
-
- case GreaterThanOperation:
- ret.setValue(r1.value > r2.value);
- break;
-
- case GreaterThanEqOperation:
- ret.setValue(r1.value >= r2.value);
- break;
-
- case EqualOperation:
- ret.setValue(r1.value == r2.value);
- break;
-
- case NotEqualOperation:
- ret.setValue(r1.value != r2.value);
- break;
-
- default:
- // 处理未知操作符的逻辑
- break;
- }
-
-
- }
-}
-
-class jumpInstInvoker extends InstructionInvoker {
-
-
- jumpInstInvoker() {
- super("jump");
- }
-
- @Override
- void execute(Executor executer) {
-
- var dest = instruction[1];
- String op = instruction[2];
- if (op.equals("always")) {
- executer.jump(dest);
- return;
- }
- var r1 = executer.getRegister(instruction[3]);
- var r2 = executer.getRegister(instruction[4]);
-
- switch (op) {
- case LessThanOperation:
- if (r1.value < r2.value) {
- executer.jump(dest);
- }
- break;
-
- case LessThanEqOperation:
- if (r1.value <= r2.value) {
- executer.jump(dest);
- }
- break;
-
- case GreaterThanOperation:
- if (r1.value > r2.value) {
- executer.jump(dest);
- }
- break;
-
- case GreaterThanEqOperation:
- if (r1.value >= r2.value) {
- executer.jump(dest);
- }
- break;
-
- case EqualOperation:
- if (r1.value == r2.value) {
- executer.jump(dest);
- }
- break;
-
- case NotEqualOperation:
- if (r1.value != r2.value) {
- executer.jump(dest);
- }
- break;
-
- default:
- // 处理未知操作符的逻辑
- break;
- }
-
-
- }
-}
-
-class readInstInvoker extends InstructionInvoker {
-
-
- readInstInvoker() {
- super("read");
- }
-
- @Override
- void execute(Executor executer) {
-
- var ret = executer.getRegister(instruction[1]);
-
- var m1 = executer.getMemory(instruction[2]);
- var index = executer.getRegister(instruction[3]);
- ret.setValue(m1.read(index.value));
- }
-}
-
-class writeInstInvoker extends InstructionInvoker {
-
-
- writeInstInvoker() {
- super("write");
- }
-
- @Override
- void execute(Executor executer) {
-
- var v = executer.getRegister(instruction[1]);
-
- var m1 = executer.getMemory(instruction[2]);
- var index = executer.getRegister(instruction[3]);
- m1.write(index.value, v.value);
- }
-}
diff --git a/src/main/java/org/mindustack/minterpreter/Memory.java b/src/main/java/org/mindustack/minterpreter/Memory.java
index c11d543..3db2b56 100644
--- a/src/main/java/org/mindustack/minterpreter/Memory.java
+++ b/src/main/java/org/mindustack/minterpreter/Memory.java
@@ -2,28 +2,40 @@
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
-public class Memory {
- ArrayList mem;
+class MemoryFactory {
+ Map memories = new HashMap<>();
- public Memory(String name) {
- mem = new ArrayList<>(Collections.nCopies(512, 0.0));
-
- }
+ MemoryFactory() {
+ }
- public double read(int index) {
- return mem.get(index);
+ public Memory getMem(Variable memoryIndex) {
+ if (memories.containsKey(memoryIndex.asInteger())) {
+ return memories.get(memoryIndex.asInteger());
}
+ Memory m = new Memory();
+ memories.put(memoryIndex.asInteger(), m);
+ return m;
- public double read(double index) {
- return mem.get((int) Math.round(index));
- }
+ }
- public void write(int index, double value) {
- mem.set(index, value);
- }
+}
- public void write(double index, double value) {
- mem.set((int) Math.round(index), value);
- }
+public class Memory {
+ List mem;
+
+ protected Memory() {
+ mem = new ArrayList(Collections.nCopies(512, 0.0));
+ }
+
+ public void read(Variable ret, Variable index) {
+ ret.value = mem.get((int) index.asInteger());
+ }
+
+ public void write(Variable source, Variable index) {
+ mem.set((int) index.asInteger(), source.value);
+ }
}
diff --git a/src/main/java/org/mindustack/minterpreter/Minterpreter.java b/src/main/java/org/mindustack/minterpreter/Minterpreter.java
index a7f31c6..66d6a62 100644
--- a/src/main/java/org/mindustack/minterpreter/Minterpreter.java
+++ b/src/main/java/org/mindustack/minterpreter/Minterpreter.java
@@ -1,104 +1,119 @@
package org.mindustack.minterpreter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.PrintStream;
+import java.lang.reflect.Array;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Minterpreter {
-
- public static int test(String code, double expectation, int limit, PrintStream PrintStream) {
- double value = new Executor(Parser.parse(code), PrintStream).run(limit).getRegister("a0").value;
-
- if (Math.abs(value - expectation) < 1e-3) {
- return 0;
- }
- ;
- return 1;
- }
-
- public static int test(InputStream inputStream, double expectation, int limit, PrintStream PrintStream) {
- byte[] buffer = new byte[1024];
- String content = "";
- int length;
- // 从输入流中读取数据,直到没有数据为止
- try {
- while ((length = inputStream.read(buffer)) > 0) {
- // 将字节转换为字符串,拼接到内容变量中
- content += new String(buffer, 0, length);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- // 关闭输入流
-
- return test(content, expectation, limit, PrintStream);
- }
-
- // String code= """
- // start:
- // op add s d 1
- // set d s
- // jump start LessThan d 5
- // set f 1
- // """;
- //
- // var module = Parser.parse(code);
- // var executor = new Executor(module);
- // executor.run(32);
-
- public List instructions = new ArrayList<>();
- public Map labels = new HashMap<>();
-
- public List parse(String source) {
- source = source.trim();
- String[] splitLines = source.split("\n");
- String label = null;
- for (String line : splitLines) {
- if (line.startsWith("#")) {
- continue;
- } else if (line.endsWith(":")) {
- label = line.replaceAll("[:]", "");
-
- } else if (line.equals("")) {
-
- continue;
- } else {
- Instruction instruction;
- String[] split = line.split(" ");
- switch (split[0]) {
-
- case "op": {
- instruction = new ALUinst(
- split[1],
- Variable.getVar(split[2]),
- Variable.getVar(split[3]),
- Variable.getVar(split[4]));
- }
- case "set": {
- instruction = new SetInst(Variable.getVar(split[1]),
- Variable.getVar(split[2]));
- }
- case "jump": {
- instruction = new JmpInst(split[1], split[2], Variable.getVar(split[3]),
- Variable.getVar(split[4]));
- }
- if (label != null) {
- labels.put(label, instruction);
- label = null;
- }
-
- instructions.add(instruction);
- }
- }
-
- }
- return instructions;
- }
-
+ public List instructions = new ArrayList<>();
+ public Map labels = new HashMap<>();
+ public VariableFactory varFctr;
+ public MemoryFactory memFctr;
+
+ public Minterpreter() {
+ this.memFctr = new MemoryFactory();
+ this.varFctr = new VariableFactory();
+ }
+
+ public Minterpreter parse(String source) {
+
+ source = source.trim();
+ String[] splitLines = source.split("\n");
+ String[] split;
+ String label = null;
+ for (String line : splitLines) {
+ if (line.startsWith("#") || line.startsWith("//")) {
+ continue;
+ } else if (line.endsWith(":")) {
+ label = line.replaceAll("[:]", "");
+
+ } else if (line.equals("")) {
+
+ continue;
+ } else {
+ Instruction instruction = null;
+ split = line.split(" ");
+
+ // System.out.println(Arrays.toString(split));
+ switch (split[0]) {
+
+ case "op" -> {
+ //System.out.println(Arrays.toString(split));
+ instruction = new ALUinst(
+ split[1],
+ this.varFctr.getVar(split[2]),
+ this.varFctr.getVar(split.length < 4 ? null : split[3]),
+ this.varFctr.getVar(split.length < 5 ? null : split[4]));
+ }
+ case "set" -> {
+ instruction = new SetInst(this.varFctr.getVar(split[1]),
+ this.varFctr.getVar(split[2]));
+ }
+ case "stop" -> {
+ instruction = new StopInst();
+ }
+ case "jump" -> {
+
+ instruction = new JmpInst(split[1],
+ split[2],
+ this.varFctr.getVar(split.length < 4 ? null : split[3]),
+ this.varFctr.getVar(split.length < 5 ? null : split[4]));
+ }
+ case "write" -> {
+ //System.out.println(Arrays.toString(split));
+ instruction = new WriteInst(
+ this.varFctr.getVar(split[1]),
+ this.varFctr.getVar(split[2]),
+ this.varFctr.getVar(split[3]));
+ }
+ case "read" -> {
+ //System.out.println(Arrays.toString(split));
+ instruction = new ReadInst(
+ this.varFctr.getVar(split[1]),
+ this.varFctr.getVar(split[2]),
+ this.varFctr.getVar(split[3]));
+ }
+ default -> {
+ throw new RuntimeException("invalid inst:" + Arrays.toString(split));
+ }
+ }
+
+ if (label != null) {
+
+ labels.put(label, instruction);
+ label = null;
+ }
+ instructions.add(instruction);
+ }
+ }
+
+ return this;
+ }
+
+ Variable counter;
+
+ public Minterpreter run() {
+ Instruction inst;
+
+ counter = this.varFctr.getVar("@counter");
+ int step = 0;
+ while (step < 1024) {
+
+ inst = instructions.get((int) counter.asInteger());
+ inst.execute(this);
+ step++;
+ counter.value++;
+ if (counter.value >= instructions.size())
+ break;
+ }
+ return this;
+
+ }
+
+ public double getRet() {
+ return this.varFctr.getVar("ret").value;
+ }
}
diff --git a/src/main/java/org/mindustack/minterpreter/Module.java b/src/main/java/org/mindustack/minterpreter/Module.java
deleted file mode 100644
index 526170b..0000000
--- a/src/main/java/org/mindustack/minterpreter/Module.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package org.mindustack.minterpreter;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-
-public class Module {
- ArrayList insts = new ArrayList<>();
- HashMap labels = new HashMap<>();
-}
diff --git a/src/main/java/org/mindustack/minterpreter/Parser.java b/src/main/java/org/mindustack/minterpreter/Parser.java
deleted file mode 100644
index 31c2f92..0000000
--- a/src/main/java/org/mindustack/minterpreter/Parser.java
+++ /dev/null
@@ -1,49 +0,0 @@
-package org.mindustack.minterpreter;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-public class Parser {
- static Module parse(String code) {
-
- Module module = new Module();
-
- code=code.trim();
- String[] splitLines = code.split("\n");
-
-
- for (String line : splitLines) {
- if (line.startsWith("#")) {
- continue;
- } else if (line.endsWith(":")) {
- module.labels.put(line.replaceAll("[:]", ""), module.insts.size());
- }else if(line.equals("")){
-
- continue;
- } else {
- String[] split = line.split(" ");
- module.insts.add(split);
-//
- }
- }
-
- return module;
- }
- static Module parse(InputStream inputStream) throws IOException {
-
- byte[] buffer = new byte[1024];
- String content="";
- int length;
- //从输入流中读取数据,直到没有数据为止
- while ((length = inputStream.read(buffer)) > 0) {
- //将字节转换为字符串,拼接到内容变量中
- content += new String(buffer, 0, length);
- }
-
- //关闭输入流
- inputStream.close();
- return parse(content);
- }
-
-
-}
diff --git a/src/main/java/org/mindustack/minterpreter/Variable.java b/src/main/java/org/mindustack/minterpreter/Variable.java
index 11db95c..309fa60 100644
--- a/src/main/java/org/mindustack/minterpreter/Variable.java
+++ b/src/main/java/org/mindustack/minterpreter/Variable.java
@@ -1,52 +1,54 @@
package org.mindustack.minterpreter;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.Map;
+class VariableFactory{
+ VariableFactory(){}
+
+ public Map variables = new HashMap<>();
+
+ public Variable getVar(String name) {
+ try {
+ double d = Double.parseDouble(name);
+ return new Const(d);
+ } catch (Exception e) {
+ }
+ if (name == null)
+ return null;
+ if (variables.containsKey(name)) {
+ return variables.get(name);
+ }
+ Variable v = new Variable();
+ v.name = name;
+ v.value = 0;
+ if (name.startsWith("memory")) {
+
+ v.value = Integer.parseInt(name.substring(6));
+ }
+ variables.put(name, v);
+ return v;
+ }
+}
public class Variable {
- public String name;
- public double value;
- public static Map variables = new HashMap<>();
-
- private Variable() {
- }
-
- public static Variable getVar(String name) {
- if(name==null)name="";
- if (variables.containsValue(name)) {
- return variables.get(name);
- }
- Variable v = new Variable();
- v.name = name;
- v.value = 0;
- variables.put(name, v);
- return v;
- }
-
- public Variable setValue(boolean value) {
-
- if (value) {
- this.value = 1;
+ public String name;
+ public double value;
- } else {
- this.value = 0;
- }
+ protected Variable() {
+ }
- return this;
+ public long asInteger() {
+ long i = Math.round(value);
+ this.value=i;
+ return i;
+ }
- }
-
- public void setValue(Variable reg) {
- this.setValue(reg.value);
- }
-
- public Variable setValue(double value) {
-
- this.value = value;
+}
- return this;
+class Const extends Variable {
- }
+ Const(double v) {
+ this.value = v;
+ }
}
diff --git a/src/test/java/Test.java b/src/test/java/Test.java
index 5c289ef..21191c6 100644
--- a/src/test/java/Test.java
+++ b/src/test/java/Test.java
@@ -1,14 +1,78 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
+import java.io.FileNotFoundException;
+import java.io.PrintStream;
+
+import org.junit.jupiter.api.BeforeAll;
import org.mindustack.minterpreter.Minterpreter;
public class Test {
-
-
- @org.junit.jupiter.api.Test
- public void test(){
- String code= "\t\t\t\t\t\t\t\t\t\t# -- Start function main\nstart:\nop add a0 a0 1\nset a1 a0\njump start lessThan a1 5\nop sub a0 a0 a1\nstop";
- int value =Minterpreter.test(code, 0, 100, System.out);
- assertEquals( 0,value);
- }
+
+ @BeforeAll
+ public static void setup() throws FileNotFoundException {
+ System.setOut(new PrintStream("./output.txt"));
+ }
+
+ @org.junit.jupiter.api.Test
+ public void testJmp() {
+
+ String code = """
+ jump begin always
+ start:
+ set ret 1
+ stop
+ begin:
+ set ret 2
+ jump start equal ret 2
+ """;
+ Minterpreter i = new Minterpreter();
+ double ret = i.parse(code).run().getRet();
+ ;
+ assertEquals(1, ret);
+ }
+
+ @org.junit.jupiter.api.Test
+ public void testOp() {
+
+ assertEquals(2, new Minterpreter()
+ .parse("op add ret 1 1")
+ .run()
+ .getRet());
+ }
+
+ @org.junit.jupiter.api.Test
+ public void testRW() {
+
+ assertEquals(2, new Minterpreter()
+ .parse("write 2 0 1\nread ret 0 1")
+ .run()
+ .getRet());
+ }
+
+ @org.junit.jupiter.api.Test
+ public void testSingleinst() {
+
+ assertEquals(1, new Minterpreter().parse("set ret 1 ")
+ .run()
+ .getRet());
+ }
+
+ @org.junit.jupiter.api.Test
+ public void testParse() {
+
+ String code = """
+
+ //start of code
+
+ op add a0 a0 1
+ set a1 a0
+ jump start lessThan a1 5
+ op sub a0 a0 a1
+ write 2 m0 a0
+ read a1 m0 2
+ stop
+ """;
+ Minterpreter i = new Minterpreter();
+ i.parse(code);
+ }
}