diff --git a/core-lib/Collections.ns b/core-lib/Collections.ns index 12ba94b2e..954a8e36a 100644 --- a/core-lib/Collections.ns +++ b/core-lib/Collections.ns @@ -129,6 +129,10 @@ class Collections usingKernel: kernel = Value ( public removeAll = ( items removeAll ) + + public asArray = ( + ^ items asArray + ) ) : ( public new = ( ^ self new: initialSize ) ) @@ -138,6 +142,16 @@ class Collections usingKernel: kernel = Value ( items do: [ :it | it == anObject ifTrue: [ ^ true ] ]. ^ false ) + + public remove: anObject = ( + | newItems | + newItems:: Vector new. + [ items isEmpty ] + whileFalse: [ | it | + it:: items remove. + it == anObject ifFalse: [ newItems append: it ] ]. + items:: newItems + ) ) : ( public new = ( ^ self new: initialSize ) ) @@ -236,6 +250,41 @@ class Collections usingKernel: kernel = Value ( current:: current next ] ) + public remove: aKey = ( + ^ remove: aKey ifAbsent: nil + ) + + public remove: aKey ifAbsent: aBlock = ( + | hash i current | + hash:: hash: aKey. + i:: bucketIdx: hash. + current:: buckets at: i. + + current isNil + ifTrue: [ ^ aBlock value ] + ifFalse: [ + ^ removeBucketEntry: aKey hash: hash idx: i head: current ifAbsent: aBlock ] + ) + + private removeBucketEntry: key hash: hash idx: i head: head ifAbsent: aBlock = ( + | current prev | + current:: head. + + [true] whileTrue: [ + (current match: hash key: key) ifTrue: [ + prev isNil + ifTrue: [ buckets at: i put: current next ] + ifFalse: [ prev next: current next ]. + size_:: size_ - 1. + ^ current value ]. + + current next isNil ifTrue: [ + ^ aBlock value ]. + + prev:: current. + current:: current next ] + ) + private resize = ( | oldStorage | oldStorage:: buckets. diff --git a/core-lib/Kernel.ns b/core-lib/Kernel.ns index 4b730bb02..31db3201c 100644 --- a/core-lib/Kernel.ns +++ b/core-lib/Kernel.ns @@ -512,6 +512,38 @@ class Kernel vmMirror: vmMirror = Object <: Value ( self doIndexes: [ :i | result at: i put: (aBlock value: (self at: i)) ]. ^ result ) + + public prependedWith: val = ( + | result | + result:: Array new: self size + 1. + self doIndexes: [:i | + result at: i + 1 put: (self at: i)]. + result at: 1 put: val. + ^ result + ) + + public extendedWith: val = ( + | result | + result:: Array new: self size + 1. + self doIndexes: [:i | + result at: i put: (self at: i)]. + result at: result size put: val. + ^ result + ) + + public join: joiner = ( + | result first | + first:: true. + + self do: [:e | + first + ifTrue: [ + result:: e. + first:: false ] + ifFalse: [ + result:: result + joiner + e ] ]. + ^ result + ) ) public class Array = Object <: ArrayReadMixin () diff --git a/core-lib/TestSuite/CollectionTests.ns b/core-lib/TestSuite/CollectionTests.ns index f8353c92f..606c15138 100644 --- a/core-lib/TestSuite/CollectionTests.ns +++ b/core-lib/TestSuite/CollectionTests.ns @@ -305,6 +305,65 @@ class CollectionTests usingPlatform: platform testFramework: minitest = ( self assert: (c contains: #b). self assert: (c contains: #c). ) + + public testPrependedWith = ( + | arr result | + arr:: Array new: 0. + result:: arr prependedWith: 0. + + assert: 1 equals: result size. + assert: 0 equals: (result at: 1). + + arr:: Array with: 1 with: 2. + result:: arr prependedWith: 0. + + assert: 3 equals: result size. + assert: 0 equals: (result at: 1). + assert: 1 equals: (result at: 2). + assert: 2 equals: (result at: 3). + ) + + public testExtendedWith = ( + | arr result | + arr:: Array new: 0. + result:: arr extendedWith: 0. + + assert: 1 equals: result size. + assert: 0 equals: (result at: 1). + + arr:: Array with: 1 with: 2. + result:: arr extendedWith: 3. + + assert: 3 equals: result size. + assert: 1 equals: (result at: 1). + assert: 2 equals: (result at: 2). + assert: 3 equals: (result at: 3). + ) + + public testAsArray = ( + | set result | + set:: Set new. + 10 to: 110 do: [:i | + set add: i]. + + result:: set asArray. + self assert: 101 equals: set size. + + 10 to: 110 do: [:i | + self assert: i equals: (result at: i - 9) ] + ) + + public testJoin = ( + | arr | + arr:: Array new: 0. + self assert: nil is: (arr join: ', '). + + arr:: Array with: 1 with: 10 with: 100. + self assert: 1 + 20000 + 10 + 20000 + 100 equals: (arr join: 20000). + + arr:: Array with: 'a' with: 'b' with: 'c'. + self assert: 'a, b, c' equals: (arr join: ', '). + ) ) : ( TEST_CONTEXT = () ) public class VectorTest = TestContext ( @@ -429,6 +488,31 @@ class CollectionTests usingPlatform: platform testFramework: minitest = ( self assert: (dict values contains: 55). ) + public testRemoveOneByOne = ( + | dict result | + dict:: Dictionary new. + + self assert: dict size equals: 0 description: 'Empty after creation'. + + dict at: #ee put: 5. + dict at: #e1 put: 66. + dict at: #e2 put: 444. + + self assert: dict size equals: 3 description: 'After inserting 3 elements'. + + self assert: 66 equals: (dict remove: #e1). + self assert: dict size equals: 2. + self assert: nil is: (dict remove: #e1). + + self assert: 5 equals: (dict remove: #ee). + self assert: nil is: (dict remove: #ee). + self assert: dict size equals: 1. + + self assert: 444 equals: (dict remove: #e2). + self assert: nil is: (dict remove: #e2). + self assert: dict size equals: 0. + ) + public testRemoveAll = ( | dict | dict:: Dictionary new. diff --git a/src/som/compiler/Parser.java b/src/som/compiler/Parser.java index 960b3c2f9..bc61cf258 100644 --- a/src/som/compiler/Parser.java +++ b/src/som/compiler/Parser.java @@ -1402,7 +1402,15 @@ private ExpressionNode binaryOperand(final MethodBuilder builder) evenutalSend = accept(EventualSend, KeywordTag.class); } - assert !evenutalSend : "eventualSend should not be true, because that means we steal it from the next operation (think here shouldn't be one, but still...)"; + if (evenutalSend) { + // eventualSend should not be true, because that means we steal it from the next + // operation (think here shouldn't be one, but still...) + throw new ParseError( + "Parsing something unexpectedly as eventual send. " + + "Perhaps some variable declaration statement is not terminated?", + Symbol.NONE, this); + } + return operand; } diff --git a/tests/dym/expected-results.tar.bz2 b/tests/dym/expected-results.tar.bz2 index 77af9dd07..b1d46bde7 100644 Binary files a/tests/dym/expected-results.tar.bz2 and b/tests/dym/expected-results.tar.bz2 differ