Examples fixed, some bugs found

This commit is contained in:
Andrey Breslav
2010-12-30 17:01:13 +03:00
parent df5bba0a00
commit f3f4a361d0
17 changed files with 57 additions and 48 deletions
+3 -2
View File
@@ -1,3 +1,5 @@
fun foo() {
addMouseListener(object MouseAdapter() { addMouseListener(object MouseAdapter() {
private var clickCount = 0; private var clickCount = 0;
@@ -19,5 +21,4 @@ val GOD = object {
}; };
// Groovy like syntax: too many things to assume }
addMouseListener([mouseClicked : {e => doFoo()}]);
+6 -6
View File
@@ -11,16 +11,16 @@ class BinaryTree<T> : IMutableSet<T> {
private var root : TreeNode private var root : TreeNode
private var version = 0 private var version = 0
override var size : Int { get; private set; } // override var size : Int { get; private set; }
this(compare : Comparison<T>) { this(compare : Comparison<T>) {
this.compare = asMatchableComparison(comparison) this.compare = asMatchableComparison(comparison)
} }
this() : this(naturalOrder<T>) { this() : this(naturalOrder<T>()) {
} }
private extension [operator] fun T.compareTo(other : T) : Int = compare(this, other) private [operator] fun T.compareTo(other : T) : Int = compare(this, other)
override fun contains(item : T) : Boolean { override fun contains(item : T) : Boolean {
return contains(root, item) return contains(root, item)
@@ -129,7 +129,7 @@ class BinaryTree<T> : IMutableSet<T> {
override fun iterator() : IIterator<T> = mutableIterator() override fun iterator() : IIterator<T> = mutableIterator()
override fun mutableIterator() : IMutableIterator<T> = new IMutableIterator() { override fun mutableIterator() : IMutableIterator<T> = object IMutableIterator {
val version = BinaryTree.this.version val version = BinaryTree.this.version
val down = Stack<TreeNode>() val down = Stack<TreeNode>()
val up = Stack<TreeNode>() val up = Stack<TreeNode>()
@@ -172,9 +172,9 @@ class BinaryTree<T> : IMutableSet<T> {
} }
} }
override val hasNext : Boolean { override val hasNext : Boolean
get() = !down.isEmpty || !up.isEmpty get() = !down.isEmpty || !up.isEmpty
}
override fun remove() { override fun remove() {
checkVersion() checkVersion()
+3 -3
View File
@@ -19,11 +19,11 @@ fun mostSignificantBit(x : INumber) = (x and oneBit(x.bits - 1) != 0) as Int
fun countOnes(x : INumber) = if (x == 0) 0 else mostSignificantBit(x) + countOnes(x shl 1) fun countOnes(x : INumber) = if (x == 0) 0 else mostSignificantBit(x) + countOnes(x shl 1)
extension fun Int.matchMask(mask : Int) = this and mask == mask fun Int.matchMask(mask : Int) = this and mask == mask
interface class INumber<T : this> : IComparable<T> { virtual class INumber<T : this> : IComparable<T> {
val bits : Int val bits : Int
[operator] fun plus(other : T) : T [operator] fun plus(other : T) : T
[operator] fun shl(bits : Int) : T [operator] fun shl(bits : Int) : T
... // ...
} }
+6 -6
View File
@@ -22,22 +22,22 @@ class AntBuilder {
abstract class ClassPathEntry {} abstract class ClassPathEntry {}
class Module : ClassPathEntry { class Module : ClassPathEntry {
fun classpath(entries : ClassPathEntry...) { ... } fun classpath(entries : ClassPathEntry/*...*/) { /*...*/ }
var targetLevel : String var targetLevel : String
fun src(src : String) { ... } fun src(src : String) { /*...*/ }
} }
class Library : ClassPathEntry { class Library : ClassPathEntry {
fun classpath(entries : ClassPathEntry...) { ... } fun classpath(entries : ClassPathEntry/*...*/) { /*...*/ }
} }
fun library(initializer : Library.{ () : Library}) { fun library(initializer : { Library.() : Library}) {
val lib = new Library() val lib = new Library()
lib.initializer() lib.initializer()
return lib return lib
} }
fun classpath... fun classpath(/*...*/)
fun module... fun module(/*...*/)
} }
+1 -1
View File
@@ -6,7 +6,7 @@ type f_tuple = {((T, E)) : X}
//type f_tuple = {((T, E)) => X} //type f_tuple = {((T, E)) => X}
type hof = { (X) : {(T) : Y} } type hof = { (X) : {(T) : Y} }
//type hof = { (X) => {(T) => Y} } //type hof = { (X) => {(T) => Y} }
type hof2 = { {(X) : Y} : {(Y) : Z} } type hof2 = { ({(X) : Y}) : {(Y) : Z} }
//type hof2 = { {(X) => Y} => {(Y) => Z} } //type hof2 = { {(X) => Y} => {(Y) => Z} }
+2 -2
View File
@@ -1,6 +1,6 @@
sealed class Vertex<V>(val data : V) class Vertex<V>(val data : V)
sealed class Edge<V, E>(val from : V, val data : E, val to : V) class Edge<V, E>(val from : V, val data : E, val to : V)
class Graph<V, E> { class Graph<V, E> {
+1 -1
View File
@@ -1,4 +1,4 @@
interface class IPushPop<T> { virtual class IPushPop<T> {
val isEmpty : Boolean val isEmpty : Boolean
fun push(item : T) fun push(item : T)
fun pop() : T fun pop() : T
+3 -2
View File
@@ -1,2 +1,3 @@
l filter {it.x} map {it.foo} aggregate {a, b => a + b} fun foo() {
l filter {it.x} map {it.foo} aggregate {(a, b) => a + b}
}
+2 -2
View File
@@ -16,10 +16,10 @@ class List<T> {
} }
extension Map<E> for T extension Map<E, T>
where where
T : Iterable<E>, T : Iterable<E>,
class object T : Buildable<E, T> { class object T : Buildable<E, T> for T {
fun map<R>(f : {(E) : R}) : T<R> = { fun map<R>(f : {(E) : R}) : T<R> = {
val builder = T.newBuilder() val builder = T.newBuilder()
+3 -3
View File
@@ -1,4 +1,4 @@
sealed class Queue<T> : IPushPop<T> { class Queue<T> : IPushPop<T> {
private class Item<T>(val data : T, var next : Item<T>) private class Item<T>(val data : T, var next : Item<T>)
private var head : Item<T> = null private var head : Item<T> = null
@@ -26,8 +26,8 @@ sealed class Queue<T> : IPushPop<T> {
result result
} }
override val isEmpty { override val isEmpty
get() = head == null get() = head == null
}
} }
+3 -3
View File
@@ -1,4 +1,4 @@
sealed class Stack<T> : IPushPop<T> { class Stack<T> : IPushPop<T> {
private val data = new ArrayList<T>(); private val data = new ArrayList<T>();
override fun push(item : T) { override fun push(item : T) {
@@ -7,7 +7,7 @@ sealed class Stack<T> : IPushPop<T> {
override fun pop() = data.removeLast() override fun pop() = data.removeLast()
override val isEmpty { // TODO: This is UGLY :( override val isEmpty
get() = data.isEmpty get() = data.isEmpty
}
} }
+1 -2
View File
@@ -29,6 +29,5 @@ class UnionFind {
} }
} }
extension val Int.isOdd : Boolean { val Int.isOdd : Boolean
get() = this % 2 != 0 get() = this % 2 != 0
}
+3 -3
View File
@@ -2,12 +2,12 @@
These declarations are "shallow" in the sense that they are not really compiled, only the type-checker uses them These declarations are "shallow" in the sense that they are not really compiled, only the type-checker uses them
*/ */
interface class ReadOnlyArray<out T> : ISized { virtual class ReadOnlyArray<out T> : ISized {
[operator] fun get(index : Int) : T [operator] fun get(index : Int) : T
} }
interface class WriteOnlyArray<in T> : ISized { // This is needed to keep IIterator's <T> covariant virtual class WriteOnlyArray<in T> : ISized { // This is needed to keep IIterator's <T> covariant
[operator] fun set(index : Int, value : T) [operator] fun set(index : Int, value : T)
} }
class MutableArray<T> : ReadOnlyArray<T>, WriteOnlyArray<T> {...} class MutableArray<T> : ReadOnlyArray<T>, WriteOnlyArray<T> {/*...*/}
+7 -7
View File
@@ -5,7 +5,7 @@ class ArrayList<T> : IMutableList<T> {
override fun iterator() : IIterator<T> = mutableIterator() override fun iterator() : IIterator<T> = mutableIterator()
override fun mutableIterator() : IMutableIterator<T> = new IMutableIterator() { // T is inferred override fun mutableIterator() : IMutableIterator<T> = object IMutableIterator() { // T is inferred
private val index = 0 private val index = 0
private var myVersion = version private var myVersion = version
@@ -21,9 +21,9 @@ class ArrayList<T> : IMutableList<T> {
data[index++] data[index++]
} }
override val hasNext { override val hasNext
get() = index < used get() = index < used
}
override fun remove() { override fun remove() {
checkVersion() checkVersion()
@@ -43,13 +43,13 @@ class ArrayList<T> : IMutableList<T> {
throw new IndexOutOfBoundsException(index) throw new IndexOutOfBoundsException(index)
} }
override val isEmpty { override val isEmpty
get() = used == 0 get() = used == 0
}
override val size {
override val size
get() = used get() = used
}
override fun set(index : Int, value : T) { override fun set(index : Int, value : T) {
checkIndex(index) checkIndex(index)
+4 -4
View File
@@ -4,9 +4,9 @@ virtual class IEquality {
} }
virtual class IHashable : IEquality { virtual class IHashable : IEquality {
val hashCode() : Integer { val hashCode : Integer
get() = (this as java.lang.Object).hashCode() get() = (this as java.lang.Object).hashCode()
}
} }
virtual class IMap<K, V> { virtual class IMap<K, V> {
@@ -16,7 +16,7 @@ virtual class IMap<K, V> {
fun containsKey(key : K) : Boolean fun containsKey(key : K) : Boolean
} }
class HashableWrapper(wraps val obj : Any) : IHashable class HashableWrapper wraps (val obj : Any) : IHashable
// equals and hashCode implementations are inherited // equals and hashCode implementations are inherited
[inline] fun Any.hashable() : HashableWrapper = new HashableWrapper(this) [inline] fun Any.hashable() : HashableWrapper = new HashableWrapper(this)
@@ -48,11 +48,11 @@ class HashMap<K, V> : IMap<K, V> {
class StrategyHashMap<K, V>(hashingStrategy : IHashingStrategy<K>) : IMap<K, V> { class StrategyHashMap<K, V>(hashingStrategy : IHashingStrategy<K>) : IMap<K, V> {
this() where (K : IHashable) : this(new DefaultHashingStrategy<K>()) {}
// where !(K : IHashable) // where !(K : IHashable)
this() : this(new JavaObjectHashingStrategy<K>()) {} this() : this(new JavaObjectHashingStrategy<K>()) {}
this() where (K : IHashable) : this(new DefaultHashingStrategy<K>()) {}
//... //...
} }
@@ -40,6 +40,7 @@ public class JetExpressionParsing extends AbstractJetParsing {
} }
}, },
// TODO: wrong priority for call: Random.nextInt().isOdd
POSTFIX(PLUSPLUS, MINUSMINUS), // typeArguments? valueArguments : arrayAccess POSTFIX(PLUSPLUS, MINUSMINUS), // typeArguments? valueArguments : arrayAccess
PREFIX(MINUS, PLUS, MINUSMINUS, PLUSPLUS, EXCL) { // attributes PREFIX(MINUS, PLUS, MINUSMINUS, PLUSPLUS, EXCL) { // attributes
@@ -61,11 +62,13 @@ public class JetExpressionParsing extends AbstractJetParsing {
RANGE(JetTokens.RANGE), RANGE(JetTokens.RANGE),
SIMPLE_NAME(IDENTIFIER), SIMPLE_NAME(IDENTIFIER),
ELVIS(JetTokens.ELVIS), ELVIS(JetTokens.ELVIS),
// TODO: RHS (type parameters)
NAMED_INFIX_OR_TYPE(IN_KEYWORD, NOT_IN, IS_KEYWORD, NOT_IS, AS_KEYWORD, COLON), NAMED_INFIX_OR_TYPE(IN_KEYWORD, NOT_IN, IS_KEYWORD, NOT_IS, AS_KEYWORD, COLON),
COMPARISON(LT, GT, LTEQ, GTEQ), COMPARISON(LT, GT, LTEQ, GTEQ),
EQUALITY(EQEQ, EXCLEQ, EQEQEQ, EXCLEQEQEQ), EQUALITY(EQEQ, EXCLEQ, EQEQEQ, EXCLEQEQEQ),
CONJUNCTION(ANDAND), CONJUNCTION(ANDAND),
DISJUNCTION(OROR), DISJUNCTION(OROR),
// TODO: RHS
MATCH(MATCH_KEYWORD), MATCH(MATCH_KEYWORD),
// TODO: don't build a binary tree, build a tuple // TODO: don't build a binary tree, build a tuple
ARROW(JetTokens.ARROW), ARROW(JetTokens.ARROW),
@@ -170,6 +173,7 @@ public class JetExpressionParsing extends AbstractJetParsing {
*/ */
private void parsePostfixExpression() { private void parsePostfixExpression() {
// System.out.println("post at " + tt()); // System.out.println("post at " + tt());
// TODO: call with a closure outside parentheses
PsiBuilder.Marker expression = mark(); PsiBuilder.Marker expression = mark();
parseBinaryExpression(Precedence.MEMBER_ACCESS); parseBinaryExpression(Precedence.MEMBER_ACCESS);
@@ -702,6 +702,7 @@ public class JetParsing extends AbstractJetParsing {
*/ */
public JetNodeType parseProperty() { public JetNodeType parseProperty() {
// TODO: var foo : Int { get; private set } // TODO: var foo : Int { get; private set }
// TODO: accessors for local variables
assert at(VAL_KEYWORD) || at(VAR_KEYWORD); assert at(VAL_KEYWORD) || at(VAR_KEYWORD);
@@ -1167,6 +1168,7 @@ public class JetParsing extends AbstractJetParsing {
* ; * ;
*/ */
public void parseTypeRef() { public void parseTypeRef() {
// TODO: Self type
PsiBuilder.Marker type = mark(); PsiBuilder.Marker type = mark();
parseAttributeList(); parseAttributeList();
@@ -1341,6 +1343,8 @@ public class JetParsing extends AbstractJetParsing {
* ; * ;
*/ */
public void parseValueParameterList(boolean isFunctionTypeContents, TokenSet recoverySet) { public void parseValueParameterList(boolean isFunctionTypeContents, TokenSet recoverySet) {
// TODO: val a, b, c : Int
PsiBuilder.Marker parameters = mark(); PsiBuilder.Marker parameters = mark();
myBuilder.disableEols(); myBuilder.disableEols();