Skip to content

Commit f2db699

Browse files
committed
Simplify toString() in BValue subclasses, Change BChoice to not depend on BExprs
1 parent 2bf4852 commit f2db699

File tree

7 files changed

+36
-39
lines changed

7 files changed

+36
-39
lines changed

src/virtual-machine/src/main/java/org/smoothbuild/virtualmachine/bytecode/expr/base/BArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public String exprToString() throws BytecodeException {
6262
return new ToStringBuilder(getClass().getSimpleName())
6363
.addField("hash", hash())
6464
.addField("type", type())
65-
.addListField("elements", elements(BValue.class).map(BExpr::exprToString))
65+
.addListField("elements", elements(BValue.class).map(BExpr::toString))
6666
.toString();
6767
}
6868
}

src/virtual-machine/src/main/java/org/smoothbuild/virtualmachine/bytecode/expr/base/BChoice.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public BChoiceType type() {
3434
return (BChoiceType) super.kind();
3535
}
3636

37-
public BSubExprs components() throws BytecodeException {
37+
public Components components() throws BytecodeException {
3838
var members = members("index", "chosen");
3939
var index = members.get(INDEX_INDEX).asInstanceOf(BInt.class);
4040

@@ -51,20 +51,20 @@ public BSubExprs components() throws BytecodeException {
5151
if (!itemType.equals(expectedExprType)) {
5252
throw new MemberHasWrongTypeException(hash(), kind(), "chosen", expectedExprType, itemType);
5353
}
54-
return new BSubExprs(index, value);
54+
return new Components(index, value);
5555
}
5656

5757
@Override
5858
public String exprToString() throws BytecodeException {
5959
return new ToStringBuilder(getClass().getSimpleName())
6060
.addField("hash", hash())
6161
.addField("type", type())
62-
.addListField("members", components().toList().map(BExpr::exprToString))
62+
.addField("index", components().index())
63+
.addField("chosen", components().chosen())
6364
.toString();
6465
}
6566

66-
public static record BSubExprs(BInt index, BValue chosen) implements BExprs {
67-
@Override
67+
public static record Components(BInt index, BValue chosen) {
6868
public List<BExpr> toList() {
6969
return list(index, chosen);
7070
}

src/virtual-machine/src/main/java/org/smoothbuild/virtualmachine/bytecode/expr/base/BTuple.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public String exprToString() throws BytecodeException {
6969
return new ToStringBuilder(getClass().getSimpleName())
7070
.addField("hash", hash())
7171
.addField("type", type())
72-
.addListField("elements", elements().map(BExpr::exprToString))
72+
.addListField("elements", elements().map(BExpr::toString))
7373
.toString();
7474
}
7575
}

src/virtual-machine/src/main/java/org/smoothbuild/virtualmachine/evaluate/base/BParamRefInliner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private BSwitch rewriteSwitch(BSwitch switch_, Resolver resolver)
108108

109109
private BCombine rewriteCombine(BCombine combine, Resolver resolver)
110110
throws BytecodeException, ParamRefIndexOutOfBoundsException {
111-
var items = combine.subExprs().items();
111+
var items = combine.subExprs().toList();
112112
var rewrittenItems = rewriteExprs(items, resolver);
113113
if (items.equals(rewrittenItems)) {
114114
return combine;

src/virtual-machine/src/test/java/org/smoothbuild/virtualmachine/bytecode/expr/BExprCorruptedTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.smoothbuild.virtualmachine.bytecode.expr.base.BBool;
3434
import org.smoothbuild.virtualmachine.bytecode.expr.base.BCall;
3535
import org.smoothbuild.virtualmachine.bytecode.expr.base.BChoice;
36+
import org.smoothbuild.virtualmachine.bytecode.expr.base.BChoice.Components;
3637
import org.smoothbuild.virtualmachine.bytecode.expr.base.BChoose;
3738
import org.smoothbuild.virtualmachine.bytecode.expr.base.BCombine;
3839
import org.smoothbuild.virtualmachine.bytecode.expr.base.BExpr;
@@ -429,8 +430,7 @@ void learning_test() throws Exception {
429430
var chosen = bString("abc");
430431
var dataHash = hash(hash(index), hash(chosen));
431432
var hash = hash(hash(choiceType), dataHash);
432-
assertThat(((BChoice) dbGet(hash)).components())
433-
.isEqualTo(new BChoice.BSubExprs(index, chosen));
433+
assertThat(((BChoice) dbGet(hash)).components()).isEqualTo(new Components(index, chosen));
434434
}
435435

436436
@Test

src/virtual-machine/src/test/java/org/smoothbuild/virtualmachine/bytecode/expr/base/BChoiceTest.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.junit.jupiter.api.Test;
99
import org.smoothbuild.common.collect.List;
1010
import org.smoothbuild.virtualmachine.bytecode.BytecodeException;
11+
import org.smoothbuild.virtualmachine.bytecode.expr.base.BChoice.Components;
1112
import org.smoothbuild.virtualmachine.dagger.VmTestContext;
1213

1314
public class BChoiceTest extends VmTestContext {
@@ -43,7 +44,7 @@ void type() throws Exception {
4344
@Test
4445
void components_contains_object_passed_to_builder() throws Exception {
4546
var choice = bChoice();
46-
assertThat(choice.components()).isEqualTo(new BChoice.BSubExprs(bInt(0), bString("7")));
47+
assertThat(choice.components()).isEqualTo(new Components(bInt(0), bString("7")));
4748
}
4849

4950
@Nested
@@ -89,21 +90,19 @@ void to_string() throws Exception {
8990
assertThat(bChoice().toString())
9091
.isEqualTo(
9192
"""
92-
BChoice(
93-
hash = 656815d265879fc6dbd78bdc187affd260d2b6af8bcfad10dc9b32f5c5ae9d4b
94-
type = {String|Int}
95-
members = [
96-
BInt(
97-
hash = 7188b43d5debd8d65201a289a38515321a8419bc78b29e75675211deff8b08ba
98-
type = Int
99-
value = 0
100-
)
101-
BString(
102-
hash = 1e1c0b706a66964d2af072b61122f728afb591ebfeacaec9ef1b846e00a16676
103-
type = String
104-
value = "7"
105-
)
106-
]
107-
)""");
93+
BChoice(
94+
hash = 656815d265879fc6dbd78bdc187affd260d2b6af8bcfad10dc9b32f5c5ae9d4b
95+
type = {String|Int}
96+
index = BInt(
97+
hash = 7188b43d5debd8d65201a289a38515321a8419bc78b29e75675211deff8b08ba
98+
type = Int
99+
value = 0
100+
)
101+
chosen = BString(
102+
hash = 1e1c0b706a66964d2af072b61122f728afb591ebfeacaec9ef1b846e00a16676
103+
type = String
104+
value = "7"
105+
)
106+
)""");
108107
}
109108
}

src/virtual-machine/src/test/java/org/smoothbuild/virtualmachine/bytecode/expr/base/BSwitchTest.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,16 @@ void to_string() throws Exception {
120120
choice = BChoice(
121121
hash = 656815d265879fc6dbd78bdc187affd260d2b6af8bcfad10dc9b32f5c5ae9d4b
122122
type = {String|Int}
123-
members = [
124-
BInt(
125-
hash = 7188b43d5debd8d65201a289a38515321a8419bc78b29e75675211deff8b08ba
126-
type = Int
127-
value = 0
128-
)
129-
BString(
130-
hash = 1e1c0b706a66964d2af072b61122f728afb591ebfeacaec9ef1b846e00a16676
131-
type = String
132-
value = "7"
133-
)
134-
]
123+
index = BInt(
124+
hash = 7188b43d5debd8d65201a289a38515321a8419bc78b29e75675211deff8b08ba
125+
type = Int
126+
value = 0
127+
)
128+
chosen = BString(
129+
hash = 1e1c0b706a66964d2af072b61122f728afb591ebfeacaec9ef1b846e00a16676
130+
type = String
131+
value = "7"
132+
)
135133
)
136134
handlers = BCombine(
137135
hash = 2a737302c93c91e983667dfe73b2f73f278d17ea8a8c954ff18b8001a68fc595

0 commit comments

Comments
 (0)