• R/O
  • SSH

kink: Commit

Kink runtime


Commit MetaInfo

Revision353114727d7d53a69c7f46a21b6824c20618dc33 (tree)
Time2022-01-15 00:40:29
Authormiyakawataku
Commitermiyakawataku

Log Message

add minArity, maxArity, and optCount to opt-vec-assignment itree

Change Summary

Incremental Difference

diff -r 3015795c1f8c -r 353114727d7d src/main/java/org/kink_lang/kink/internal/program/i2w/TranslationSession.java
--- a/src/main/java/org/kink_lang/kink/internal/program/i2w/TranslationSession.java Sat Jan 15 00:36:31 2022 +0900
+++ b/src/main/java/org/kink_lang/kink/internal/program/i2w/TranslationSession.java Sat Jan 15 00:40:29 2022 +0900
@@ -542,12 +542,13 @@
542542 // => { e1 e2 ,,, en #N }
543543 insns.add(new SpreadInsn(pos));
544544
545- int maxArity = mandatorySyms.size() + optSyms.size();
546- List<String> remainingLables = IntStream.rangeClosed(mandatorySyms.size(), maxArity)
545+ int minArity = assignment.minArity();
546+ int maxArity = assignment.maxArity();
547+ List<String> remainingLables = IntStream.rangeClosed(minArity, maxArity)
547548 .mapToObj(i -> makeLabel(String.format(Locale.ROOT, "remaining-%d", i)))
548549 .collect(Collectors.toList());
549550
550- for (int arity = mandatorySyms.size(); arity <= maxArity - 1; ++ arity) {
551+ for (int arity = minArity; arity <= maxArity - 1; ++ arity) {
551552 // { e1 e2 ,,, en #N } => { e1 e2 ,,, en #N #N }
552553 insns.add(new DupInsn(pos));
553554 // => { e1 e2 ,,, en #N }
@@ -555,7 +556,7 @@
555556 insns.add(new JumpIfNeInsn(intHolder.of(arity), notArity, pos));
556557 // => { e1 e2 ,,, eArity }
557558 insns.add(new RemoveInsn(pos));
558- for (int i = optSyms.size() - 1; i >= arity - mandatorySyms.size(); -- i) {
559+ for (int i = assignment.optCount() - 1; i >= arity - minArity; -- i) {
559560 if (shouldWriteOptVar.test(i)) {
560561 // => { e1 e2 ,,, eArity [] }
561562 insns.addAll(emptyVec(pos));
@@ -564,7 +565,7 @@
564565 }
565566 }
566567 // => { e1 e2 ,,, eArity }
567- String nextLabel = remainingLables.get(arity - mandatorySyms.size());
568+ String nextLabel = remainingLables.get(arity - minArity);
568569 insns.add(new GotoInsn(nextLabel, pos));
569570 // => { e1 e2 ,,, en #N }
570571 insns.add(new LabelPseudoInsn(notArity));
@@ -578,20 +579,19 @@
578579 // => { e1 e2 ,,, en }
579580 insns.add(new RemoveInsn(pos));
580581 // => { e1 e2 ,,, en }
581- String allRemainingLabel = remainingLables.get(optSyms.size());
582+ String allRemainingLabel = remainingLables.get(assignment.optCount());
582583 insns.add(new GotoInsn(allRemainingLabel, pos));
583584
584585 // => { e1 e2 ,,, en #N }
585586 insns.add(new LabelPseudoInsn(wrongNumberOfArgs));
586587
587588 // => bye
588- int minArity = mandatorySyms.size();
589589 insns.addAll(raiseWrongNumberOfArgs(pos, minArity, assignment.lhsRepr()));
590590
591591 // => { e1 e2 ,,, eMaxArity }
592- insns.add(new LabelPseudoInsn(remainingLables.get(optSyms.size())));
592+ insns.add(new LabelPseudoInsn(remainingLables.get(assignment.optCount())));
593593
594- for (int i = optSyms.size() - 1; i >= 0; -- i) {
594+ for (int i = assignment.optCount() - 1; i >= 0; -- i) {
595595 if (shouldWriteOptVar.test(i)) {
596596 // => { e1 e2 ,,, [eo] }
597597 insns.addAll(singleVec(pos));
diff -r 3015795c1f8c -r 353114727d7d src/main/java/org/kink_lang/kink/internal/program/itree/OptVecAssignmentItree.java
--- a/src/main/java/org/kink_lang/kink/internal/program/itree/OptVecAssignmentItree.java Sat Jan 15 00:36:31 2022 +0900
+++ b/src/main/java/org/kink_lang/kink/internal/program/itree/OptVecAssignmentItree.java Sat Jan 15 00:40:29 2022 +0900
@@ -61,6 +61,33 @@
6161 .collect(Collectors.joining(" ", "[", "]"));
6262 }
6363
64+ /**
65+ * Returns the minimum arity of the left hand side.
66+ *
67+ * @return the minimum arity of the left hand side.
68+ */
69+ public int minArity() {
70+ return mandatorySyms().size();
71+ }
72+
73+ /**
74+ * Returns the maximum arity of the left hand side.
75+ *
76+ * @return the maximum arity of the left hand side.
77+ */
78+ public int maxArity() {
79+ return minArity() + optCount();
80+ }
81+
82+ /**
83+ * Returns the count of opt parameters.
84+ *
85+ * @return the count of opt parameters.
86+ */
87+ public int optCount() {
88+ return optSyms().size();
89+ }
90+
6491 @Override
6592 public <T> T accept(ItreeVisitor<T> visitor) {
6693 return visitor.visit(this);
diff -r 3015795c1f8c -r 353114727d7d src/test/java/org/kink_lang/kink/internal/program/itree/OptVecAssignmentItreeTest.java
--- a/src/test/java/org/kink_lang/kink/internal/program/itree/OptVecAssignmentItreeTest.java Sat Jan 15 00:36:31 2022 +0900
+++ b/src/test/java/org/kink_lang/kink/internal/program/itree/OptVecAssignmentItreeTest.java Sat Jan 15 00:40:29 2022 +0900
@@ -37,6 +37,36 @@
3737 }
3838
3939 @Test
40+ public void test_minArity() {
41+ Itree rhs = new StrItree("Rhs", 20);
42+ OptVecAssignmentItree itree = new OptVecAssignmentItree(
43+ List.of("A", "B"),
44+ List.of("C", "D", "E"),
45+ rhs, 99);
46+ assertThat(itree.minArity()).isEqualTo(2);
47+ }
48+
49+ @Test
50+ public void test_maxArity() {
51+ Itree rhs = new StrItree("Rhs", 20);
52+ OptVecAssignmentItree itree = new OptVecAssignmentItree(
53+ List.of("A", "B"),
54+ List.of("C", "D", "E"),
55+ rhs, 99);
56+ assertThat(itree.maxArity()).isEqualTo(5);
57+ }
58+
59+ @Test
60+ public void test_optCount() {
61+ Itree rhs = new StrItree("Rhs", 20);
62+ OptVecAssignmentItree itree = new OptVecAssignmentItree(
63+ List.of("A", "B"),
64+ List.of("C", "D", "E"),
65+ rhs, 99);
66+ assertThat(itree.optCount()).isEqualTo(3);
67+ }
68+
69+ @Test
4070 public void test_accept() {
4171 Itree rhs = new StrItree("Rhs", 20);
4272 OptVecAssignmentItree itree = new OptVecAssignmentItree(
Show on old repository browser