Skip to content

Commit

Permalink
update string operations to use overloads; fix associated reference c…
Browse files Browse the repository at this point in the history
…ounting
  • Loading branch information
gewang committed Nov 16, 2024
1 parent 17c4b05 commit 4db3cb4
Show file tree
Hide file tree
Showing 13 changed files with 660 additions and 241 deletions.
4 changes: 2 additions & 2 deletions examples/string/strops.ck
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ assert( "foo" + "bar" == "foobar", "16" );
"bar" +=> s;
assert( s == "foobar", "17" );
assert( "bar" + 10 == "bar10", "18" );
assert( "bar" + 10.0 == "bar10.0000", "19" );
assert( "bar" + 10.0 == "bar10.000000", "19" );
assert( 10 + "bar" == "10bar", "20" );
assert( 10.0 + "bar" == "10.0000bar", "21" );
assert( 10.0 + "bar" == "10.000000bar", "21" );
assert( "foo" + "bar" + "cle" == "foobarcle", "22" );
assert( "FoO".lower() == "foo", "23" );
assert( "foo".upper() == "FOO", "24" );
Expand Down
34 changes: 34 additions & 0 deletions src/core/chuck_dl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,40 @@ Chuck_DL_Value * CK_DLL_CALL make_new_mvar( const char * t, const char * n, t_CK
{ return new Chuck_DL_Value( t, n, c ); }
Chuck_DL_Value * CK_DLL_CALL make_new_svar( const char * t, const char * n, t_CKBOOL c, void * a )
{ return new Chuck_DL_Value( t, n, c, a ); }
Chuck_DL_Func * make_new_op_binary( const char * t, ae_Operator op, f_gfun gfun )
{
// allocate
Chuck_DL_Func * f = new Chuck_DL_Func( t, (string("@operator")+op2str(op)).c_str(), (t_CKUINT)gfun, ae_fp_gfun );
// set operator overload kind
f->opOverloadKind = ckte_op_overload_BINARY;
// set the op
f->op2overload = op;
// return
return f;
}
Chuck_DL_Func * make_new_op_prefix( const char * t, ae_Operator op, f_gfun gfun )
{
// allocate
Chuck_DL_Func * f = new Chuck_DL_Func( t, (string("@operator")+op2str(op)).c_str(), (t_CKUINT)gfun, ae_fp_gfun );
// set operator overload kind
f->opOverloadKind = ckte_op_overload_UNARY_PRE;
// set the op
f->op2overload = op;
// return
return f;
}
Chuck_DL_Func * make_new_op_postfix( const char * t, ae_Operator op, f_gfun gfun )
{
// allocate
Chuck_DL_Func * f = new Chuck_DL_Func( t, (string("@operator")+op2str(op)).c_str(), (t_CKUINT)gfun, ae_fp_gfun );
// set operator overload kind
f->opOverloadKind = ckte_op_overload_UNARY_POST;
// set the op
f->op2overload = op;
// return
return f;
}




Expand Down
5 changes: 4 additions & 1 deletion src/core/chuck_dl.h
Original file line number Diff line number Diff line change
Expand Up @@ -883,11 +883,14 @@ struct Chuck_DL_Arg


//------------------------------------------------------------------------------
// alternative functions to make stuff
// alternative functions to make stuff`
//------------------------------------------------------------------------------
Chuck_DL_Func * make_new_ctor( f_ctor ctor );
Chuck_DL_Func * make_new_mfun( const char * t, const char * n, f_mfun mfun );
Chuck_DL_Func * make_new_sfun( const char * t, const char * n, f_sfun sfun );
Chuck_DL_Func * make_new_op_binary( const char * t, ae_Operator op, f_gfun gfun );
Chuck_DL_Func * make_new_op_prefix( const char * t, ae_Operator op, f_gfun gfun );
Chuck_DL_Func * make_new_op_postfix( const char * t, ae_Operator op, f_gfun gfun );
Chuck_DL_Value * make_new_arg( const char * t, const char * n );
Chuck_DL_Value * make_new_mvar( const char * t, const char * n, t_CKBOOL c = FALSE );
Chuck_DL_Value * make_new_svar( const char * t, const char * n, t_CKBOOL c, void * a );
Expand Down
127 changes: 38 additions & 89 deletions src/core/chuck_emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2116,57 +2116,6 @@ t_CKBOOL emit_engine_emit_op( Chuck_Emitter * emit, ae_Operator op, a_Exp lhs, a
{
emit->append( instr = new Chuck_Instr_Add_double );
}
// string + string
else if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) )
{
// concatenate
emit->append( instr = new Chuck_Instr_Add_string );
instr->set_linepos( lhs->line );
}
// left: string
else if( isa( t_left, emit->env->ckt_string ) )
{
// + int
if( isa( t_right, emit->env->ckt_int ) )
{
emit->append( instr = new Chuck_Instr_Add_string_int );
instr->set_linepos( lhs->line );
}
else if( isa( t_right, emit->env->ckt_float ) )
{
emit->append( instr = new Chuck_Instr_Add_string_float );
instr->set_linepos( lhs->line );
}
else
{
EM_error2( lhs->where,
"(emit): internal error: unhandled op '%s' %s '%s'",
t_left->c_name(), op2str( op ), t_right->c_name() );
return FALSE;
}
}
// right: string
else if( isa( t_right, emit->env->ckt_string ) )
{
// + int
if( isa( t_left, emit->env->ckt_int ) )
{
emit->append( instr = new Chuck_Instr_Add_int_string );
instr->set_linepos( rhs->line );
}
else if( isa( t_left, emit->env->ckt_float ) )
{
emit->append( instr = new Chuck_Instr_Add_float_string );
instr->set_linepos( rhs->line );
}
else
{
EM_error2( lhs->where,
"(emit): internal error: unhandled op '%s' %s '%s'",
t_left->c_name(), op2str( op ), t_right->c_name() );
return FALSE;
}
}
else // other types
{
switch( left )
Expand Down Expand Up @@ -2210,13 +2159,13 @@ t_CKBOOL emit_engine_emit_op( Chuck_Emitter * emit, ae_Operator op, a_Exp lhs, a
emit->append( instr = new Chuck_Instr_Time_Advance );
instr->set_linepos( lhs->line );
}
// time + dur
// time +=> dur
else if( ( left == te_dur && right == te_time ) ||
( left == te_time && right == te_dur ) )
{
emit->append( instr = new Chuck_Instr_Add_double_Assign );
}
// string + string
// string +=> string
else if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) )
{
// concatenate
Expand Down Expand Up @@ -2780,13 +2729,13 @@ t_CKBOOL emit_engine_emit_op( Chuck_Emitter * emit, ae_Operator op, a_Exp lhs, a

// -------------------------------- bool -----------------------------------
case ae_op_eq:
if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string )
&& !isa( t_left, emit->env->ckt_null ) && !isa( t_right, emit->env->ckt_null ) ) // !null
{
emit->append( instr = new Chuck_Instr_Op_string( op ) );
instr->set_linepos( lhs->line );
}
else if( ( isa( t_left, emit->env->ckt_object ) && isa( t_right, emit->env->ckt_object ) ) ||
// if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string )
// && !isa( t_left, emit->env->ckt_null ) && !isa( t_right, emit->env->ckt_null ) ) // !null
// {
// emit->append( instr = new Chuck_Instr_Op_string( op ) );
// instr->set_linepos( lhs->line );
// } else
if( ( isa( t_left, emit->env->ckt_object ) && isa( t_right, emit->env->ckt_object ) ) ||
( isa( t_left, emit->env->ckt_object ) && isnull( emit->env, t_right ) ) ||
( isnull( emit->env,t_left ) && isa( t_right, emit->env->ckt_object ) ) ||
( isnull( emit->env,t_left ) && isnull( emit->env, t_right ) ) )
Expand Down Expand Up @@ -2826,14 +2775,14 @@ t_CKBOOL emit_engine_emit_op( Chuck_Emitter * emit, ae_Operator op, a_Exp lhs, a
break;

case ae_op_neq:
if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string )
&& !isa( t_left, emit->env->ckt_null ) && !isa( t_right, emit->env->ckt_null ) ) // !null
// added 1.3.2.0 (spencer)
{
emit->append( instr = new Chuck_Instr_Op_string( op ) );
instr->set_linepos( lhs->line );
}
else if( ( isa( t_left, emit->env->ckt_object ) && isa( t_right, emit->env->ckt_object ) ) ||
// if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string )
// && !isa( t_left, emit->env->ckt_null ) && !isa( t_right, emit->env->ckt_null ) ) // !null
// // added 1.3.2.0 (spencer)
// {
// emit->append( instr = new Chuck_Instr_Op_string( op ) );
// instr->set_linepos( lhs->line );
// } else
if( ( isa( t_left, emit->env->ckt_object ) && isa( t_right, emit->env->ckt_object ) ) ||
( isa( t_left, emit->env->ckt_object ) && isnull( emit->env, t_right ) ) ||
( isnull( emit->env,t_left ) && isa( t_right, emit->env->ckt_object ) ) ||
( isnull( emit->env,t_left ) && isnull( emit->env, t_right ) ) )
Expand Down Expand Up @@ -2884,11 +2833,11 @@ t_CKBOOL emit_engine_emit_op( Chuck_Emitter * emit, ae_Operator op, a_Exp lhs, a
break;

default:
if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) )
{
emit->append( instr = new Chuck_Instr_Op_string( op ) );
instr->set_linepos( lhs->line );
}
// if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) )
// {
// emit->append( instr = new Chuck_Instr_Op_string( op ) );
// instr->set_linepos( lhs->line );
// }
break;
}
break;
Expand All @@ -2906,12 +2855,12 @@ t_CKBOOL emit_engine_emit_op( Chuck_Emitter * emit, ae_Operator op, a_Exp lhs, a
break;

default:
if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) )
{
emit->append( instr = new Chuck_Instr_Op_string( op ) );
instr->set_linepos( lhs->line );
}
else if( isa( t_left, emit->env->ckt_io ) )
// if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) )
// {
// emit->append( instr = new Chuck_Instr_Op_string( op ) );
// instr->set_linepos( lhs->line );
// } else
if( isa( t_left, emit->env->ckt_io ) )
{
// output
if( isa( t_right, emit->env->ckt_int ) )
Expand Down Expand Up @@ -2972,11 +2921,11 @@ t_CKBOOL emit_engine_emit_op( Chuck_Emitter * emit, ae_Operator op, a_Exp lhs, a
break;

default:
if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) )
{
emit->append( instr = new Chuck_Instr_Op_string( op ) );
instr->set_linepos( lhs->line );
}
// if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) )
// {
// emit->append( instr = new Chuck_Instr_Op_string( op ) );
// instr->set_linepos( lhs->line );
// }
break;
}
break;
Expand All @@ -2994,11 +2943,11 @@ t_CKBOOL emit_engine_emit_op( Chuck_Emitter * emit, ae_Operator op, a_Exp lhs, a
break;

default:
if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) )
{
emit->append( instr = new Chuck_Instr_Op_string( op ) );
instr->set_linepos( lhs->line );
}
// if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) )
// {
// emit->append( instr = new Chuck_Instr_Op_string( op ) );
// instr->set_linepos( lhs->line );
// }
break;
}
break;
Expand Down
Loading

0 comments on commit 4db3cb4

Please sign in to comment.