Skip to content

Commit

Permalink
update compiler namespace commit() and rollback()
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Nov 28, 2024
1 parent ac2a6ef commit 8462a3f
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 19 deletions.
6 changes: 4 additions & 2 deletions src/core/chuck_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1110,9 +1110,11 @@ t_CKBOOL Chuck_Compiler::compile_single( Chuck_CompileTarget * target )
EM_setCurrentTarget( NULL );

// commit
if( ret ) env()->global()->commit();
if( ret ) // 1.5.4.3 (ge) update to call commit_namespaces | was: env()->global()->commit();
{ env()->commit_namespaces(); }
// or rollback
else env()->global()->rollback();
else // 1.5.4.3 (ge) update to call commit_namespaces | was: env()->global()->rollback();
{ env()->rollback_namespaces(); }

// unload the context from the type-checker
if( !type_engine_unload_context( env() ) )
Expand Down
19 changes: 10 additions & 9 deletions src/core/chuck_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ t_CKBOOL type_engine_scan0_prog( Chuck_Env * env, a_Program prog,
// NOTE: for legacy reasons, fallback to [global] if no [user] namespace
// this fallback mechanism was moved out of env->user() to here for clarity in 1.5.4.0 (ge)
// needs further verification, as this fallback may not be necessary
if( !prog->section->class_def->home ) prog->section->class_def->home = env->global();
if( !prog->section->class_def->home )
{ prog->section->class_def->home = env->global(); }
// -----------------------------------------
}
// scan the class definition
Expand Down Expand Up @@ -307,7 +308,7 @@ t_CKBOOL type_engine_scan0_class_def( Chuck_Env * env, a_Class_Def class_def )
// name | 1.5.2.0 (ge) added
the_class->nspc->pre_ctor->name = string("class ") + the_class->base_name;
// add to env
env->curr->type.add( the_class->base_name, the_class ); // URGENT: make this global
env->curr->add_type( the_class->base_name, the_class ); // URGENT: make this global
// incomplete
the_class->is_complete = FALSE;

Expand Down Expand Up @@ -370,7 +371,7 @@ t_CKBOOL type_engine_scan0_class_def( Chuck_Env * env, a_Class_Def class_def )
value->is_const = TRUE;
value->is_member = FALSE;
// add to env
env->curr->value.add( the_class->base_name, value );
env->curr->add_value( the_class->base_name, value );

// remember
class_def->type = the_class;
Expand Down Expand Up @@ -2663,7 +2664,7 @@ t_CKBOOL type_engine_scan2_exp_decl_create( Chuck_Env * env, a_Exp_Decl decl )
}

// enter into value binding
env->curr->value.add( var_decl->xid,
env->curr->add_value( S_name(var_decl->xid),
value = env->context->new_Chuck_Value( type, S_name(var_decl->xid) ) );

// remember the owner
Expand Down Expand Up @@ -3241,7 +3242,7 @@ t_CKBOOL type_engine_scan2_func_def( Chuck_Env * env, a_Func_Def f )
// later: add as value
// symbols.push_back( arg_list );
// values.push_back( v );
// later: env->curr->value.add( arg_list->var_decl->xid, v );
// later: env->curr->add_value( arg_list->var_decl->xid, v );

// stack
v->offset = f->stack_depth;
Expand Down Expand Up @@ -3394,15 +3395,15 @@ t_CKBOOL type_engine_scan2_func_def( Chuck_Env * env, a_Func_Def f )
}

// add as value
env->curr->value.add( value->name, value );
env->curr->add_value( value->name, value );
// enter the name into the function table
env->curr->func.add( func->name, func );
env->curr->add_func( func->name, func );

// if not overload, add entries for orig name
if( !overload )
{
env->curr->value.add( orig_name, value );
env->curr->func.add( orig_name, func );
env->curr->add_value( orig_name, value );
env->curr->add_func( orig_name, func );
}

// set the current function to this
Expand Down
111 changes: 105 additions & 6 deletions src/core/chuck_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,9 +903,11 @@ t_CKBOOL type_engine_check_prog( Chuck_Env * env, a_Program prog,
cleanup:

// commit
if( ret ) env->global()->commit();
if( ret ) // 1.5.4.3 (ge) update to env->commit_namespaces() | was: env->global()->commit();
{ env->commit_namespaces(); }
// or rollback
else env->global()->rollback();
else // 1.5.4.3 (ge) update to env->commit_namespaces() | was: env->global()->rollback();
{ env->rollback_namespaces(); }

// unload the context from the type-checker
if( !type_engine_unload_context( env ) )
Expand Down Expand Up @@ -4406,7 +4408,7 @@ t_CKTYPE type_engine_check_exp_decl_part2( Chuck_Env * env, a_Exp_Decl decl )
if( !env->class_def || env->class_scope > 0 )
{
// add as value
env->curr->value.add( var_decl->xid, value );
env->curr->add_value( S_name(var_decl->xid), value );
}

// the next var decl
Expand Down Expand Up @@ -5568,7 +5570,7 @@ t_CKBOOL type_engine_check_func_def( Chuck_Env * env, a_Func_Def f )
}

// add as value
env->curr->value.add( arg_list->var_decl->xid, v );
env->curr->add_value( S_name(arg_list->var_decl->xid), v );

// increment count
count++;
Expand Down Expand Up @@ -5655,6 +5657,51 @@ Chuck_Namespace::~Chuck_Namespace()



