Skip to content

Commit 0304996

Browse files
committed
some fixes and more examples
Former-commit-id: 22d9c71e438655606cd7764f9be259505b008422 [formerly 0a493d83a06383c0eacefeb3fe95668383ca592d] Former-commit-id: 059a41f59c2933123ba547445f65aac7742aa15e
1 parent 84e3796 commit 0304996

7 files changed

Lines changed: 50 additions & 32 deletions

File tree

kore/src/main/scala/org/kframework/Collection.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait Indexed[I, T] {
1515
trait Collection[T] {
1616
type This <: Collection[T]
1717

18-
def newBuilder: Builder[T, This]
18+
def newBuilder(): Builder[T, This]
1919

2020
def canEqual(that: Any): Boolean
2121

@@ -33,7 +33,7 @@ trait Collection[T] {
3333
def size: Int = { var s = 0; foreach { x => s += 1 }; s }
3434

3535
def map(f: T => T): This = {
36-
val builder = newBuilder
36+
val builder = newBuilder()
3737
foreach { builder += f(_) }
3838
builder.result()
3939
}

kore/src/main/scala/org/kframework/kore/Constructors.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,21 @@ object Constructors {
5353
kore.KVariable(name, Attributes())
5454
}
5555

56-
@annotation.varargs def KSequence(ks: K*) = kore.KSequence(ks)
56+
@annotation.varargs def KSequence(ks: K*) = kore.KSequence(ks: _*)
5757

5858
def KSequence(ks: java.util.List[K]) = kore.KSequence(KList(ks))
5959

6060
def KRewrite(left: K, right: K) = kore.KRewrite(left, right, Attributes())
6161

6262
def KRewrite(left: K, right: K, att: Attributes) = kore.KRewrite(left, right, att)
63-
63+
6464
def KInt(n: Int) = kore.KInt(n)
65-
65+
6666
def stream(c: KCollection) = org.kframework.Collections.stream(c);
67-
67+
6868
def toKList: Collector[K, KList] =
69-
Collector(() => new CombinerFromBuilder(kore.KList.newBuilder))
69+
Collector(() => new CombinerFromBuilder(kore.KList.newBuilder()))
70+
71+
def toKSequence: Collector[K, KSequence] =
72+
Collector(() => new CombinerFromBuilder(kore.KSequence.newBuilder()))
7073
}

kore/src/main/scala/org/kframework/kore/KCollection.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait KAbstractCollection extends KCollection {
2121

2222
override def equals(that: Any) = {
2323
canEqual(that) && (that match {
24-
case that: KAbstractCollection => that.canEqual(KAbstractCollection.this) && delegate == that.delegate
24+
case that: KAbstractCollection => that.canEqual(this) && delegate == that.delegate
2525
case _ => false
2626
})
2727
}
@@ -44,13 +44,13 @@ trait CanBuildKCollection {
4444
type This <: KCollection
4545

4646
def apply(l: K*): This = (canBuildFrom.apply() ++= l).result
47-
def newBuilder: Builder[K, This]
47+
def newBuilder(att: Attributes = Attributes()): Builder[K, This]
4848

4949
protected val fromList = apply _
5050

5151
implicit def canBuildFrom: generic.CanBuildFrom[This, K, This] =
5252
new generic.CanBuildFrom[This, K, This] {
53-
def apply(): mutable.Builder[K, This] = newBuilder
53+
def apply(): mutable.Builder[K, This] = newBuilder()
5454
def apply(from: This): mutable.Builder[K, This] = from.newBuilder.asInstanceOf[Builder[K, This]]
5555
}
5656
}

kore/src/main/scala/org/kframework/kore/builtins.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ object KSet {
6767
}
6868

6969
object KBag extends Sort with KLabel {
70-
def newBuilder = KList.newBuilder mapResult { new KBag(_) }
70+
def newBuilder = KList.newBuilder() mapResult { new KBag(_) }
7171

7272
val name: String = "Bag"
7373
}

kore/src/main/scala/org/kframework/kore/kast.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class KList(protected[kore] val delegate: List[K])
5252
type This = KList
5353

5454
override def canEqual(that: Any) = that.isInstanceOf[KList]
55-
override def newBuilder: Builder[K, KList] = KList.newBuilder
55+
def newBuilder(): Builder[K, KList] = KList.newBuilder(att)
5656

5757
def get(i: Int) = delegate.lift(i)
5858
def att = Attributes()
@@ -69,7 +69,7 @@ case class KApply(val klabel: KLabel, val klist: KList, val att: Attributes = At
6969

7070
def get(i: Int) = klist.get(i)
7171

72-
def newBuilder: Builder[K, KApply] = klist.newBuilder mapResult { new KApply(klabel, _, att) }
72+
def newBuilder: Builder[K, KApply] = klist.newBuilder() mapResult { new KApply(klabel, _, att) }
7373

7474
override def canEqual(that: Any) = that match {
7575
case t: KApply => t.klabel == klabel
@@ -89,13 +89,13 @@ case class ConcreteKLabel(name: String) extends KLabel with KORE {
8989
def apply(ks: K*) = new KApply(this, KList(ks))
9090
}
9191

92-
case class KSequence(val klist: KList, val att: Attributes = Attributes())
92+
case class KSequence(val ks: List[K], val att: Attributes = Attributes())
9393
extends KAbstractCollection with KSequenceToString with KORE {
9494
type This = KSequence
95-
def delegate = klist.delegate
95+
def delegate = ks
9696

97-
def newBuilder: Builder[K, KSequence] = klist.newBuilder mapResult { new KSequence(_, att) }
98-
def copy(att: Attributes): KSequence = new KSequence(klist, att)
97+
def newBuilder(): Builder[K, KSequence] = KSequence.newBuilder(att)
98+
def copy(att: Attributes): KSequence = new KSequence(ks, att)
9999

100100
def canEqual(that: Any) = that.isInstanceOf[KSequence]
101101
}
@@ -124,9 +124,9 @@ case class KRewrite(left: K, right: K, att: Attributes = Attributes())
124124
object KList extends CanBuildKCollection {
125125
type This = KList
126126

127-
def apply(l: Iterable[K]): KList = (newBuilder ++= l).result()
127+
def apply(l: Iterable[K]): KList = (newBuilder() ++= l).result()
128128

129-
def newBuilder: Builder[K, KList] =
129+
def newBuilder(att: Attributes = Attributes()): Builder[K, KList] =
130130
new AssocBuilder[K, List[K], KList](ListBuffer()) mapResult { new KList(_) }
131131

132132
def unapplySeq(l: KList): Option[Seq[K]] = Some(l.delegate.toSeq)
@@ -153,9 +153,9 @@ object KString extends Sort {
153153
object KSequence extends CanBuildKCollection {
154154
type This = KSequence
155155

156-
def newBuilder = KList.newBuilder mapResult { new KSequence(_, Attributes()) }
157-
158-
def fromJava(l: Array[K]) = new KSequence(KList(l: _*), Attributes())
156+
def newBuilder(att: Attributes = Attributes()) =
157+
new AssocBuilder[K, List[K], KSequence](ListBuffer())
158+
.mapResult { new KSequence(_, att) }
159159
}
160160

161161
object KRewrite {
@@ -166,7 +166,7 @@ object KRewrite {
166166
}
167167

168168
object EmptyK {
169-
def apply() = KSequence(KList(), Attributes())
169+
def apply() = KSequence(List(), Attributes())
170170
}
171171

172172
object KLabel extends ConcreteKLabel("KLabel") {

kore/src/test/java/org/kframework/kore/InterfaceTest.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,33 @@ public void example() {
1313
// Creating "A + 0 => A" programmatically
1414

1515
KRewrite k = KRewrite(
16-
KApply(KLabel("_+_"),
17-
KList(KVariable("A"), KToken(Sort("Int"), KString("0")))),
16+
KApply(KLabel("_+_"), KList(KVariable("A"), KToken(Sort("Int"), KString("0")))),
1817
KVariable("A"));
1918

2019
// Navigating it
2120
KLabel theLabel = ((KApply) k.left()).klabel();
2221
theLabel.name();
2322
}
24-
25-
@Test
26-
public void kListIsAssociative() {
23+
24+
@Test
25+
public void kListIsAssociative() {
2726
assertEquals(KList(KInt(1), KInt(2)), KList(KInt(1), KList(KInt(2))));
2827
}
29-
30-
@Test
28+
29+
@Test
3130
public void manipulatingKList() {
3231
KList l = stream(KList(KInt(1), KInt(2))).map(x -> KInt(3)).collect(toKList());
3332
assertEquals(KList(KInt(3), KInt(3)), l);
3433
}
34+
35+
@Test
36+
public void kSeqIsAssociative() {
37+
assertEquals(KSequence(KInt(1), KInt(2)), KSequence(KInt(1), KSequence(KInt(2))));
38+
}
39+
40+
@Test
41+
public void manipulatingKSeq() {
42+
KSequence l = stream(KSequence(KInt(1), KInt(2))).map(x -> KInt(3)).collect(toKSequence());
43+
assertEquals(KSequence(KInt(3), KInt(3)), l);
44+
}
3545
}

kore/src/test/scala/org/kframework/kore/KoreTest.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class KoreTest {
4646
case _ => throw new RuntimeException("match fail")
4747
}
4848
}
49-
49+
5050
@Test def testKListAssoc {
5151
assertEquals(KList(TestK), KList(KList(TestK)))
5252
}
@@ -64,7 +64,12 @@ class KoreTest {
6464
assertNotEquals(KList(5), KApply(KLabel("foo"), KList(5), Attributes()))
6565
assertNotEquals(KApply(KLabel("foo"), KList(5), Attributes()), KList(5))
6666
}
67-
67+
68+
@Test def testKSequenceEquals {
69+
assertEquals(KSequence(TestK), KSequence(TestK))
70+
assertEquals(KSequence(TestK, TestK), KSequence(TestK, TestK))
71+
}
72+
6873
@Test def testAttributes {
6974
assertEquals("[]", Attributes().toString())
7075
assertEquals("", Attributes().postfixString)

0 commit comments

Comments
 (0)