Skip to content

Fix C codegen for interpolated strings (f-strings)#19

Merged
dev-dami merged 1 commit intomainfrom
fix/codegen-interpolated-strings-v2
Jan 20, 2026
Merged

Fix C codegen for interpolated strings (f-strings)#19
dev-dami merged 1 commit intomainfrom
fix/codegen-interpolated-strings-v2

Conversation

@dev-dami
Copy link
Owner

@dev-dami dev-dami commented Jan 20, 2026

Summary

Fixes string interpolation (f"...") not compiling to valid C code.

Problem

Previously, code like:

let name = "World";
println(f"Hello, {name}!");

Would generate invalid C:

(printf("%lld", ), printf("\n"));  // Empty argument!

Solution

Added proper code generation for InterpolatedString AST nodes:

  1. New runtime helpers:

    • carv_int_to_string() - converts int to string
    • carv_float_to_string() - converts float to string
    • carv_bool_to_string() - converts bool to string
    • carv_concat() - concatenates two strings
  2. New codegen functions:

    • generateInterpolatedString() - generates C code for f-strings
    • convertToString() - converts any expression to string based on type
  3. Type inference: Added InterpolatedString case to inferExprType returning carv_string

Testing

# Compiled mode now works correctly
$ cat test.carv
let name = "World";
let num = 42;
println(f"Hello, {name}! The answer is {num}.");
println(f"2 + 2 = {2 + 2}");

$ ./build/carv build test.carv && ./test
Hello, World! The answer is 42.
2 + 2 = 4

Replaces #18 (which had merge conflicts due to being branched from wrong base).

Summary by CodeRabbit

Release Notes

  • New Features
    • String interpolation is now supported, allowing variables and expressions to be embedded within string literals
    • New conversion helpers enable automatic transformation of numeric and boolean types to strings
    • Result constructor API expanded with string-value support and error code handling for enhanced error reporting

✏️ Tip: You can customize this high-level summary in your review settings.

Add support for generating C code from interpolated strings:
- Add carv_int_to_string, carv_float_to_string, carv_bool_to_string helpers
- Add carv_concat for string concatenation
- Add generateInterpolatedString to properly convert f-strings to C
- Add InterpolatedString case to inferExprType

Previously, f-strings like `println(f"x = {x}")` would generate
invalid C code with empty printf arguments. Now they correctly
generate string concatenation code.
@coderabbitai
Copy link

coderabbitai bot commented Jan 20, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

This pull request adds support for interpolated strings in the C code generator by implementing string interpolation handling, introducing new runtime string conversion helpers (int_to_string, float_to_string, bool_to_string, concat), and exposing additional result constructors (ok_str, err_str, err_code) in the generated C runtime.

Changes

Cohort / File(s) Summary
String Interpolation Support
pkg/codegen/cgen.go
Added generateInterpolatedString() and convertToString() methods to handle AST InterpolatedString nodes; extended generateExpression() dispatch and inferExprType() to recognize InterpolatedString as carv_string type.
Runtime Helper Functions
pkg/codegen/cgen.go
Emitted four new C runtime string conversion functions: carv_int_to_string, carv_float_to_string, carv_bool_to_string, and carv_concat for string composition.
Result Constructor Expansion
pkg/codegen/cgen.go
Exposed three new result constructors in generated C runtime: carv_ok_str, carv_err_str, and carv_err_code alongside existing carv_ok_int.

Sequence Diagram(s)

sequenceDiagram
    participant Parser as AST Parser
    participant Gen as CGenerator
    participant Conv as convertToString
    participant Runtime as Generated C Runtime
    
    Parser->>Gen: InterpolatedString node
    Gen->>Gen: generateExpression() dispatches
    Gen->>Gen: generateInterpolatedString() called
    Gen->>Conv: convertToString(expr) for each part
    Conv->>Runtime: carv_int_to_string / carv_float_to_string
    Conv->>Runtime: carv_bool_to_string (as needed)
    Conv->>Runtime: carv_concat for composition
    Runtime-->>Gen: Concatenated string result
    Gen-->>Parser: Generated C code
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • feat: add C code generator #3: Directly related as the base PR that introduced the CGenerator and runtime emission infrastructure that this PR extends with interpolated string support and new runtime functions.

Poem

🐰 A rabbit hops through strings so fine,
With int and float in each design,
We weave them all with carv\_concat's might,
Interpolated whispers, oh what delight!
From AST to C, a merry dance,
The code now strings together in romance! 🌟

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dev-dami dev-dami merged commit 5f61ee0 into main Jan 20, 2026
4 of 5 checks passed
@dev-dami dev-dami deleted the fix/codegen-interpolated-strings-v2 branch January 20, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant