@@ -1904,6 +1904,20 @@ impl Compiler {
1904
1904
Ok ( ( ) )
1905
1905
}
1906
1906
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
+
1907
1921
// static int
1908
1922
// codegen_pattern_helper_store_name(compiler *c, location loc,
1909
1923
// identifier n, pattern_context *pc)
@@ -1926,7 +1940,6 @@ impl Compiler {
1926
1940
// }
1927
1941
fn codegen_pattern_helper_store_name (
1928
1942
& mut self ,
1929
- loc : SourceLocation ,
1930
1943
n : Option < & str > ,
1931
1944
pattern_context : & mut PatternContext ,
1932
1945
) -> CompileResult < ( ) > {
@@ -1940,8 +1953,7 @@ impl Compiler {
1940
1953
return Err ( self . error ( CodegenErrorType :: DuplicateStore ( n. to_string ( ) ) ) ) ;
1941
1954
}
1942
1955
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) ?;
1945
1957
pattern_context. stores . push ( n. to_string ( ) ) ;
1946
1958
Ok ( ( ) )
1947
1959
}
@@ -1953,7 +1965,6 @@ impl Compiler {
1953
1965
) -> CompileResult < ( ) > {
1954
1966
// codegen_pattern_helper_store_name(c, LOC(p), p->v.MatchStar.name, pc));
1955
1967
self . codegen_pattern_helper_store_name (
1956
- star. location ( ) ,
1957
1968
star. name . as_deref ( ) ,
1958
1969
pattern_context,
1959
1970
) ?;
@@ -1993,14 +2004,14 @@ impl Compiler {
1993
2004
if as_pattern. pattern . is_none ( ) {
1994
2005
// An irrefutable match:
1995
2006
if !pattern_context. allow_irrefutable {
2007
+ // TODO: better error message
1996
2008
if let Some ( name) = & as_pattern. name {
1997
2009
return Err ( self . error ( CodegenErrorType :: InvalidMatchCase ) ) ;
1998
2010
} else {
1999
2011
return Err ( self . error ( CodegenErrorType :: InvalidMatchCase ) ) ;
2000
2012
}
2001
2013
}
2002
2014
return self . codegen_pattern_helper_store_name (
2003
- as_pattern. location ( ) ,
2004
2015
as_pattern. name . as_deref ( ) ,
2005
2016
pattern_context,
2006
2017
) ;
@@ -2010,7 +2021,6 @@ impl Compiler {
2010
2021
self . codegen_pattern ( as_pattern. pattern . as_ref ( ) . unwrap ( ) , pattern_context) ?;
2011
2022
pattern_context. on_top -= 1 ;
2012
2023
self . codegen_pattern_helper_store_name (
2013
- as_pattern. location ( ) ,
2014
2024
as_pattern. name . as_deref ( ) ,
2015
2025
pattern_context,
2016
2026
) ?;
@@ -2045,8 +2055,9 @@ impl Compiler {
2045
2055
pattern_context : & mut PatternContext ,
2046
2056
) -> CompileResult < ( ) > {
2047
2057
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 ( ) ;
2050
2061
2051
2062
let match_case_type = cases. last ( ) . expect ( "cases is not empty" ) ;
2052
2063
let has_default = match_case_type. pattern . is_match_star ( ) && 1 < cases. len ( ) ;
@@ -2105,6 +2116,7 @@ impl Compiler {
2105
2116
}
2106
2117
self . compile_statements ( & m. body ) ?;
2107
2118
}
2119
+
2108
2120
self . switch_to_block ( end_block) ;
2109
2121
Ok ( ( ) )
2110
2122
}
@@ -3401,6 +3413,10 @@ impl Compiler {
3401
3413
ir:: BlockIdx :: NULL ,
3402
3414
"switching {prev:?} -> {block:?} to completed block"
3403
3415
) ;
3416
+ println ! ( "{}" , prev. 0 ) ;
3417
+ for ( count, b) in code. blocks . iter ( ) . enumerate ( ) {
3418
+ println ! ( "{count}: {} {}" , b. next. 0 , b. instructions. len( ) ) ;
3419
+ }
3404
3420
let prev_block = & mut code. blocks [ prev. 0 as usize ] ;
3405
3421
assert_eq ! (
3406
3422
prev_block. next. 0 ,
0 commit comments