Skip to content

feat: add yield statement and expression support#39

Open
dfl wants to merge 5 commits intotype-ruby:mainfrom
dfl:feat/yield-support
Open

feat: add yield statement and expression support#39
dfl wants to merge 5 commits intotype-ruby:mainfrom
dfl:feat/yield-support

Conversation

@dfl
Copy link

@dfl dfl commented Jan 17, 2026

Summary

  • Add yield statement support for calling blocks
  • Support yield as expression for assignment context
  • Add yield argument type checking (not just count)

Test plan

  • Statement parser tests for yield syntax
  • Expression parser tests for yield in assignment
  • Compiler tests for yield code generation
  • Type checking tests for yield argument validation

🤖 Generated with Claude Code

dfl and others added 3 commits January 15, 2026 15:54
- Add IR::Yield node class with arguments attribute
- Implement parse_yield in StatementParser
- Add visit_yield in CodeGenerator for Ruby output
- Support yield with/without arguments, modifiers

Closes type-ruby#34

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add yield parsing in ExpressionParser for `result = yield(x)` pattern
- Support parenthesized arguments in expression context
- Add expression parser and E2E tests

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Extend check_yield_arguments to verify types match block signature
- Infer yield argument types using ASTTypeInferrer
- Create method type environment with parameter types
- Report type mismatch errors with expected vs actual types

Example error:
  def each(&block: Proc(Integer) -> void)
    yield "hello"  # Error: expected 'Integer' but got 'String'

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@yhk1038 yhk1038 added this to the v0.0.5 milestone Jan 19, 2026
@yhk1038 yhk1038 moved this from Backlog to In review in T-Ruby Roadmap Jan 19, 2026
@yhk1038
Copy link
Contributor

yhk1038 commented Jan 19, 2026

@dfl Thank you for your pull request!
This is a very important feature for covering Ruby. It's a significant and impressive attempt.

Before merging, I’d like to discuss a few things regarding this great new implementation.

Let’s continue our conversation in the Discussions section while keeping this PR thread as is.

Discussion #41 on going.

@yhk1038
Copy link
Contributor

yhk1038 commented Jan 19, 2026

@dfl I just enabled CI workflow. Please check CI broken.

- Add check_yield_arguments calls in check_types method
- Add parse_block_parameter method for &block: Proc(T) -> R syntax
- Add optional attribute to IR::Parameter for &block? syntax
- Add callable_kind attribute to IR::FunctionType for Proc/Lambda
- Add Proc(T) -> R and Lambda(T) -> R type syntax parsing
- Fix RuboCop empty line issue
@yhk1038
Copy link
Contributor

yhk1038 commented Jan 22, 2026

Test Failure Fix Applied

I've pushed a fix for the failing tests to the feat/yield-support branch on the main repo.

Changes:

  1. compiler.rb: Added check_yield_arguments calls in check_types method
  2. parser.rb: Added parse_block_parameter method for &block: Proc(T) -> R syntax
  3. ir.rb:
    • Added optional attribute to Parameter for &block? syntax
    • Added callable_kind attribute to FunctionType for Proc/Lambda distinction
    • Updated build_method to handle kind, optional, and ir_type
  4. type_parser.rb: Added Proc(T) -> R and Lambda(T) -> R type syntax parsing
  5. Fixed RuboCop empty line issue

Test Results:

222 examples, 0 failures

@dfl Please cherry-pick commit 8e038f6 or merge from origin/feat/yield-support to include this fix.

# Conflicts:
#	Gemfile.lock
#	lib/t_ruby/ir.rb
#	lib/t_ruby/parser_combinator/type_parser.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

2 participants