9
9
extern crate rustpython_parser;
10
10
11
11
use self :: rustpython_parser:: { ast, parser} ;
12
- use super :: bytecode:: { self , CodeObject , Instruction } ;
12
+ use super :: bytecode:: { self , CallType , CodeObject , Instruction } ;
13
13
use super :: pyobject:: { PyObject , PyObjectKind , PyResult } ;
14
14
use super :: vm:: VirtualMachine ;
15
15
@@ -330,7 +330,9 @@ impl Compiler {
330
330
} ) ;
331
331
self . emit ( Instruction :: Rotate { amount : 2 } ) ;
332
332
self . compile_expression ( exc_type) ?;
333
- self . emit ( Instruction :: CallFunction { count : 2 } ) ;
333
+ self . emit ( Instruction :: CallFunction {
334
+ typ : CallType :: Positional ( 2 ) ,
335
+ } ) ;
334
336
335
337
// We cannot handle this exception type:
336
338
self . emit ( Instruction :: JumpIfFalse {
@@ -478,7 +480,7 @@ impl Compiler {
478
480
self . compile_expression ( base) ?;
479
481
}
480
482
self . emit ( Instruction :: CallFunction {
481
- count : 2 + bases. len ( ) ,
483
+ typ : CallType :: Positional ( 2 + bases. len ( ) ) ,
482
484
} ) ;
483
485
484
486
self . apply_decorators ( decorator_list) ;
@@ -498,10 +500,14 @@ impl Compiler {
498
500
match msg {
499
501
Some ( e) => {
500
502
self . compile_expression ( e) ?;
501
- self . emit ( Instruction :: CallFunction { count : 1 } ) ;
503
+ self . emit ( Instruction :: CallFunction {
504
+ typ : CallType :: Positional ( 1 ) ,
505
+ } ) ;
502
506
}
503
507
None => {
504
- self . emit ( Instruction :: CallFunction { count : 0 } ) ;
508
+ self . emit ( Instruction :: CallFunction {
509
+ typ : CallType :: Positional ( 0 ) ,
510
+ } ) ;
505
511
}
506
512
}
507
513
self . emit ( Instruction :: Raise { argc : 1 } ) ;
@@ -633,7 +639,9 @@ impl Compiler {
633
639
fn apply_decorators ( & mut self , decorator_list : & Vec < ast:: Expression > ) {
634
640
// Apply decorators:
635
641
for _ in decorator_list {
636
- self . emit ( Instruction :: CallFunction { count : 1 } ) ;
642
+ self . emit ( Instruction :: CallFunction {
643
+ typ : CallType :: Positional ( 1 ) ,
644
+ } ) ;
637
645
}
638
646
}
639
647
@@ -859,10 +867,11 @@ impl Compiler {
859
867
}
860
868
ast:: Expression :: Set { elements } => {
861
869
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
+ } ) ;
866
875
}
867
876
ast:: Expression :: Dict { elements } => {
868
877
let size = elements. len ( ) ;
@@ -968,37 +977,53 @@ impl Compiler {
968
977
args : & Vec < ast:: Expression > ,
969
978
keywords : & Vec < ast:: Keyword > ,
970
979
) -> Result < ( ) , String > {
971
- self . compile_expression ( & * function) ?;
980
+ self . compile_expression ( function) ?;
972
981
let count = args. len ( ) + keywords. len ( ) ;
973
982
974
983
// 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) ?;
993
985
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 ,
998
990
} ) ;
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
+ }
1000
998
} 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
+ }
1002
1027
}
1003
1028
Ok ( ( ) )
1004
1029
}
@@ -1068,7 +1093,10 @@ impl Compiler {
1068
1093
} ) ;
1069
1094
}
1070
1095
ast:: ComprehensionKind :: Set { .. } => {
1071
- self . emit ( Instruction :: BuildSet { size : 0 } ) ;
1096
+ self . emit ( Instruction :: BuildSet {
1097
+ size : 0 ,
1098
+ unpack : false ,
1099
+ } ) ;
1072
1100
}
1073
1101
ast:: ComprehensionKind :: Dict { .. } => {
1074
1102
self . emit ( Instruction :: BuildMap { size : 0 } ) ;
@@ -1183,7 +1211,9 @@ impl Compiler {
1183
1211
self . emit ( Instruction :: GetIter ) ;
1184
1212
1185
1213
// Call just created <listcomp> function:
1186
- self . emit ( Instruction :: CallFunction { count : 1 } ) ;
1214
+ self . emit ( Instruction :: CallFunction {
1215
+ typ : CallType :: Positional ( 1 ) ,
1216
+ } ) ;
1187
1217
Ok ( ( ) )
1188
1218
}
1189
1219
0 commit comments