//-----------------------------------------------------------------------------
// name: add_type()
// desc: add type to name space
//-----------------------------------------------------------------------------
void Chuck_Namespace::add_type( const std::string & xid, Chuck_Type * type )
{
// log it
EM_log( CK_LOG_DEBUG, "namespace '%s' adding type '%s'->'%s'", this->name.c_str(), xid.c_str(), type->name().c_str() );
// add it
this->type.add( xid, type );
}




//-----------------------------------------------------------------------------
// name: add_value()
// desc: add value to name space
//-----------------------------------------------------------------------------
void Chuck_Namespace::add_value( const std::string & xid, Chuck_Value * value )
{
// log it
EM_log( CK_LOG_DEBUG, "namespace '%s' adding value '%s'->'%s'", this->name.c_str(), xid.c_str(), value->name.c_str() );
// add it
this->value.add( xid, value );
}




//-----------------------------------------------------------------------------
// name: add_func()
// desc: add type to name space
//-----------------------------------------------------------------------------
void Chuck_Namespace::add_func( const std::string & xid, Chuck_Func * func )
{
// log it
EM_log( CK_LOG_DEBUG, "namespace '%s' adding func '%s'->'%s'", this->name.c_str(), xid.c_str(), func->base_name.c_str() );
// add it
this->func.add( xid, func );
}




//-----------------------------------------------------------------------------
// name: lookup_value()
// desc: lookup value in the env; climb means to climb the scope and...
Expand Down Expand Up @@ -6859,7 +6906,7 @@ Chuck_Type * type_engine_import_class_begin( Chuck_Env * env, Chuck_Type * type,
value->is_member = FALSE;

// add to env
where->value.add( value->name, value );
where->add_value( value->name, value );

// make the type current
env->nspc_stack.push_back( env->curr );
Expand Down Expand Up @@ -6912,7 +6959,7 @@ Chuck_Type * type_engine_import_class_begin( Chuck_Env * env, const char * name,
type = new Chuck_Type( env, te_user, name, parent, sizeof(void *) );

// add to namespace - TODO: handle failure, remove from where
where->type.add( name, type );
where->add_type( name, type );

// do the rest
if( !type_engine_import_class_begin( env, type, where, pre_ctor, dtor, doc ) )
Expand Down Expand Up @@ -8082,6 +8129,58 @@ Chuck_Type * Chuck_Env::get_array_type( Chuck_Type * array_parent,



//-----------------------------------------------------------------------------
// name: commit_namespaces()
// desc: commit namespace | 1.5.4.3 (ge) added
//-----------------------------------------------------------------------------
void Chuck_Env::commit_namespaces()
{
// get current
Chuck_Namespace * nspc = this->curr;
// while not null and above user()
while( nspc != NULL && nspc != this->user() && nspc != this->global() )
{
// commit
nspc->commit();
// move up to parent
nspc = nspc->parent;
}

// if user, explicitly commit user
if( this->user() ) this->user()->commit();
// explicitly commit global
this->global()->commit();
}




//-----------------------------------------------------------------------------
// name: rollback_namespaces()
// desc: commit namespace | 1.5.4.3 (ge) added
//-----------------------------------------------------------------------------
void Chuck_Env::rollback_namespaces()
{
// get current
Chuck_Namespace * nspc = this->curr;
// while not null and above user()
while( nspc != NULL && nspc != this->user() && nspc != this->global() )
{
// rollback
nspc->rollback();
// move up to parent
nspc = nspc->parent;
}

// if user, explicitly rollback user
if( this->user() ) this->user()->rollback();
// explicitly rollback global
this->global()->rollback();
}




//-----------------------------------------------------------------------------
// operator overload (for map)
//-----------------------------------------------------------------------------
Expand Down
13 changes: 11 additions & 2 deletions src/core/chuck_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ struct Chuck_Namespace : public Chuck_VM_Object
// destructor
virtual ~Chuck_Namespace();

// add type to name space
void add_type( const std::string & xid, Chuck_Type * target );
void add_value( const std::string & xid, Chuck_Value * target );
void add_func( const std::string & xid, Chuck_Func * target );

// look up value
Chuck_Value * lookup_value( const std::string & name, t_CKINT climb = 1, t_CKBOOL stayWithinClassDef = FALSE );
Chuck_Value * lookup_value( S_Symbol name, t_CKINT climb = 1, t_CKBOOL stayWithinClassDef = FALSE );
Expand All @@ -346,13 +351,13 @@ struct Chuck_Namespace : public Chuck_VM_Object

// commit the maps
void commit() {
EM_log( CK_LOG_FINER, "committing namespace: '%s'...", name.c_str() );
EM_log( CK_LOG_DEBUG, "namespace: '%s' committing...", name.c_str() );
type.commit(); value.commit(); func.commit();
}

// rollback the maps
void rollback() {
EM_log( CK_LOG_FINER, "rolling back namespace: '%s'...", name.c_str() );
EM_log( CK_LOG_DEBUG, "namespace: '%s' rolling back...", name.c_str() );
type.rollback(); value.rollback(); func.rollback();
}

Expand Down Expand Up @@ -752,6 +757,10 @@ struct Chuck_Env : public Chuck_VM_Object
Chuck_Namespace * nspc_top();
// get type at top of type stack
Chuck_Type * class_top();
// commit namespaces
void commit_namespaces();
// rollback namespace
void rollback_namespaces();

public:
// REFACTOR-2017: carrier and accessors
Expand Down

0 comments on commit 8462a3f

Please sign in to comment.