Skip to content

Commit f644179

Browse files
committed
add swap (sort of)
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
1 parent be4df5d commit f644179

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

compiler/codegen/src/compile.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ use itertools::Itertools;
1818
use num_complex::Complex64;
1919
use num_traits::ToPrimitive;
2020
use rustpython_ast::located::{self as located_ast, Located};
21-
use rustpython_ast::{
22-
Pattern, PatternMatchAs, PatternMatchSingleton, PatternMatchStar, PatternMatchValue,
23-
};
24-
use rustpython_compiler_core::bytecode::ComparisonOperator;
21+
use rustpython_ast::{Pattern, PatternMatchAs, PatternMatchSingleton, PatternMatchStar, PatternMatchValue};
22+
use rustpython_compiler_core::bytecode::{Arg, ComparisonOperator};
2523
use rustpython_compiler_core::{
2624
bytecode::{self, Arg as OpArgMarker, CodeObject, ConstantData, Instruction, OpArg, OpArgType},
2725
Mode,
@@ -1826,18 +1824,9 @@ impl Compiler {
18261824
Ok(())
18271825
}
18281826

1829-
// static int
1830-
// jump_to_fail_pop(compiler *c, location loc,
1831-
// pattern_context *pc, int op)
1832-
// {
1833-
// // Pop any items on the top of the stack, plus any objects we were going to
1834-
// // capture on success:
1835-
// Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores);
1836-
// RETURN_IF_ERROR(ensure_fail_pop(c, pc, pops));
1837-
// ADDOP_JUMP(c, loc, op, pc->fail_pop[pops]);
1838-
// return SUCCESS;
1839-
// }
18401827
fn jump_to_fail_pop(&mut self, pattern_context: &mut PatternContext) -> CompileResult<()> {
1828+
// Pop any items on the top of the stack, plus any objects we were going to
1829+
// capture on success:
18411830
let pops = pattern_context.on_top + pattern_context.stores.len();
18421831
self.ensure_fail_pop(pattern_context, pops)?;
18431832
self.switch_to_block(pattern_context.fail_pop[pops]);
@@ -1908,9 +1897,9 @@ impl Compiler {
19081897
) -> CompileResult<()> {
19091898
let mut count = count;
19101899
while 1 < count {
1911-
todo!("below");
1900+
todo!();
19121901
// ADDOP_I(c, loc, SWAP, count--);
1913-
// emit!(self, Instruction::Swap { count: count as u32 });
1902+
// emit!(self, Instruction::Swap(Arg::new(count as u32)));
19141903
count -= 1;
19151904
}
19161905
Ok(())
@@ -2030,6 +2019,8 @@ impl Compiler {
20302019
pattern_type: &Pattern<SourceRange>,
20312020
pattern_context: &mut PatternContext,
20322021
) -> CompileResult<()> {
2022+
let block = self.new_block();
2023+
self.switch_to_block(block);
20332024
match &pattern_type {
20342025
Pattern::MatchValue(value) => self.compile_pattern_value(&value, pattern_context),
20352026
Pattern::MatchSingleton(singleton) => {
@@ -2053,6 +2044,8 @@ impl Compiler {
20532044
pattern_context: &mut PatternContext,
20542045
) -> CompileResult<()> {
20552046
self.compile_expression(subject)?;
2047+
let block = self.new_block();
2048+
self.switch_to_block(block);
20562049
// Block at the end of the switch statement that we jump to after finishing a branch
20572050
let end_block = self.new_block();
20582051

@@ -3409,10 +3402,6 @@ impl Compiler {
34093402
ir::BlockIdx::NULL,
34103403
"switching {prev:?} -> {block:?} to completed block"
34113404
);
3412-
println!("{}", prev.0);
3413-
for (count, b) in code.blocks.iter().enumerate() {
3414-
println!("{count}: {} {}", b.next.0, b.instructions.len());
3415-
}
34163405
let prev_block = &mut code.blocks[prev.0 as usize];
34173406
assert_eq!(
34183407
prev_block.next.0,

0 commit comments

Comments
 (0)