• R/O
  • SSH

kink: Commit

Kink runtime


Commit MetaInfo

Révisionaed121df1343df6864afc4c478ee3a7374b780db (tree)
l'heure2020-10-25 19:38:54
Auteurmiyakawataku
Commitermiyakawataku

Message de Log

add JumpIfNeInsn

Change Summary

Modification

diff -r a9e19ac69f2e -r aed121df1343 src/main/java/org/kink_lang/kink/internal/program/wire/InsnVisitor.java
--- a/src/main/java/org/kink_lang/kink/internal/program/wire/InsnVisitor.java Sun Oct 25 18:54:49 2020 +0900
+++ b/src/main/java/org/kink_lang/kink/internal/program/wire/InsnVisitor.java Sun Oct 25 19:38:54 2020 +0900
@@ -288,6 +288,14 @@
288288 public abstract T visit(IfEqInsn insn);
289289
290290 /**
291+ * Visits JUMPIFNE instruction.
292+ *
293+ * @param insn the instruction.
294+ * @return the result of the visitor.
295+ */
296+ public abstract T visit(JumpIfNeInsn insn);
297+
298+ /**
291299 * Visits IFLT instruction.
292300 *
293301 * @param insn the instruction.
diff -r a9e19ac69f2e -r aed121df1343 src/main/java/org/kink_lang/kink/internal/program/wire/JumpIfNeInsn.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/kink_lang/kink/internal/program/wire/JumpIfNeInsn.java Sun Oct 25 19:38:54 2020 +0900
@@ -0,0 +1,82 @@
1+package org.kink_lang.kink.internal.program.wire;
2+
3+import java.util.List;
4+import java.util.Locale;
5+
6+import org.kink_lang.kink.Val;
7+
8+/**
9+ * JUMPIFNE insn.
10+ */
11+public class JumpIfNeInsn extends Insn {
12+
13+ /** The val which is tested against the stack top val. */
14+ private final Val test;
15+
16+ /** The destination label which is used when the two vals are not equal. */
17+ private final String destLabel;
18+
19+ /**
20+ * Constructs an insn.
21+ *
22+ * @param test the val which is tested against the stack top val.
23+ * @param destLabel the destination label which is used when the two vals are not equal.
24+ * @param pos the pos of the insn.
25+ */
26+ public JumpIfNeInsn(Val test, String destLabel, int pos) {
27+ super(pos);
28+ this.test = test;
29+ this.destLabel = destLabel;
30+ }
31+
32+ /**
33+ * Returns the val which is tested against the stack top val.
34+ *
35+ * @return the val which is tested against the stack top val.
36+ */
37+ public Val getTest() {
38+ return this.test;
39+ }
40+
41+ /**
42+ * Returns the label string of the destination.
43+ *
44+ * @return the label string of the destination.
45+ */
46+ public String getDestLabel() {
47+ return this.destLabel;
48+ }
49+
50+ @Override
51+ public <T> T accept(InsnVisitor<T> visitor) {
52+ return visitor.visit(this);
53+ }
54+
55+ @Override
56+ public String toString() {
57+ return String.format(Locale.ROOT, "JumpIfNeInsn(%s %s %d)",
58+ getTest(), getDestLabel(), getPos());
59+ }
60+
61+ /**
62+ * Returns the equality properties of the insn.
63+ */
64+ private List<Object> getProperties() {
65+ return List.of(getTest(), getDestLabel(), getPos());
66+ }
67+
68+ @Override
69+ public int hashCode() {
70+ return getProperties().hashCode();
71+ }
72+
73+ @Override
74+ public boolean equals(Object arg) {
75+ return arg == this
76+ || arg instanceof JumpIfNeInsn
77+ && getProperties().equals(((JumpIfNeInsn) arg).getProperties());
78+ }
79+
80+}
81+
82+// vim: et sw=4 sts=4 fdm=marker
diff -r a9e19ac69f2e -r aed121df1343 src/main/java/org/kink_lang/kink/internal/qcode/WireToQblockTranslator.java
--- a/src/main/java/org/kink_lang/kink/internal/qcode/WireToQblockTranslator.java Sun Oct 25 18:54:49 2020 +0900
+++ b/src/main/java/org/kink_lang/kink/internal/qcode/WireToQblockTranslator.java Sun Oct 25 19:38:54 2020 +0900
@@ -331,6 +331,13 @@
331331 }
332332
333333 @Override
334+ public Void visit(JumpIfNeInsn insn) {
335+ int destProgramCounter = wire.getProgramCounterOf(insn.getDestLabel()).getAsInt();
336+ this.qinsns.add(Qinsn.makeJumpIfNe(insn.getTest(), destProgramCounter, insn.getPos()));
337+ return null;
338+ }
339+
340+ @Override
334341 public Void visit(IfLtInsn insn) {
335342 this.qinsns.add(Qinsn.makeIfLt(insn.getPos()));
336343 return null;
diff -r a9e19ac69f2e -r aed121df1343 src/test/java/org/kink_lang/kink/internal/program/wire/JumpIfNeInsnTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/org/kink_lang/kink/internal/program/wire/JumpIfNeInsnTest.java Sun Oct 25 19:38:54 2020 +0900
@@ -0,0 +1,63 @@
1+package org.kink_lang.kink.internal.program.wire;
2+
3+import org.junit.jupiter.api.AfterAll;
4+import org.junit.jupiter.api.Test;
5+
6+import static org.assertj.core.api.Assertions.assertThat;
7+
8+import mockit.Expectations;
9+import mockit.Injectable;
10+
11+import org.kink_lang.kink.Vm;
12+import org.kink_lang.kink.internal.test.EqualityVerifier;
13+
14+public class JumpIfNeInsnTest {
15+
16+ private static Vm vm = Vm.newVm();
17+
18+ @AfterAll
19+ public static void nullifyVm() {
20+ JumpIfNeInsnTest.vm = null;
21+ }
22+
23+ @Test
24+ public void test_propreties() {
25+ JumpIfNeInsn insn = new JumpIfNeInsn(vm.str.of("str"), "foo", 42);
26+ assertThat(insn.getTest()).isEqualTo(vm.str.of("str"));
27+ assertThat(insn.getDestLabel()).isEqualTo("foo");
28+ assertThat(insn.getPos()).isEqualTo(42);
29+ }
30+
31+ @Test
32+ public void test_accept(@Injectable InsnVisitor<String> visitor) {
33+ JumpIfNeInsn insn = new JumpIfNeInsn(vm.str.of("str"), "foo", 42);
34+ new Expectations() {{
35+ visitor.visit(insn); result = "ok";
36+ }};
37+ assertThat(insn.accept(visitor)).isEqualTo("ok");
38+ }
39+
40+ @Test
41+ public void test_toString() {
42+ JumpIfNeInsn insn = new JumpIfNeInsn(vm.str.of("str"), "foo", 42);
43+ assertThat(insn.toString()).isEqualTo("JumpIfNeInsn(StrVal(str) foo 42)");
44+ }
45+
46+ @Test
47+ public void test_equality() {
48+ EqualityVerifier ev = new EqualityVerifier();
49+ ev.group("str foo 42",
50+ new JumpIfNeInsn(vm.str.of("str"), "foo", 42),
51+ new JumpIfNeInsn(vm.str.of("str"), "foo", 42));
52+ ev.group("num foo 42",
53+ new JumpIfNeInsn(vm.num.of(100), "foo", 42));
54+ ev.group("str bar 42",
55+ new JumpIfNeInsn(vm.str.of("str"), "bar", 42));
56+ ev.group("str foo 420",
57+ new JumpIfNeInsn(vm.str.of("str"), "foo", 420));
58+ ev.verify();
59+ }
60+
61+}
62+
63+// vim: et sw=4 sts=4 fdm=marker
diff -r a9e19ac69f2e -r aed121df1343 src/test/java/org/kink_lang/kink/internal/qcode/WireToQblockTranslatorTest.java
--- a/src/test/java/org/kink_lang/kink/internal/qcode/WireToQblockTranslatorTest.java Sun Oct 25 18:54:49 2020 +0900
+++ b/src/test/java/org/kink_lang/kink/internal/qcode/WireToQblockTranslatorTest.java Sun Oct 25 19:38:54 2020 +0900
@@ -726,6 +726,7 @@
726726 }
727727
728728 // }}}1
729+
729730 // IFEQ {{{1
730731
731732 @Test
@@ -757,6 +758,35 @@
757758 }
758759
759760 // }}}1
761+
762+ // JUMPIFNE {{{1
763+
764+ @Test
765+ public void test_translate_jumpifne() {
766+ Wire wire = new Wire(List.of(
767+ new KonstInsn(intHolder.of(42), 100),
768+ new JumpIfNeInsn(intHolder.of(42), "ne", 101),
769+ new KonstInsn(vm.str.of("ok"), 102),
770+ new RetInsn(103),
771+ new LabelPseudoInsn("ne"),
772+ new KonstInsn(vm.str.of("bad"), 104),
773+ new RetInsn(105)), 1, 0, List.of());
774+ WireToQblockTranslator w2q = new WireToQblockTranslator(
775+ vm, "foo.kn", "if 42 == 42 then return 'ok' else 'bad'", 0, 999);
776+
777+ Qblock qblock = w2q.apply(wire);
778+
779+ assertThat(qblock.getQinsns()).containsExactly(
780+ Qinsn.makeKonst(intHolder.of(42), 100),
781+ Qinsn.makeJumpIfNe(intHolder.of(42), 4, 101),
782+ Qinsn.makeKonst(vm.str.of("ok"), 102),
783+ Qinsn.makeRet(103),
784+ Qinsn.makeKonst(vm.str.of("bad"), 104),
785+ Qinsn.makeRet(105));
786+ }
787+
788+ // }}}1
789+
760790 // IFLT {{{1
761791
762792 @Test
Afficher sur ancien navigateur de dépôt.