@@ -15,7 +15,7 @@ struct Compiler {
1515
1616pub fn compile ( rt : & mut Executor , source : & String , mode : Mode ) -> Result < PyObjectRef , String > {
1717 let mut compiler = Compiler :: new ( ) ;
18- compiler. push_code_object ( CodeObject :: new ( ) ) ;
18+ compiler. push_code_object ( CodeObject :: new ( Vec :: new ( ) ) ) ;
1919 match mode {
2020 Mode :: Exec => match parser:: parse_program ( source) {
2121 Ok ( ast) => {
@@ -62,7 +62,7 @@ impl Compiler {
6262 }
6363
6464 fn push_code_object ( & mut self , code_object : CodeObject ) {
65- self . code_object_stack . push ( CodeObject :: new ( ) ) ;
65+ self . code_object_stack . push ( CodeObject :: new ( Vec :: new ( ) ) ) ;
6666 }
6767
6868 fn pop_code_object ( & mut self ) -> CodeObject {
@@ -191,7 +191,7 @@ impl Compiler {
191191 }
192192 ast:: Statement :: FunctionDef { name, args, body } => {
193193 // Create bytecode for this function:
194- self . code_object_stack . push ( CodeObject :: new ( ) ) ;
194+ self . code_object_stack . push ( CodeObject :: new ( args . to_vec ( ) ) ) ;
195195 self . compile_statements ( body) ;
196196 let code = self . code_object_stack . pop ( ) . unwrap ( ) ;
197197 self . emit ( Instruction :: LoadConst {
@@ -444,6 +444,22 @@ impl Compiler {
444444 name : name. to_string ( ) ,
445445 } ) ;
446446 }
447+ ast:: Expression :: Lambda { args, body } => {
448+ self . code_object_stack . push ( CodeObject :: new ( args. to_vec ( ) ) ) ;
449+ self . compile_expression ( body) ;
450+ self . emit ( Instruction :: ReturnValue ) ;
451+ let code = self . code_object_stack . pop ( ) . unwrap ( ) ;
452+ self . emit ( Instruction :: LoadConst {
453+ value : bytecode:: Constant :: Code { code : code } ,
454+ } ) ;
455+ self . emit ( Instruction :: LoadConst {
456+ value : bytecode:: Constant :: String {
457+ value : String :: from ( "<lambda>" ) ,
458+ } ,
459+ } ) ;
460+ // Turn code object into function object:
461+ self . emit ( Instruction :: MakeFunction ) ;
462+ }
447463 }
448464 }
449465
0 commit comments