Implement Phase 2: Contracts, Ownership, Spawn, Assert, and Test Blocks#7
Merged
Implement Phase 2: Contracts, Ownership, Spawn, Assert, and Test Blocks#7
Conversation
…ts, testing
Add parser support for all five Phase 2 sub-phases:
- 2.1 Ownership: own/shared/sync qualifiers on variable declarations with
OwnershipQualifier enum and move tracking on VariableDefinition
- 2.2 Concurrency: spawn { } blocks parsed into SpawnStatement AST nodes;
chan keyword added to lexer for future channel type support
- 2.3 Async/Await: async fn declarations with mIsAsync flag, await prefix
expressions as AwaitExpression nodes, on expr { } event handlers
- 2.4 Contracts: requires/ensures clauses parsed after function return types,
stored as clause text on FunctionDefinition
- 2.5 Testing: test "name" { } blocks parsed into TestBlock at module level,
assert expr; statements with optional failure messages
12 new lexer keywords, 5 new AST node classes, 4 new parser source files,
25 new test files (19 pass + 6 fail). All 108 tests pass (81 pass + 27 fail).
Existing test functions named 'test' renamed to 'run_test' since test is
now a reserved keyword.
https://claude.ai/code/session_01JWnUURggw1VQCApVkghxLV
…, test blocks
Add LLVM IR generation for all Phase 2 language features:
- Assert statements with optional messages (conditional branch to exit(1))
- Contract clauses (requires/ensures) as runtime checks with expression ASTs
- Spawn blocks (sequential stub - inline body execution)
- Async/await (synchronous stubs - direct evaluation)
- Event handlers (sequential stub - inline body execution)
- Test blocks (__blang_test_* functions + __blang_run_tests runner)
Fix critical lexer enum collision: Phase 2 keyword enum values (e.g.
KEYWORD_ENSURES=45) collided with ASCII operator characters ('-'=45),
causing the expression parser to misparse contract clauses. Fixed by
starting Phase 2 keywords at enum value 256.
Change contract clause storage from strings to Expression ASTs in Type.h,
enabling proper codegen evaluation of requires/ensures conditions.
All 108 parse tests pass, all E2E codegen tests pass (assert, contracts,
spawn, async, phase2 comprehensive + existing Phase 1 tests).
https://claude.ai/code/session_01JWnUURggw1VQCApVkghxLV
Create runtime library (runtime/blang_runtime.{h,c}) providing:
- ARC (automatic reference counting): alloc, retain, release for
shared/sync heap-allocated variables
- Thread pool: init, spawn, shutdown for green thread execution
- Channels: create, send, recv, close, destroy for inter-thread comms
- Async tasks: async_call, await, task_destroy for async/await support
All backed by pthreads with proper synchronization.
Update CodeGen for ownership qualifiers:
- shared variables: heap-allocated via __blang_rc_alloc, loads/stores
go through the heap pointer indirection
- sync variables: heap-allocated via __blang_rc_alloc_sync, assignments
wrapped in __blang_sync_lock/__blang_sync_unlock calls
- own variables: stack-allocated with move semantics tracking
Add for-in range loop codegen: for x in start..end generates proper
loop with counter variable, condition check, and increment.
Update build system: CMakeLists.txt builds libblang_runtime.a static
library and PIC object file for linking into BLang programs.
Update test_codegen.sh: links runtime library when available, uses
set +e for binary execution to handle non-zero exit codes gracefully.
New E2E tests: codegen_ownership.b, codegen_forin.b,
codegen_comprehensive.b exercising all Phase 2 features together.
All 108 parse tests pass, all 11 E2E codegen tests pass.
https://claude.ai/code/session_01JWnUURggw1VQCApVkghxLV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements Phase 2 of the BLang compiler, adding support for function contracts (requires/ensures), ownership qualifiers (own/shared/sync), spawn statements, assert statements, test blocks, and async/await stubs. It includes a complete runtime library for reference counting and thread pool management.
Key Changes
Language Features
requires(preconditions) andensures(postconditions) clauses with compile-time parsing and runtime checkingown,shared, andsyncqualifiers for variables with distinct memory management strategiesspawn { }blocks for concurrent task submission (currently sequential stubs)assert expr;andassert expr, "message";with optional failure messagestest "name" { }syntax for unit tests with automatic test runner generationasync fndeclarations andawait exprexpressions (synchronous stubs for now)for i in 0..Nsyntaxon expr { }event handler syntaxCode Generation
__blang_rc_allocand__blang_rc_alloc_sync, with automatic retain/releaseresultvariable accessible in postconditionsRuntime Library
blang_runtime.c): Reference counting with atomic operations, mutex support for sync objectsParser & Lexer
own,shared,sync,spawn,assert,test,requires,ensures,async,await,on,chan,in,break,continueTesting
Notable Implementation Details
ownuses stack allocation with move semantics (stub),sharedandsyncuse heap allocation with ARCresultvariable for return value validationBlangRefHeadercontaining ref count, sync flag, and mutex pointerhttps://claude.ai/code/session_01JWnUURggw1VQCApVkghxLV