feat: Implement PriorityQueue<T> collection#133
Conversation
Add priority queue collection type with the following features: - Max heap by default: PriorityQueue<T>() - Min heap via static method: PriorityQueue<T>.min() - Custom comparison via less_than method on structs - Methods: push, pop, top, length, is_empty Implementation includes: - Lexer: PRIORITY_QUEUE token - Parser: PriorityQueueCreate AST node with .min() support - Typechecker: priority_queues.hpp/cpp and check_priority_queue.cpp - Codegen: emit_priority_queue.cpp - Runtime: bishop::MaxPriorityQueue and bishop::MinPriorityQueue templates - Tests: test_priority_queue.b with max/min heap and custom comparator tests - Documentation: README.md Language Reference updated Closes #39 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The test file used incorrect Rust-like syntax (impl block, colon in
params, different struct syntax). Fixed to use proper Bishop syntax:
- struct Task { name: str } -> Task :: struct { name str, }
- impl Task { fn less_than... } -> Task :: less_than(self, Task other)
Also updated README example to be consistent.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Bishop methods don't have const qualifiers, so the comparators need to use const_cast when calling less_than. This is safe because less_than is a pure comparison that doesn't mutate the object. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
REVIEW
Summary
Well-implemented PriorityQueue collection with solid max/min heap support and custom comparison via less_than method.
Critical Issues
None found.
Suggestions (Non-blocking)
-
Empty queue access behavior - The
pop()andtop()methods inruntime/collections/priority_queue.hppdon't check for empty state before accessing elements. While this is consistent withstd::priority_queuebehavior (undefined behavior on empty queue), it might be worth documenting this clearly or considering apop_or_none()method for safer access patterns in future versions. -
const_cast usage - The comparators use
const_castonless_than()calls. The comment explains this is because Bishop methods don't have const qualifiers yet. This is acceptable as a workaround but worth tracking for when const methods are added to the language.
What Was Reviewed
Parser/Lexer:
- ✅
PRIORITY_QUEUEtoken added to lexer - ✅
PriorityQueueCreateAST node with line number tracking - ✅ Parse handles both
PriorityQueue<T>()andPriorityQueue<T>.min()syntax - ✅ Proper error message for unsupported static methods
Type Checker:
- ✅
check_priority_queue.cppvalidates element types - ✅
priority_queues.cppdefines all method signatures (push, pop, top, length, is_empty) - ✅ Type substitution for
Tplaceholder works correctly - ✅ Proper error reporting for unknown methods and type mismatches
Code Generation:
- ✅
emit_priority_queue.cppemits correct C++ for all methods - ✅
emit_type.cppmapsPriorityQueue<T>tobishop::PriorityQueueBase<T> - ✅ Method call routing in
emit_method_call.cpp
Runtime:
- ✅ SFINAE-based
has_less_thandetection for custom comparators - ✅ Separate comparators for max/min heaps with both primitives and custom types
- ✅ Clean template metaprogramming using
std::conditional_t - ✅ Proper docstrings on all methods and classes
Documentation:
- ✅ README.md updated with comprehensive PriorityQueue section
- ✅ Examples for max heap, min heap, and custom structs with
less_than - ✅ Method reference table included
Testing:
- ✅ Tests cover max heap with int
- ✅ Tests cover min heap with int
- ✅ Tests cover length/is_empty
- ✅ Tests cover custom struct with
less_than - ✅ Tests cover float values
- ✅ Tests verify
top()doesn't remove elements
Security Check (OWASP)
No security concerns - this is a pure data structure implementation with no external input handling, network access, or file I/O.
Decision: APPROVE ✅
The implementation is correct, well-documented, and follows the codebase patterns. The test coverage is comprehensive. Good work on the SFINAE-based comparator selection!
There was a problem hiding this comment.
REVIEW
Summary: Clean implementation of PriorityQueue with max/min heap support and custom comparison via less_than method.
Security Review (OWASP Top 10)
- ✅ No injection vulnerabilities
- ✅ No authentication/authorization concerns
- ✅ No sensitive data handling
- ✅ No external input parsing that could be exploited
- ✅ No SSRF or deserialization risks
Code Quality Assessment
Strengths:
- Well-structured implementation following existing patterns for collections (List, Set, Map)
- Proper separation: lexer → parser → typechecker → codegen → runtime
- Good use of C++17 features (
std::conditional_t,std::void_tfor SFINAE) - Comprehensive test coverage including max heap, min heap, custom structs, and floats
- Clear documentation in README matching the API
- AST node properly sets line number
Implementation Details:
PriorityQueue<T>()creates max heap (default)PriorityQueue<T>.min()creates min heap- Custom comparison via
less_thanmethod detected at compile-time using SFINAE - Correct heap semantics: max heap uses
<, min heap uses>(inverted for std::priority_queue)
Suggestions (Non-blocking)
-
Empty queue behavior:
pop()andtop()on empty queues will cause undefined behavior (same as C++std::priority_queue). This is consistent with Bishop's List.pop() behavior, so it's a design choice, not a bug. Consider documenting this in README. -
Rebase needed: Branch is behind master by 2 commits (including PR #130 which changed error handling syntax). You'll need to rebase before merging to resolve conflicts.
-
const_cast usage: The
const_castin comparators is necessary because Bishop methods aren't const-qualified. The comment explaining this is helpful. Long-term, consider adding const method support to Bishop.
Test Coverage
- ✅ Max heap with integers
- ✅ Min heap with integers
- ✅ Empty/length checks
- ✅ Custom struct with less_than method
- ✅ Float values
- ✅ top() without removal
Decision: APPROVE ✅
The implementation is correct, follows existing patterns, and has good test coverage. The code is production-quality.
@chrishayen - Please review when you have a chance. Note: rebase onto master needed before merge.
There was a problem hiding this comment.
REVIEW
summary: Well-implemented PriorityQueue collection with clean design and good test coverage.
Critical Issues
None found.
Security Review (OWASP Top 10)
- A01 Broken Access Control: N/A - no authorization in language runtime
- A02 Cryptographic Failures: N/A - no crypto operations
- A03 Injection: N/A - no user input processing
- A04 Insecure Design: N/A - standalone collection type
- A05 Security Misconfiguration: N/A
- A06 Vulnerable Components: Uses
std::priority_queue- standard library, no CVEs - A07-A10: N/A for this change
No security concerns identified.
Code Quality Review
Runtime Implementation (priority_queue.hpp)
- Clean separation of
MaxPriorityQueueandMinPriorityQueueusing template metaprogramming - Proper use of SFINAE (
has_less_than_v) to detect customless_thanmethods - The
const_castin comparators (lines 107, 132) is documented and justified - Bishop methods currently lack const qualifiers - Well-documented with docstrings per CLAUDE.md requirements
Parser (parse_primary.cpp, parse_type.cpp)
- Correct handling of
PriorityQueue<T>()andPriorityQueue<T>.min()syntax - Line numbers properly set on AST nodes
- Good error message for invalid constructor:
"PriorityQueue<T> only supports .min() constructor"
Typechecker
priority_queues.cpp: Clean method signatures with proper T placeholder substitutioncheck_priority_queue.cpp: Proper type validation and argument checking- Consistent with existing collection patterns (List, Tuple)
Codegen
emit_priority_queue.cpp: Clean mapping of Bishop methods to C++ equivalentsemit_type.cpp: UsesPriorityQueueBase<T>for type declarations, actual max/min selection happens at creation
Tests (test_priority_queue.b)
- Covers: max heap, min heap, custom struct with
less_than, floats, length/is_empty - Tests verify correct ordering for both heap types
- Tests custom comparison with priority field
Documentation
- README.md updated with comprehensive examples for max heap, min heap, and custom comparison
- Keywords section updated to include
PriorityQueue
Suggestions (Non-blocking)
-
Empty queue behavior (line 55-58 of
priority_queue.hpp):pop()andtop()on empty queue will cause undefined behavior (underlyingstd::priority_queue::top()on empty is UB). This matches existing Bishop patterns (e.g.,List.first()on empty list) and is consistent with the language design - users should checkis_empty()first. Consider documenting this expectation in README. -
Test coverage: Consider adding a test for calling
top()multiple times to verify it doesn't mutate state (currently teststop()once beforepop()). ✅ Actually,test_top_no_remove()already covers this - good job!
Decision: APPROVE
The implementation is correct, well-tested, follows existing patterns, and has no security concerns. The code is clean, well-documented, and meets the issue requirements. Good work! 🎉
🤖 Reviewed with Claude Code
Resolves merge conflicts between PriorityQueue<T> implementation and Set<T> implementation. Both collections are now included: - PriorityQueue<T>: Max/min heaps with push, pop, top, length, is_empty - Set<T>: Unique element storage with add, remove, contains, and set operations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Merge conflicts have been resolved! ✅ I merged
The PR is ready for merge. |
Resolved merge conflict in CMakeLists.txt by including both: - typechecker/check_map.cpp (from master) - typechecker/check_priority_queue.cpp and priority_queues.cpp (from this PR) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Merge conflicts have been resolved. The conflict in The PR should now be mergeable. |
Resolve merge conflicts between Deque/Stack/Queue PR and PriorityQueue PR. Both collection types are now included: - Deque<T>, Stack<T>, Queue<T> (from this PR) - PriorityQueue<T> (from master/PR #133) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Summary
PriorityQueue<T>collection type with max heap (default) and min heap supportPriorityQueue<T>()- highest value firstPriorityQueue<T>.min()- lowest value firstless_thanmethod on structspush,pop,top,length,is_emptyTest plan
Files changed
Closes #39
🤖 Generated with Claude Code