feat: implement Map.keys, Map.values iterators and proper toString with key-value formatting

Add MapKeySequence and MapValueSequence classes in core.wren to expose iterable key and value views on Map instances. Implement native iterate_, keyIteratorValue_, and valueIteratorValue_ primitives in wren_core.c to support these sequences. Replace the stub Map.toString with a full implementation that iterates keys and formats each entry as "key: value", handling empty maps and nested maps correctly. Include comprehensive test suites for key iteration, value iteration, iterator type validation, and toString output across multiple orderings.
This commit is contained in:
Bob Nystrom
2015-01-25 18:27:38 +00:00
parent 56123c71e3
commit f093d0a42a
9 changed files with 250 additions and 4 deletions
+33 -2
View File
@@ -89,8 +89,39 @@ class List is Sequence {
}
class Map {
// TODO: Implement this.
toString { "{}" }
keys { new MapKeySequence(this) }
values { new MapValueSequence(this) }
toString {
var first = true
var result = "{"
for (key in keys) {
if (!first) result = result + ", "
first = false
result = result + key.toString + ": " + this[key].toString
}
return result + "}"
}
}
class MapKeySequence is Sequence {
new(map) {
_map = map
}
iterate(n) { _map.iterate_(n) }
iteratorValue(iterator) { _map.keyIteratorValue_(iterator) }
}
class MapValueSequence is Sequence {
new(map) {
_map = map
}
iterate(n) { _map.iterate_(n) }
iteratorValue(iterator) { _map.valueIteratorValue_(iterator) }
}
class Range is Sequence {}