Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9fbd359
initial stub implementation of match statements
arihant2math Jan 19, 2025
bfb4091
formatting
arihant2math Jan 19, 2025
c021326
solved compilation errors
arihant2math Jan 20, 2025
6c7a543
almost working
arihant2math Jan 20, 2025
8617786
formatting
arihant2math Jan 20, 2025
a01240f
as and * support for switch case
arihant2math Jan 20, 2025
d8c4cff
replace todos with errors
arihant2math Jan 20, 2025
aaccadb
fix compile
arihant2math Jan 20, 2025
f9d2418
added Noop instruction
arihant2math Jan 20, 2025
5839dfd
finished default for switch case
arihant2math Jan 20, 2025
40e380e
fix compile
arihant2math Jan 20, 2025
8af2c42
formatting
arihant2math Jan 20, 2025
0774cbd
more implementation
arihant2math Jan 20, 2025
cd0f1cf
rename codegen_* to compile_*
arihant2math Jan 21, 2025
223fbe4
implement SWAP instruction
arihant2math Jan 21, 2025
4ef2a50
applied fix
youknowone Jan 21, 2025
aa6a040
enabled IR debug and attempted to fix codegen
arihant2math Jan 22, 2025
822d565
formatting
arihant2math Jan 22, 2025
ad164c0
basic match statement working
arihant2math Jan 22, 2025
e955ede
basic match statement working
arihant2math Jan 22, 2025
4649898
working as pattern (default works too by extension)
arihant2math Jan 23, 2025
493fc6e
fix rust tests
arihant2math Jan 23, 2025
fe7f8a3
cleanup
arihant2math Jan 23, 2025
805757a
fix clippy errors
arihant2math Jan 23, 2025
b176f9c
formatting
arihant2math Jan 23, 2025
8e78c6d
trivial edits
youknowone Jan 25, 2025
4beef2c
Remove currently unused Nop,Swap op
youknowone Jan 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rename codegen_* to compile_*
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
  • Loading branch information
arihant2math committed Jan 22, 2025
commit cd0f1cf63b99b54fa522c8c449e42ea445c1a09a
48 changes: 33 additions & 15 deletions compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,7 @@ impl Compiler {
// RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
// return SUCCESS;
// }
fn codegen_pattern_value(
fn compile_pattern_value(
&mut self,
value: &PatternMatchValue<SourceRange>,
pattern_context: &mut PatternContext,
Expand All @@ -1886,7 +1886,7 @@ impl Compiler {
// RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
// return SUCCESS;
// }
fn codegen_pattern_singleton(
fn compile_pattern_singleton(
&mut self,
singleton: &PatternMatchSingleton<SourceRange>,
pattern_context: &mut PatternContext,
Expand All @@ -1904,7 +1904,7 @@ impl Compiler {
Ok(())
}

fn codegen_pattern_helper_rotate(
fn compile_pattern_helper_rotate(
&mut self,
count: usize,
) -> CompileResult<()> {
Expand Down Expand Up @@ -1938,7 +1938,7 @@ impl Compiler {
// RETURN_IF_ERROR(PyList_Append(pc->stores, n));
// return SUCCESS;
// }
fn codegen_pattern_helper_store_name(
fn compile_pattern_helper_store_name(
&mut self,
n: Option<&str>,
pattern_context: &mut PatternContext,
Expand All @@ -1953,18 +1953,18 @@ impl Compiler {
return Err(self.error(CodegenErrorType::DuplicateStore(n.to_string())));
}
let rotations = pattern_context.on_top + pattern_context.stores.len() + 1;
self.codegen_pattern_helper_rotate(rotations)?;
self.compile_pattern_helper_rotate(rotations)?;
pattern_context.stores.push(n.to_string());
Ok(())
}

fn codegen_pattern_star(
fn compile_pattern_star(
&mut self,
star: &PatternMatchStar<SourceRange>,
pattern_context: &mut PatternContext,
) -> CompileResult<()> {
// codegen_pattern_helper_store_name(c, LOC(p), p->v.MatchStar.name, pc));
self.codegen_pattern_helper_store_name(
self.compile_pattern_helper_store_name(
star.name.as_deref(),
pattern_context,
)?;
Expand Down Expand Up @@ -2011,38 +2011,38 @@ impl Compiler {
return Err(self.error(CodegenErrorType::InvalidMatchCase));
}
}
return self.codegen_pattern_helper_store_name(
return self.compile_pattern_helper_store_name(
as_pattern.name.as_deref(),
pattern_context,
);
}
pattern_context.on_top += 1;
emit!(self, Instruction::Duplicate);
self.codegen_pattern(as_pattern.pattern.as_ref().unwrap(), pattern_context)?;
self.compile_pattern(as_pattern.pattern.as_ref().unwrap(), pattern_context)?;
pattern_context.on_top -= 1;
self.codegen_pattern_helper_store_name(
self.compile_pattern_helper_store_name(
as_pattern.name.as_deref(),
pattern_context,
)?;
Ok(())
}

fn codegen_pattern(
fn compile_pattern(
&mut self,
pattern_type: &Pattern<SourceRange>,
pattern_context: &mut PatternContext,
) -> CompileResult<()> {
match &pattern_type {
Pattern::MatchValue(value) => self.codegen_pattern_value(&value, pattern_context),
Pattern::MatchValue(value) => self.compile_pattern_value(&value, pattern_context),
Pattern::MatchSingleton(singleton) => {
self.codegen_pattern_singleton(&singleton, pattern_context)
self.compile_pattern_singleton(&singleton, pattern_context)
}
Pattern::MatchSequence(_sequence) => {
Err(self.error(CodegenErrorType::NotImplementedYet))
}
Pattern::MatchMapping(_mapping) => Err(self.error(CodegenErrorType::NotImplementedYet)),
Pattern::MatchClass(_class) => Err(self.error(CodegenErrorType::NotImplementedYet)),
Pattern::MatchStar(star) => self.codegen_pattern_star(&star, pattern_context),
Pattern::MatchStar(star) => self.compile_pattern_star(&star, pattern_context),
Pattern::MatchAs(as_pattern) => self.codegen_pattern_as(&as_pattern, pattern_context),
Pattern::MatchOr(_or_pattern) => Err(self.error(CodegenErrorType::NotImplementedYet)),
}
Expand Down Expand Up @@ -2076,7 +2076,7 @@ impl Compiler {
pattern_context.allow_irrefutable = m.guard.is_some() || i == cases.len() - 1;
// pc->fail_pop = NULL;
pattern_context.on_top = 0;
self.codegen_pattern(&m.pattern, pattern_context)?;
self.compile_pattern(&m.pattern, pattern_context)?;
assert_eq!(pattern_context.on_top, 0);
// It's a match! Store all of the captured names (they're on the stack).
let nstores = pattern_context.stores.len();
Expand Down Expand Up @@ -3727,4 +3727,22 @@ for stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')):
"
));
}

#[test]
fn test_match() {
assert_dis_snapshot!(compile_exec(
r#"\
v = "one"
match v:
case "one":
v = "two"
case "two":
v = "three"
case "three":
v = "one"
case _:
v = "one"
"#
));
}
}