Fix C codegen for interpolated strings (f-strings)#19
Conversation
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.
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughThis 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
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
✨ Finishing touches
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. Comment |
Summary
Fixes string interpolation (
f"...") not compiling to valid C code.Problem
Previously, code like:
Would generate invalid C:
Solution
Added proper code generation for
InterpolatedStringAST nodes:New runtime helpers:
carv_int_to_string()- converts int to stringcarv_float_to_string()- converts float to stringcarv_bool_to_string()- converts bool to stringcarv_concat()- concatenates two stringsNew codegen functions:
generateInterpolatedString()- generates C code for f-stringsconvertToString()- converts any expression to string based on typeType inference: Added
InterpolatedStringcase toinferExprTypereturningcarv_stringTesting
Replaces #18 (which had merge conflicts due to being branched from wrong base).
Summary by CodeRabbit
Release Notes
✏️ Tip: You can customize this high-level summary in your review settings.