MCPcopy Index your code
hub / github.com/RustPython/RustPython / compile_expr_tstring

Method compile_expr_tstring

crates/codegen/src/compile.rs:10269–10326  ·  view source on GitHub ↗
(&mut self, expr_tstring: &ast::ExprTString)

Source from the content-addressed store, hash-verified

10267 }
10268
10269 fn compile_expr_tstring(&mut self, expr_tstring: &ast::ExprTString) -> CompileResult<()> {
10270 // ast::TStringValue can contain multiple ast::TString parts (implicit concatenation)
10271 // Each ast::TString part should be compiled and the results merged into a single Template
10272 let tstring_value = &expr_tstring.value;
10273
10274 // Collect all strings and compile all interpolations
10275 let mut all_strings: Vec<Wtf8Buf> = Vec::new();
10276 let mut current_string = Wtf8Buf::new();
10277 let mut interp_count: u32 = 0;
10278
10279 for tstring in tstring_value.iter() {
10280 self.compile_tstring_into(
10281 tstring,
10282 &mut all_strings,
10283 &mut current_string,
10284 &mut interp_count,
10285 )?;
10286 }
10287
10288 // Add trailing string
10289 all_strings.push(core::mem::take(&mut current_string));
10290
10291 // Now build the Template:
10292 // Stack currently has all interpolations from compile_tstring_into calls
10293
10294 // 1. Build interpolations tuple from the interpolations on the stack
10295 emit!(
10296 self,
10297 Instruction::BuildTuple {
10298 count: interp_count
10299 }
10300 );
10301
10302 // 2. Load all string parts
10303 let string_count: u32 = all_strings
10304 .len()
10305 .try_into()
10306 .expect("t-string string count overflowed");
10307 for s in &all_strings {
10308 self.emit_load_const(ConstantData::Str { value: s.clone() });
10309 }
10310
10311 // 3. Build strings tuple
10312 emit!(
10313 self,
10314 Instruction::BuildTuple {
10315 count: string_count
10316 }
10317 );
10318
10319 // 4. Swap so strings is below interpolations: [interps, strings] -> [strings, interps]
10320 emit!(self, Instruction::Swap { i: 2 });
10321
10322 // 5. Build the Template
10323 emit!(self, Instruction::BuildTemplate);
10324
10325 Ok(())
10326 }

Callers 1

compile_expressionMethod · 0.80

Calls 8

newFunction · 0.85
takeFunction · 0.85
compile_tstring_intoMethod · 0.80
emit_load_constMethod · 0.80
iterMethod · 0.45
pushMethod · 0.45
lenMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected