SuperForth crashes when trying to generate a type-signature while compiling top-level code(code that isn't encapsulated in a procedure).
The following code was used:
hashmap.txt
:
abstract record hash_bucket<K, V>
final record empty_bucket<K, V> extends hash_bucket<K, V>
final record full_bucket<K, V> extends hash_bucket<K, V> {
mustinit K key;
mustinit V value;
}
final record hash_map<K, V> {
mustinit array<hash_bucket<K, V>> buckets = [new empty_bucket<K, V>, new empty_bucket<K, V>, new empty_bucket<K, V>, new empty_bucket<K, V>, new empty_bucket<K, V>,
new empty_bucket<K, V>, new empty_bucket<K, V>, new empty_bucket<K, V>, new empty_bucket<K, V>, new empty_bucket<K, V>];
mustinit proc<int, K> hasher;
}
global readonly auto hash_map_emplace = proc<K, V>(hash_map<K, V> map, K key, V value) return nothing {
readonly auto rehash = proc<K, V>(hash_map<K, V> map, int new_size) return auto {
array<hash_bucket<K, V>> old_buckets = map.buckets;
map.buckets = new hash_bucket<K, V>[new_size];
int i = 0;
while(i < new_size) {
map.buckets[i] = new empty_bucket<K, V>;
i = i + 1;
}
return old_buckets;
};
int bucket = map.hasher(key) % #map.buckets;
while(bucket < #map.buckets) { $find the first empty bucket after computed hash_location
if(map.buckets[bucket] is empty_bucket<any, any>) {
map.buckets[bucket] = new full_bucket<K, V> {
key = key;
value = value;
};
return;
}
bucket = bucket + 1;
}
$no availible bukets were found if this code is reached
auto reinsert_buckets = rehash<K, V>(map, bucket + 5);
bucket = 0;
while(bucket < #reinsert_buckets) {
thisproc<K, V>(map, key, value);
bucket = bucket + 1;
}
thisproc<K, V>(map, key, value);
return;
};
test.txt
:
include "hashmap.txt";
include "io.sf";
auto mymap = new hash_map<int, array<char>> {
hasher = proc(int i) return int {
return i;
};
};
hash_map_emplace<int, array<char>>(mymap, 5, "Michael");
hash_map_emplace<int, array<char>>(mymap, 2, "Michael");
hash_map_emplace<int, array<char>>(mymap, 7, "Tim");
println("HI!");
It can be reproduced by running:
superforth -cr -s test.txt
.
bug