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

Method match_stmt

compiler/src/modules/parser/control.rs:53–89  ·  view source on GitHub ↗

match/case: literals, captures, wildcards, OR, guards, sequences; emits subject-load + pattern + guard + Jump-end. */

(&mut self)

Source from the content-addressed store, hash-verified

51
52 /* match/case: literals, captures, wildcards, OR, guards, sequences; emits subject-load + pattern + guard + Jump-end. */
53 pub(super) fn match_stmt(&mut self) {
54 self.advance();
55 self.expr();
56
57 let ver = self.increment_version(super::SSA_TMP_MATCH);
58 let subj = self.chunk.push_name(&s!(str super::SSA_TMP_MATCH, int ver));
59 self.chunk.emit(OpCode::StoreName, subj);
60
61 self.eat(TokenType::Colon);
62 self.eat_if(TokenType::Indent);
63
64 let mut end_jumps = Vec::new();
65
66 while matches!(self.peek(), Some(TokenType::Case)) {
67 self.advance();
68
69 let mut fail_jumps: Vec<usize> = Vec::new();
70 self.parse_pattern(subj, &mut fail_jumps);
71
72 // Guard fail joins pattern fails; both land at the next case.
73 if self.eat_if(TokenType::If) {
74 self.expr();
75 fail_jumps.push(self.emit_jump(OpCode::JumpIfFalse));
76 }
77
78 self.eat(TokenType::Colon);
79 self.compile_block();
80
81 end_jumps.push(self.emit_jump(OpCode::Jump));
82
83 for j in fail_jumps { self.patch(j); }
84 }
85
86 self.eat_if(TokenType::Dedent);
87
88 for pos in end_jumps { self.patch(pos); }
89 }
90
91 /* Emits bytecode for one pattern; appends case-fail jumps to `fail_jumps`; reloads subject from subj. */
92 pub(super) fn parse_pattern(&mut self, subj: u16, fail_jumps: &mut Vec<usize>) {

Callers 1

stmtMethod · 0.80

Calls 12

advanceMethod · 0.80
exprMethod · 0.80
increment_versionMethod · 0.80
push_nameMethod · 0.80
emitMethod · 0.80
eatMethod · 0.80
eat_ifMethod · 0.80
parse_patternMethod · 0.80
pushMethod · 0.80
emit_jumpMethod · 0.80
compile_blockMethod · 0.80
patchMethod · 0.80

Tested by

no test coverage detected