Skip to content

Commit

Permalink
Merge PR #347: Turn parser assertion into error, and add core-lib ext…
Browse files Browse the repository at this point in the history
…ensions
  • Loading branch information
smarr authored Apr 11, 2020
2 parents c3a2773 + 4968528 commit df7c4b6
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 1 deletion.
49 changes: 49 additions & 0 deletions core-lib/Collections.ns
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ class Collections usingKernel: kernel = Value (
public removeAll = (
items removeAll
)

public asArray = (
^ items asArray
)
) : (
public new = ( ^ self new: initialSize )
)
Expand All @@ -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 )
)
Expand Down Expand Up @@ -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.
Expand Down
32 changes: 32 additions & 0 deletions core-lib/Kernel.ns
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
84 changes: 84 additions & 0 deletions core-lib/TestSuite/CollectionTests.ns
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion src/som/compiler/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Binary file modified tests/dym/expected-results.tar.bz2
Binary file not shown.

0 comments on commit df7c4b6

Please sign in to comment.