Skip to content

Commit 4da65e9

Browse files
committed
implement CPLI
1 parent 0548c31 commit 4da65e9

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.falsepattern.jfunge.Globals;
44
import com.falsepattern.jfunge.interpreter.ExecutionContext;
55
import com.falsepattern.jfunge.interpreter.instructions.fingerprints.BASE;
6+
import com.falsepattern.jfunge.interpreter.instructions.fingerprints.CPLI;
67
import com.falsepattern.jfunge.interpreter.instructions.fingerprints.FPDP;
78
import com.falsepattern.jfunge.interpreter.instructions.fingerprints.FPSP;
89
import com.falsepattern.jfunge.interpreter.instructions.fingerprints.HRTI;
@@ -45,6 +46,7 @@ public class Funge98 implements InstructionSet {
4546
static {
4647
addFingerprint(_3DSP.INSTANCE);
4748
addFingerprint(BASE.INSTANCE);
49+
addFingerprint(CPLI.INSTANCE);
4850
addFingerprint(FPSP.INSTANCE);
4951
addFingerprint(FPDP.INSTANCE);
5052
addFingerprint(HRTI.INSTANCE);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.falsepattern.jfunge.interpreter.instructions.fingerprints;
2+
3+
import com.falsepattern.jfunge.interpreter.ExecutionContext;
4+
import com.falsepattern.jfunge.interpreter.instructions.Fingerprint;
5+
import lombok.AccessLevel;
6+
import lombok.NoArgsConstructor;
7+
import lombok.SneakyThrows;
8+
import lombok.val;
9+
10+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
11+
public class CPLI implements Fingerprint {
12+
public static final CPLI INSTANCE = new CPLI();
13+
@Override
14+
public int code() {
15+
return 0x43504C49;
16+
}
17+
18+
@Instr('A')
19+
public static void add(ExecutionContext ctx) {
20+
val stack = ctx.stack();
21+
val bi = stack.pop();
22+
val br = stack.pop();
23+
val ai = stack.pop();
24+
val ar = stack.pop();
25+
stack.push(ar + br);
26+
stack.push(ai + bi);
27+
}
28+
29+
@Instr('D')
30+
public static void div(ExecutionContext ctx) {
31+
val stack = ctx.stack();
32+
val bi = stack.pop();
33+
val br = stack.pop();
34+
val ai = stack.pop();
35+
val ar = stack.pop();
36+
val r = (ar * br + ai * bi) / (br * br + bi * bi);
37+
val i = (ai * br - ar * bi) / (br * br + bi * bi);
38+
stack.push(r);
39+
stack.push(i);
40+
}
41+
42+
@Instr('M')
43+
public static void mul(ExecutionContext ctx) {
44+
val stack = ctx.stack();
45+
val bi = stack.pop();
46+
val br = stack.pop();
47+
val ai = stack.pop();
48+
val ar = stack.pop();
49+
val r = ar * br - ai * bi;
50+
val i = ar * bi + ai * br;
51+
stack.push(r);
52+
stack.push(i);
53+
}
54+
55+
@SneakyThrows
56+
@Instr('O')
57+
public static void output(ExecutionContext ctx) {
58+
val stack = ctx.stack();
59+
val i = stack.pop();
60+
val r = stack.pop();
61+
ctx.output().write(("(" + r + " + " + i + "i)").getBytes());
62+
}
63+
64+
@Instr('S')
65+
public static void sub(ExecutionContext ctx) {
66+
val stack = ctx.stack();
67+
val bi = stack.pop();
68+
val br = stack.pop();
69+
val ai = stack.pop();
70+
val ar = stack.pop();
71+
stack.push(ar - br);
72+
stack.push(ai - bi);
73+
}
74+
75+
@Instr('V')
76+
public static void length(ExecutionContext ctx) {
77+
val stack = ctx.stack();
78+
val i = stack.pop();
79+
val r = stack.pop();
80+
stack.push((int)Math.sqrt(r * r + i * i));
81+
}
82+
}

0 commit comments

Comments
 (0)