Skip to content

Commit cc78406

Browse files
committed
concurrency implemented
1 parent f30edc1 commit cc78406

File tree

6 files changed

+48
-14
lines changed

6 files changed

+48
-14
lines changed

README.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ A standard-conforming Funge-98 interpreter.
2525

2626
### Additional goals
2727
- [ ] File IO
28-
- [ ] Concurrency
28+
- [x] Concurrency

src/main/java/com/falsepattern/jfunge/interpreter/ExecutionContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
public interface ExecutionContext {
1212
InstructionPointer[] allIPs();
1313
InstructionPointer IP();
14+
InstructionPointer cloneIP();
1415
FungeSpace fungeSpace();
1516
int dimensions();
1617
boolean stopped();

src/main/java/com/falsepattern/jfunge/interpreter/Interpreter.java

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Interpreter implements ExecutionContext {
1818
@Getter
1919
private final FungeSpace fungeSpace = new FungeSpace(' ');
2020

21-
private final InstructionPointer IP = new InstructionPointer();
21+
private final List<InstructionPointer> IPs = new ArrayList<>();
2222

2323
private final InstructionManager baseInstructionManager = new InstructionManager();
2424

@@ -36,6 +36,12 @@ public class Interpreter implements ExecutionContext {
3636

3737
private Integer exitCode = null;
3838

39+
private InstructionPointer currentIP = null;
40+
41+
private InstructionPointer clone = null;
42+
43+
private int nextUUID = 0;
44+
3945
public static int executeProgram(boolean trefunge, String[] args, byte[] program, long iterLimit, InputStream input, OutputStream output) {
4046
val interpreter = new Interpreter(trefunge, args, input, output);
4147
interpreter.fungeSpace().loadFileAt(0, 0, 0, program, trefunge);
@@ -60,21 +66,31 @@ public Interpreter(boolean trefunge, String[] args, InputStream input, OutputStr
6066
baseInstructionManager.loadInstructionSet(Funge98.INSTANCE);
6167
this.input = input;
6268
this.output = output;
69+
val ip = new InstructionPointer();
70+
ip.UUID = nextUUID++;
71+
IPs.add(ip);
6372
}
6473

6574
@Override
6675
public InstructionPointer[] allIPs() {
67-
return new InstructionPointer[]{IP};
76+
return IPs.toArray(new InstructionPointer[0]);
6877
}
6978

7079
@Override
7180
public InstructionPointer IP() {
72-
return IP;
81+
return currentIP;
82+
}
83+
84+
@Override
85+
public InstructionPointer cloneIP() {
86+
clone = currentIP.deepCopy();
87+
clone.UUID = nextUUID++;
88+
return clone;
7389
}
7490

7591
@Override
7692
public boolean stopped() {
77-
if (dead()) {
93+
if (IPs.size() == 0) {
7894
exitCode = 0;
7995
}
8096
return exitCode != null;
@@ -90,10 +106,6 @@ public int exitCode() {
90106
return exitCode == null ? 0 : exitCode;
91107
}
92108

93-
public boolean dead() {
94-
return IP.isDead();
95-
}
96-
97109
@Override
98110
public void interpret(int opcode) {
99111
if (opcode == '"') {
@@ -165,8 +177,21 @@ public int paradigm() {
165177
}
166178

167179
public void tick() {
168-
for (InstructionPointer ip : allIPs()) {
169-
interpret(fungeSpace().get(ip.position));
180+
currentIP = null;
181+
for (int i = 0; i < IPs.size(); i++) {
182+
currentIP = IPs.get(i);
183+
if (IP().isDead()) {
184+
IPs.remove(i);
185+
i--;
186+
continue;
187+
}
188+
interpret(fungeSpace().get(IP().position));
189+
if (clone != null) {
190+
IPs.add(i++, clone);
191+
clone = null;
192+
}
193+
}
194+
for (val ip: IPs) {
170195
step(ip);
171196
}
172197
}

src/main/java/com/falsepattern/jfunge/interpreter/instructions/Funge98.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ public static void sysInfo(ExecutionContext ctx) {
402402
//9 teamnumber
403403
s.push(0);
404404
//8 uuid
405-
s.push(0);
405+
s.push(ctx.IP().UUID);
406406
//7 dimensions
407407
s.push(ctx.dimensions());
408408
//6 separator
@@ -416,7 +416,7 @@ public static void sysInfo(ExecutionContext ctx) {
416416
//2 bpc
417417
s.push(4);
418418
//1 flags
419-
s.push(0);
419+
s.push(0b00000001);
420420
if (n > 0) {
421421
int curr = s.pick(n - 1);
422422
for (int i = s.size(); i >= tossSize; i--) {
@@ -432,6 +432,12 @@ public static void quit(ExecutionContext ctx) {
432432
ctx.stop(q);
433433
}
434434

435+
@Instr('t')
436+
public static void split(ExecutionContext ctx) {
437+
val clone = ctx.cloneIP();
438+
clone.delta.mul(-1);
439+
}
440+
435441
@Override
436442
public void load(ObjIntConsumer<Instruction> instructionSet) {
437443
InstructionSet.super.load(instructionSet);

src/main/java/com/falsepattern/jfunge/interpreter/instructions/InstructionManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ public Instruction fetch(int c) {
6060

6161
@Override
6262
public InstructionManager deepCopy() {
63-
return null;
63+
return new InstructionManager(this);
6464
}
6565
}

src/main/java/com/falsepattern/jfunge/ip/InstructionPointer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class InstructionPointer implements Copiable<InstructionPointer> {
1717
@Getter
1818
private boolean dead = false;
1919
public boolean stringMode = false;
20+
public int UUID;
2021

2122
public InstructionPointer() {
2223
position = new Vector3i();
@@ -36,6 +37,7 @@ private InstructionPointer(InstructionPointer original) {
3637
stringMode = original.stringMode;
3738
instructionManager = original.instructionManager.deepCopy();
3839
customStorage = new TObjectIntHashMap<>(original.customStorage);
40+
UUID = 0;
3941
}
4042

4143
public void die() {

0 commit comments

Comments
 (0)