@@ -1860,7 +1860,7 @@ impl Compiler {
1860
1860
// RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
1861
1861
// return SUCCESS;
1862
1862
// }
1863
- fn codegen_pattern_value (
1863
+ fn compile_pattern_value (
1864
1864
& mut self ,
1865
1865
value : & PatternMatchValue < SourceRange > ,
1866
1866
pattern_context : & mut PatternContext ,
@@ -1886,7 +1886,7 @@ impl Compiler {
1886
1886
// RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
1887
1887
// return SUCCESS;
1888
1888
// }
1889
- fn codegen_pattern_singleton (
1889
+ fn compile_pattern_singleton (
1890
1890
& mut self ,
1891
1891
singleton : & PatternMatchSingleton < SourceRange > ,
1892
1892
pattern_context : & mut PatternContext ,
@@ -1904,7 +1904,7 @@ impl Compiler {
1904
1904
Ok ( ( ) )
1905
1905
}
1906
1906
1907
- fn codegen_pattern_helper_rotate (
1907
+ fn compile_pattern_helper_rotate (
1908
1908
& mut self ,
1909
1909
count : usize ,
1910
1910
) -> CompileResult < ( ) > {
@@ -1938,7 +1938,7 @@ impl Compiler {
1938
1938
// RETURN_IF_ERROR(PyList_Append(pc->stores, n));
1939
1939
// return SUCCESS;
1940
1940
// }
1941
- fn codegen_pattern_helper_store_name (
1941
+ fn compile_pattern_helper_store_name (
1942
1942
& mut self ,
1943
1943
n : Option < & str > ,
1944
1944
pattern_context : & mut PatternContext ,
@@ -1953,18 +1953,18 @@ impl Compiler {
1953
1953
return Err ( self . error ( CodegenErrorType :: DuplicateStore ( n. to_string ( ) ) ) ) ;
1954
1954
}
1955
1955
let rotations = pattern_context. on_top + pattern_context. stores . len ( ) + 1 ;
1956
- self . codegen_pattern_helper_rotate ( rotations) ?;
1956
+ self . compile_pattern_helper_rotate ( rotations) ?;
1957
1957
pattern_context. stores . push ( n. to_string ( ) ) ;
1958
1958
Ok ( ( ) )
1959
1959
}
1960
1960
1961
- fn codegen_pattern_star (
1961
+ fn compile_pattern_star (
1962
1962
& mut self ,
1963
1963
star : & PatternMatchStar < SourceRange > ,
1964
1964
pattern_context : & mut PatternContext ,
1965
1965
) -> CompileResult < ( ) > {
1966
1966
// codegen_pattern_helper_store_name(c, LOC(p), p->v.MatchStar.name, pc));
1967
- self . codegen_pattern_helper_store_name (
1967
+ self . compile_pattern_helper_store_name (
1968
1968
star. name . as_deref ( ) ,
1969
1969
pattern_context,
1970
1970
) ?;
@@ -2011,38 +2011,38 @@ impl Compiler {
2011
2011
return Err ( self . error ( CodegenErrorType :: InvalidMatchCase ) ) ;
2012
2012
}
2013
2013
}
2014
- return self . codegen_pattern_helper_store_name (
2014
+ return self . compile_pattern_helper_store_name (
2015
2015
as_pattern. name . as_deref ( ) ,
2016
2016
pattern_context,
2017
2017
) ;
2018
2018
}
2019
2019
pattern_context. on_top += 1 ;
2020
2020
emit ! ( self , Instruction :: Duplicate ) ;
2021
- self . codegen_pattern ( as_pattern. pattern . as_ref ( ) . unwrap ( ) , pattern_context) ?;
2021
+ self . compile_pattern ( as_pattern. pattern . as_ref ( ) . unwrap ( ) , pattern_context) ?;
2022
2022
pattern_context. on_top -= 1 ;
2023
- self . codegen_pattern_helper_store_name (
2023
+ self . compile_pattern_helper_store_name (
2024
2024
as_pattern. name . as_deref ( ) ,
2025
2025
pattern_context,
2026
2026
) ?;
2027
2027
Ok ( ( ) )
2028
2028
}
2029
2029
2030
- fn codegen_pattern (
2030
+ fn compile_pattern (
2031
2031
& mut self ,
2032
2032
pattern_type : & Pattern < SourceRange > ,
2033
2033
pattern_context : & mut PatternContext ,
2034
2034
) -> CompileResult < ( ) > {
2035
2035
match & pattern_type {
2036
- Pattern :: MatchValue ( value) => self . codegen_pattern_value ( & value, pattern_context) ,
2036
+ Pattern :: MatchValue ( value) => self . compile_pattern_value ( & value, pattern_context) ,
2037
2037
Pattern :: MatchSingleton ( singleton) => {
2038
- self . codegen_pattern_singleton ( & singleton, pattern_context)
2038
+ self . compile_pattern_singleton ( & singleton, pattern_context)
2039
2039
}
2040
2040
Pattern :: MatchSequence ( _sequence) => {
2041
2041
Err ( self . error ( CodegenErrorType :: NotImplementedYet ) )
2042
2042
}
2043
2043
Pattern :: MatchMapping ( _mapping) => Err ( self . error ( CodegenErrorType :: NotImplementedYet ) ) ,
2044
2044
Pattern :: MatchClass ( _class) => Err ( self . error ( CodegenErrorType :: NotImplementedYet ) ) ,
2045
- Pattern :: MatchStar ( star) => self . codegen_pattern_star ( & star, pattern_context) ,
2045
+ Pattern :: MatchStar ( star) => self . compile_pattern_star ( & star, pattern_context) ,
2046
2046
Pattern :: MatchAs ( as_pattern) => self . codegen_pattern_as ( & as_pattern, pattern_context) ,
2047
2047
Pattern :: MatchOr ( _or_pattern) => Err ( self . error ( CodegenErrorType :: NotImplementedYet ) ) ,
2048
2048
}
@@ -2076,7 +2076,7 @@ impl Compiler {
2076
2076
pattern_context. allow_irrefutable = m. guard . is_some ( ) || i == cases. len ( ) - 1 ;
2077
2077
// pc->fail_pop = NULL;
2078
2078
pattern_context. on_top = 0 ;
2079
- self . codegen_pattern ( & m. pattern , pattern_context) ?;
2079
+ self . compile_pattern ( & m. pattern , pattern_context) ?;
2080
2080
assert_eq ! ( pattern_context. on_top, 0 ) ;
2081
2081
// It's a match! Store all of the captured names (they're on the stack).
2082
2082
let nstores = pattern_context. stores . len ( ) ;
@@ -3727,4 +3727,22 @@ for stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')):
3727
3727
"
3728
3728
) ) ;
3729
3729
}
3730
+
3731
+ #[ test]
3732
+ fn test_match ( ) {
3733
+ assert_dis_snapshot ! ( compile_exec(
3734
+ r#"\
3735
+ v = "one"
3736
+ match v:
3737
+ case "one":
3738
+ v = "two"
3739
+ case "two":
3740
+ v = "three"
3741
+ case "three":
3742
+ v = "one"
3743
+ case _:
3744
+ v = "one"
3745
+ "#
3746
+ ) ) ;
3747
+ }
3730
3748
}
0 commit comments