Skip to content

Commit 2604746

Browse files
committed
more implementation
1 parent a5d1beb commit 2604746

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

compiler/codegen/src/compile.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -1904,6 +1904,20 @@ impl Compiler {
19041904
Ok(())
19051905
}
19061906

1907+
fn codegen_pattern_helper_rotate(
1908+
&mut self,
1909+
count: usize,
1910+
) -> CompileResult<()> {
1911+
let mut count = count;
1912+
while 1 < count {
1913+
todo!("below");
1914+
// ADDOP_I(c, loc, SWAP, count--);
1915+
// emit!(self, Instruction::Swap { count: count as u32 });
1916+
count -= 1;
1917+
}
1918+
Ok(())
1919+
}
1920+
19071921
// static int
19081922
// codegen_pattern_helper_store_name(compiler *c, location loc,
19091923
// identifier n, pattern_context *pc)
@@ -1926,7 +1940,6 @@ impl Compiler {
19261940
// }
19271941
fn codegen_pattern_helper_store_name(
19281942
&mut self,
1929-
loc: SourceLocation,
19301943
n: Option<&str>,
19311944
pattern_context: &mut PatternContext,
19321945
) -> CompileResult<()> {
@@ -1940,8 +1953,7 @@ impl Compiler {
19401953
return Err(self.error(CodegenErrorType::DuplicateStore(n.to_string())));
19411954
}
19421955
let rotations = pattern_context.on_top + pattern_context.stores.len() + 1;
1943-
todo!("below");
1944-
// self.codegen_pattern_helper_rotate(loc, rotations)?;
1956+
self.codegen_pattern_helper_rotate(rotations)?;
19451957
pattern_context.stores.push(n.to_string());
19461958
Ok(())
19471959
}
@@ -1953,7 +1965,6 @@ impl Compiler {
19531965
) -> CompileResult<()> {
19541966
// codegen_pattern_helper_store_name(c, LOC(p), p->v.MatchStar.name, pc));
19551967
self.codegen_pattern_helper_store_name(
1956-
star.location(),
19571968
star.name.as_deref(),
19581969
pattern_context,
19591970
)?;
@@ -1993,14 +2004,14 @@ impl Compiler {
19932004
if as_pattern.pattern.is_none() {
19942005
// An irrefutable match:
19952006
if !pattern_context.allow_irrefutable {
2007+
// TODO: better error message
19962008
if let Some(name) = &as_pattern.name {
19972009
return Err(self.error(CodegenErrorType::InvalidMatchCase));
19982010
} else {
19992011
return Err(self.error(CodegenErrorType::InvalidMatchCase));
20002012
}
20012013
}
20022014
return self.codegen_pattern_helper_store_name(
2003-
as_pattern.location(),
20042015
as_pattern.name.as_deref(),
20052016
pattern_context,
20062017
);
@@ -2010,7 +2021,6 @@ impl Compiler {
20102021
self.codegen_pattern(as_pattern.pattern.as_ref().unwrap(), pattern_context)?;
20112022
pattern_context.on_top -= 1;
20122023
self.codegen_pattern_helper_store_name(
2013-
as_pattern.location(),
20142024
as_pattern.name.as_deref(),
20152025
pattern_context,
20162026
)?;
@@ -2045,8 +2055,9 @@ impl Compiler {
20452055
pattern_context: &mut PatternContext,
20462056
) -> CompileResult<()> {
20472057
self.compile_expression(subject)?;
2048-
// Block at the end of the switch statement that we jump to after finishing a branch
2049-
let end_block = self.new_block();
2058+
// Blocks at the end of the switch statement that we jump to after finishing a branch
2059+
// TODO: optimize, we can reuse the same block for all cases
2060+
let mut end_block = self.new_block();
20502061

20512062
let match_case_type = cases.last().expect("cases is not empty");
20522063
let has_default = match_case_type.pattern.is_match_star() && 1 < cases.len();
@@ -2105,6 +2116,7 @@ impl Compiler {
21052116
}
21062117
self.compile_statements(&m.body)?;
21072118
}
2119+
21082120
self.switch_to_block(end_block);
21092121
Ok(())
21102122
}
@@ -3401,6 +3413,10 @@ impl Compiler {
34013413
ir::BlockIdx::NULL,
34023414
"switching {prev:?} -> {block:?} to completed block"
34033415
);
3416+
println!("{}", prev.0);
3417+
for (count, b) in code.blocks.iter().enumerate() {
3418+
println!("{count}: {} {}", b.next.0, b.instructions.len());
3419+
}
34043420
let prev_block = &mut code.blocks[prev.0 as usize];
34053421
assert_eq!(
34063422
prev_block.next.0,

0 commit comments

Comments
 (0)