2013-12-21 13:17:45 -05:00
# include <stdlib.h>
# include <stdint.h>
# include <string.h>
# include <assert.h>
# include "nlr.h"
# include "misc.h"
# include "mpconfig.h"
2014-01-21 16:40:13 -05:00
# include "qstr.h"
2013-12-21 13:17:45 -05:00
# include "obj.h"
# include "runtime.h"
# include "bc.h"
/******************************************************************************/
/* generator wrapper */
typedef struct _mp_obj_gen_wrap_t {
mp_obj_base_t base ;
mp_obj_t * fun ;
} mp_obj_gen_wrap_t ;
2014-02-12 11:15:40 -05:00
STATIC mp_obj_t gen_wrap_call ( mp_obj_t self_in , uint n_args , uint n_kw , const mp_obj_t * args ) {
2013-12-21 13:17:45 -05:00
mp_obj_gen_wrap_t * self = self_in ;
mp_obj_t self_fun = self - > fun ;
assert ( MP_OBJ_IS_TYPE ( self_fun , & fun_bc_type ) ) ;
int bc_n_args ;
uint bc_n_state ;
const byte * bc_code ;
mp_obj_fun_bc_get ( self_fun , & bc_n_args , & bc_n_state , & bc_code ) ;
if ( n_args ! = bc_n_args ) {
2014-01-04 10:57:35 -05:00
nlr_jump ( mp_obj_new_exception_msg_2_args ( MP_QSTR_TypeError , " function takes %d positional arguments but %d were given " , ( const char * ) ( machine_int_t ) bc_n_args , ( const char * ) ( machine_int_t ) n_args ) ) ;
2013-12-21 13:17:45 -05:00
}
2014-01-18 09:10:48 -05:00
if ( n_kw ! = 0 ) {
nlr_jump ( mp_obj_new_exception_msg ( MP_QSTR_TypeError , " function does not take keyword arguments " ) ) ;
}
2013-12-30 17:32:17 -05:00
2014-01-29 15:30:52 -05:00
return mp_obj_new_gen_instance ( bc_code , bc_n_state , n_args , args ) ;
2013-12-21 13:17:45 -05:00
}
const mp_obj_type_t gen_wrap_type = {
{ & mp_const_type } ,
" generator " ,
2014-01-18 09:10:48 -05:00
. call = gen_wrap_call ,
2013-12-21 13:17:45 -05:00
} ;
2014-01-29 15:30:52 -05:00
mp_obj_t mp_obj_new_gen_wrap ( mp_obj_t fun ) {
2013-12-21 13:17:45 -05:00
mp_obj_gen_wrap_t * o = m_new_obj ( mp_obj_gen_wrap_t ) ;
o - > base . type = & gen_wrap_type ;
o - > fun = fun ;
return o ;
}
/******************************************************************************/
/* generator instance */
typedef struct _mp_obj_gen_instance_t {
mp_obj_base_t base ;
2014-01-18 18:24:36 -05:00
const byte * code_info ;
2013-12-21 13:17:45 -05:00
const byte * ip ;
mp_obj_t * sp ;
2014-01-18 09:10:48 -05:00
uint n_state ;
2014-01-02 15:57:05 -05:00
mp_obj_t state [ ] ;
2013-12-21 13:17:45 -05:00
} mp_obj_gen_instance_t ;
2014-01-13 12:19:16 -05:00
void gen_instance_print ( void ( * print ) ( void * env , const char * fmt , . . . ) , void * env , mp_obj_t self_in , mp_print_kind_t kind ) {
2013-12-21 13:17:45 -05:00
print ( env , " <generator object 'fun-name' at %p> " , self_in ) ;
}
mp_obj_t gen_instance_getiter ( mp_obj_t self_in ) {
return self_in ;
}
2014-02-12 11:15:40 -05:00
STATIC mp_obj_t gen_next_send ( mp_obj_t self_in , mp_obj_t send_value ) {
2013-12-21 13:17:45 -05:00
mp_obj_gen_instance_t * self = self_in ;
2014-01-26 18:01:37 -05:00
if ( self - > ip = = 0 ) {
return mp_const_stop_iteration ;
}
2014-01-26 13:50:11 -05:00
if ( self - > sp = = self - > state - 1 ) {
if ( send_value ! = mp_const_none ) {
nlr_jump ( mp_obj_new_exception_msg ( MP_QSTR_TypeError , " can't send non-None value to a just-started generator " ) ) ;
}
} else {
* self - > sp = send_value ;
}
2014-01-18 18:24:36 -05:00
bool yield = mp_execute_byte_code_2 ( self - > code_info , & self - > ip , & self - > state [ self - > n_state - 1 ] , & self - > sp ) ;
2013-12-21 13:17:45 -05:00
if ( yield ) {
return * self - > sp ;
} else {
2014-01-26 18:01:37 -05:00
// Explicitly mark generator as completed. If we don't do this,
// subsequent next() may re-execute statements after last yield
// again and again, leading to side effects.
// TODO: check how return with value behaves under such conditions
// in CPython.
self - > ip = 0 ;
2013-12-21 13:17:45 -05:00
if ( * self - > sp = = mp_const_none ) {
return mp_const_stop_iteration ;
} else {
// TODO return StopIteration with value *self->sp
return mp_const_stop_iteration ;
}
}
}
2014-01-26 13:50:11 -05:00
mp_obj_t gen_instance_iternext ( mp_obj_t self_in ) {
2014-01-26 18:01:37 -05:00
return gen_next_send ( self_in , mp_const_none ) ;
}
2014-02-12 11:15:40 -05:00
STATIC mp_obj_t gen_instance_send ( mp_obj_t self_in , mp_obj_t send_value ) {
2014-01-26 18:01:37 -05:00
mp_obj_t ret = gen_next_send ( self_in , send_value ) ;
if ( ret = = mp_const_stop_iteration ) {
nlr_jump ( mp_obj_new_exception ( MP_QSTR_StopIteration ) ) ;
}
return ret ;
2014-01-26 13:50:11 -05:00
}
2014-02-12 11:15:40 -05:00
STATIC MP_DEFINE_CONST_FUN_OBJ_2 ( gen_instance_send_obj , gen_instance_send ) ;
2014-01-26 13:50:11 -05:00
2014-02-12 11:15:40 -05:00
STATIC const mp_method_t gen_type_methods [ ] = {
2014-01-26 18:01:37 -05:00
{ " send " , & gen_instance_send_obj } ,
2014-01-26 13:50:11 -05:00
{ NULL , NULL } , // end-of-list sentinel
} ;
2013-12-21 13:17:45 -05:00
const mp_obj_type_t gen_instance_type = {
{ & mp_const_type } ,
" generator " ,
2014-01-07 10:58:30 -05:00
. print = gen_instance_print ,
. getiter = gen_instance_getiter ,
. iternext = gen_instance_iternext ,
2014-01-26 13:50:11 -05:00
. methods = gen_type_methods ,
2013-12-21 13:17:45 -05:00
} ;
2014-01-02 15:57:05 -05:00
mp_obj_t mp_obj_new_gen_instance ( const byte * bytecode , uint n_state , int n_args , const mp_obj_t * args ) {
mp_obj_gen_instance_t * o = m_new_obj_var ( mp_obj_gen_instance_t , mp_obj_t , n_state ) ;
2013-12-21 13:17:45 -05:00
o - > base . type = & gen_instance_type ;
2014-01-18 18:24:36 -05:00
o - > code_info = bytecode ;
2014-01-02 15:57:05 -05:00
o - > ip = bytecode ;
2014-01-18 09:10:48 -05:00
o - > sp = & o - > state [ 0 ] - 1 ; // sp points to top of stack, which starts off 1 below the state
o - > n_state = n_state ;
2014-01-02 15:57:05 -05:00
2014-01-18 09:10:48 -05:00
// copy args to end of state array, in reverse (that's how mp_execute_byte_code_2 needs it)
2014-01-02 15:57:05 -05:00
for ( int i = 0 ; i < n_args ; i + + ) {
2014-01-18 09:10:48 -05:00
o - > state [ n_state - 1 - i ] = args [ i ] ;
2014-01-02 15:57:05 -05:00
}
// TODO
// prelude for making cells (closed over variables)
// for now we just make sure there are no cells variables
// need to work out how to implement closed over variables in generators
2014-01-18 18:24:36 -05:00
// get code info size
machine_uint_t code_info_size = bytecode [ 0 ] | ( bytecode [ 1 ] < < 8 ) | ( bytecode [ 2 ] < < 16 ) | ( bytecode [ 3 ] < < 24 ) ;
o - > ip + = code_info_size ;
2014-01-02 15:57:05 -05:00
assert ( o - > ip [ 0 ] = = 0 ) ;
o - > ip + = 1 ;
2013-12-21 13:17:45 -05:00
return o ;
}