Files
hakorune/tools/hako_check/rules
nyash-codex ace741b755 Implement HC022: Stage-3 Gate detection (while/for constructs)
## Overview
Detects while/for loop constructs that require Stage-3 parser support. These constructs may not work with standard VM without proper environment flags.

## Implementation Details
- **Rule**: `rule_stage3_gate.hako` following box principles
- **Detection Method**: Text-based scanning for while/for keywords
  - Checks for `while ` or `while(` patterns
  - Checks for `for ` or `for(` patterns
  - Skips comments (lines starting with `//`)
  - Reports first occurrence of each construct type
- **Integration**: Added to cli.hako in text-based rules section

## Technical Approach
- **Keyword Detection**: Simple indexOf() checks for while/for keywords
- **One-per-type Reporting**: Reports only first `while` and first `for` occurrence
- **Suggestion Message**: Provides NYASH_PARSER_STAGE3=1 environment variable guidance

## Helper Methods
- `_trim()`: Whitespace trimming
- `_is_comment()`: Comment line detection
- `_split_lines()`: Line-by-line text processing
- `_itoa()`: Integer to string conversion

## Test Cases
- **ok.hako**: Uses only `loop()` construct (no while/for) → no warnings
- **ng.hako**: Contains while/for constructs
  - Line 7: `while (i < n)` → HC022 warning
  - Line 14: `for (local item in arr)` → HC022 warning
  - Includes suggestion message about Stage-3 environment variables

## Test Results
```
[TEST/OK] HC011_dead_methods
[TEST/OK] HC012_dead_static_box
[TEST/OK] HC013_duplicate_method
[TEST/OK] HC014_missing_entrypoint
[TEST/OK] HC015_arity_mismatch
[TEST/OK] HC016_unused_alias
[TEST/OK] HC017_non_ascii_quotes
[TEST/OK] HC018_top_level_local
[TEST/OK] HC022_stage3_gate ← NEW
[TEST/SUMMARY] all green
```

## Diagnostic Format
```
[HC022] Stage-3 construct detected (while): <path>:<line>
[HC022] Stage-3 construct detected (for): <path>:<line>
[HC022] Suggestion: Use NYASH_PARSER_STAGE3=1 or HAKO_PARSER_STAGE3=1 environment variables
```

## Architecture
- Box-first design: RuleStage3GateBox with single responsibility
- Text-based analysis: No AST/IR dependencies
- Clean separation: detection, reporting, suggestion

## Notes
- Hakorune standard syntax uses `loop()` instead of `while`/`for`
- This rule detects legacy constructs or experimental Stage-3 features
- Helps users identify code requiring special parser flags

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-08 03:23:27 +09:00
..