99extern crate rustpython_parser;
1010
1111use self :: rustpython_parser:: { ast, parser} ;
12- use super :: bytecode:: { self , CodeObject , Instruction } ;
12+ use super :: bytecode:: { self , CallType , CodeObject , Instruction } ;
1313use super :: pyobject:: { PyObject , PyObjectKind , PyResult } ;
1414use super :: vm:: VirtualMachine ;
1515
@@ -330,7 +330,9 @@ impl Compiler {
330330 } ) ;
331331 self . emit ( Instruction :: Rotate { amount : 2 } ) ;
332332 self . compile_expression ( exc_type) ?;
333- self . emit ( Instruction :: CallFunction { count : 2 } ) ;
333+ self . emit ( Instruction :: CallFunction {
334+ typ : CallType :: Positional ( 2 ) ,
335+ } ) ;
334336
335337 // We cannot handle this exception type:
336338 self . emit ( Instruction :: JumpIfFalse {
@@ -478,7 +480,7 @@ impl Compiler {
478480 self . compile_expression ( base) ?;
479481 }
480482 self . emit ( Instruction :: CallFunction {
481- count : 2 + bases. len ( ) ,
483+ typ : CallType :: Positional ( 2 + bases. len ( ) ) ,
482484 } ) ;
483485
484486 self . apply_decorators ( decorator_list) ;
@@ -498,10 +500,14 @@ impl Compiler {
498500 match msg {
499501 Some ( e) => {
500502 self . compile_expression ( e) ?;
501- self . emit ( Instruction :: CallFunction { count : 1 } ) ;
503+ self . emit ( Instruction :: CallFunction {
504+ typ : CallType :: Positional ( 1 ) ,
505+ } ) ;
502506 }
503507 None => {
504- self . emit ( Instruction :: CallFunction { count : 0 } ) ;
508+ self . emit ( Instruction :: CallFunction {
509+ typ : CallType :: Positional ( 0 ) ,
510+ } ) ;
505511 }
506512 }
507513 self . emit ( Instruction :: Raise { argc : 1 } ) ;
@@ -633,7 +639,9 @@ impl Compiler {
633639 fn apply_decorators ( & mut self , decorator_list : & Vec < ast:: Expression > ) {
634640 // Apply decorators:
635641 for _ in decorator_list {
636- self . emit ( Instruction :: CallFunction { count : 1 } ) ;
642+ self . emit ( Instruction :: CallFunction {
643+ typ : CallType :: Positional ( 1 ) ,
644+ } ) ;
637645 }
638646 }
639647
@@ -859,10 +867,11 @@ impl Compiler {
859867 }
860868 ast:: Expression :: Set { elements } => {
861869 let size = elements. len ( ) ;
862- for element in elements {
863- self . compile_expression ( element) ?;
864- }
865- self . emit ( Instruction :: BuildSet { size : size } ) ;
870+ let must_unpack = self . gather_elements ( elements) ?;
871+ self . emit ( Instruction :: BuildSet {
872+ size : size,
873+ unpack : must_unpack,
874+ } ) ;
866875 }
867876 ast:: Expression :: Dict { elements } => {
868877 let size = elements. len ( ) ;
@@ -968,37 +977,53 @@ impl Compiler {
968977 args : & Vec < ast:: Expression > ,
969978 keywords : & Vec < ast:: Keyword > ,
970979 ) -> Result < ( ) , String > {
971- self . compile_expression ( & * function) ?;
980+ self . compile_expression ( function) ?;
972981 let count = args. len ( ) + keywords. len ( ) ;
973982
974983 // Normal arguments:
975- for value in args {
976- self . compile_expression ( value) ?;
977- }
978-
979- // Keyword arguments:
980- if keywords. len ( ) > 0 {
981- let mut kwarg_names = vec ! [ ] ;
982- for keyword in keywords {
983- if let Some ( name) = & keyword. name {
984- kwarg_names. push ( bytecode:: Constant :: String {
985- value : name. to_string ( ) ,
986- } ) ;
987- } else {
988- // This means **kwargs!
989- panic ! ( "name must be set" ) ;
990- }
991- self . compile_expression ( & keyword. value ) ?;
992- }
984+ let must_unpack = self . gather_elements ( args) ?;
993985
994- self . emit ( Instruction :: LoadConst {
995- value : bytecode :: Constant :: Tuple {
996- elements : kwarg_names ,
997- } ,
986+ if must_unpack {
987+ self . emit ( Instruction :: BuildTuple {
988+ size : args . len ( ) ,
989+ unpack : true ,
998990 } ) ;
999- self . emit ( Instruction :: CallFunctionKw { count } ) ;
991+ if keywords. len ( ) > 0 {
992+ unimplemented ! ( )
993+ } else {
994+ self . emit ( Instruction :: CallFunction {
995+ typ : CallType :: Ex ( false ) ,
996+ } ) ;
997+ }
1000998 } else {
1001- self . emit ( Instruction :: CallFunction { count } ) ;
999+ // Keyword arguments:
1000+ if keywords. len ( ) > 0 {
1001+ let mut kwarg_names = vec ! [ ] ;
1002+ for keyword in keywords {
1003+ if let Some ( name) = & keyword. name {
1004+ kwarg_names. push ( bytecode:: Constant :: String {
1005+ value : name. to_string ( ) ,
1006+ } ) ;
1007+ } else {
1008+ // This means **kwargs!
1009+ panic ! ( "name must be set" ) ;
1010+ }
1011+ self . compile_expression ( & keyword. value ) ?;
1012+ }
1013+
1014+ self . emit ( Instruction :: LoadConst {
1015+ value : bytecode:: Constant :: Tuple {
1016+ elements : kwarg_names,
1017+ } ,
1018+ } ) ;
1019+ self . emit ( Instruction :: CallFunction {
1020+ typ : CallType :: Keyword ( count) ,
1021+ } ) ;
1022+ } else {
1023+ self . emit ( Instruction :: CallFunction {
1024+ typ : CallType :: Positional ( count) ,
1025+ } ) ;
1026+ }
10021027 }
10031028 Ok ( ( ) )
10041029 }
@@ -1068,7 +1093,10 @@ impl Compiler {
10681093 } ) ;
10691094 }
10701095 ast:: ComprehensionKind :: Set { .. } => {
1071- self . emit ( Instruction :: BuildSet { size : 0 } ) ;
1096+ self . emit ( Instruction :: BuildSet {
1097+ size : 0 ,
1098+ unpack : false ,
1099+ } ) ;
10721100 }
10731101 ast:: ComprehensionKind :: Dict { .. } => {
10741102 self . emit ( Instruction :: BuildMap { size : 0 } ) ;
@@ -1183,7 +1211,9 @@ impl Compiler {
11831211 self . emit ( Instruction :: GetIter ) ;
11841212
11851213 // Call just created <listcomp> function:
1186- self . emit ( Instruction :: CallFunction { count : 1 } ) ;
1214+ self . emit ( Instruction :: CallFunction {
1215+ typ : CallType :: Positional ( 1 ) ,
1216+ } ) ;
11871217 Ok ( ( ) )
11881218 }
11891219
0 commit comments