MCPcopy Create free account
hub / github.com/dylan-sutton-chavez/edge-python / parse_lambda

Method parse_lambda

compiler/src/modules/parser/expr.rs:387–431  ·  view source on GitHub ↗

lambda: fresh chunk, compiles body to Return, emits MakeFunction. */

(&mut self)

Source from the content-addressed store, hash-verified

385
386 /* lambda: fresh chunk, compiles body to Return, emits MakeFunction. */
387 pub(super) fn parse_lambda(&mut self) {
388 let mut params = Vec::new();
389 let mut defaults = 0u16;
390 if !matches!(self.peek(), Some(TokenType::Colon)) {
391 loop {
392 // Match parse_params prefix detection so *args/**kw names align with ParamKind.
393 let prefix = if self.eat_if(TokenType::DoubleStar) { "**" }
394 else if self.eat_if(TokenType::Star) { "*" }
395 else { "" };
396 let nm = self.advance_text();
397 params.push(if prefix.is_empty() { nm } else { s!(str prefix, str &nm) });
398 if prefix.is_empty() && self.eat_if(TokenType::Equal) {
399 self.expr();
400 defaults += 1;
401 // Trailing `=` marks this param as carrying a default value.
402 if let Some(last) = params.last_mut() { last.push('='); }
403 }
404 if !self.eat_if(TokenType::Comma) {
405 break;
406 }
407 }
408 }
409 self.eat(TokenType::Colon);
410
411 let outer_versions = self.ssa_versions.clone();
412
413 let body = self.with_fresh_chunk(|s| {
414 s.ssa_versions = outer_versions;
415 // Base name shadows the enclosing scope; prefix/`=` marker must be stripped.
416 for p in &params { s.ssa_versions.insert(super::types::param_base_name(p).to_string(), 0); }
417 s.expr();
418 s.chunk.emit(OpCode::ReturnValue, 0);
419 });
420
421 let param_slots: crate::util::fx::FxHashSet<String> = params.iter().map(|p| s!(str super::types::param_base_name(p), "_0")).collect();
422 for name in &body.names {
423 if !param_slots.contains(name.as_str()) {
424 self.chunk.push_name(name);
425 }
426 }
427
428 let fi = self.chunk.functions.len() as u16;
429 self.chunk.functions.push((params, body, defaults, u16::MAX));
430 self.chunk.emit(OpCode::MakeFunction, fi);
431 }
432}

Callers 1

parse_atomMethod · 0.80

Calls 15

param_base_nameFunction · 0.85
eat_ifMethod · 0.80
advance_textMethod · 0.80
pushMethod · 0.80
exprMethod · 0.80
eatMethod · 0.80
with_fresh_chunkMethod · 0.80
insertMethod · 0.80
emitMethod · 0.80
collectMethod · 0.80
push_nameMethod · 0.80
is_emptyMethod · 0.45

Tested by

no test coverage detected