⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bwb_ops.c

📁 这是一个简易的basic语言解释器, 可供我们学习和改进.
💻 C
📖 第 1 页 / 共 4 页
字号:
   /* decrement the stack twice */   op_pulldown( 2 );   return TRUE;   }/***************************************************************	FUNCTION:       op_or()	DESCRIPTION:    This function compares two integers and			performs a logical OR on them.***************************************************************/#if ANSI_Cstatic intop_or( int level, int precision )#elsestatic intop_or( level, precision )   int level;   int precision;#endif   {   switch( precision )      {      case STRING:         /* both sides of the operation should be numbers for            logical comparison; if not, report an error */#if PROG_ERRORS         sprintf( bwb_ebuf, "Strings cannot be compared logically." );         bwb_error( bwb_ebuf );#else         bwb_error( err_mismatch );#endif         break;      case NUMBER:         CURTASK exps[ level - 1 ].nval            = (bnumber) ((int) exp_getnval( &( CURTASK exps[ level - 1 ] ))            | (int) exp_getnval( &( CURTASK exps[ level + 1 ] )));         break;      }   /* set variable type to integer */   CURTASK exps[ level - 1 ].type = NUMBER;   CURTASK exps[ level - 1 ].operation = NUMBER;   /* decrement the stack twice */   op_pulldown( 2 );   return TRUE;   }/***************************************************************	FUNCTION:       op_and()	DESCRIPTION:    This function compares two integers and			performs a logical AND on them.***************************************************************/#if ANSI_Cstatic intop_and( int level, int precision )#elsestatic intop_and( level, precision )   int level;   int precision;#endif   {   switch( precision )      {      case STRING:         /* both sides of the operation should be numbers for            logical comparison; if not, report an error */#if PROG_ERRORS         sprintf( bwb_ebuf, "Strings cannot be compared logically." );         bwb_error( bwb_ebuf );#else         bwb_error( err_mismatch );#endif         break;      case NUMBER:         CURTASK exps[ level - 1 ].nval            = (bnumber) ((int) exp_getnval( &( CURTASK exps[ level - 1 ] ))            & (int) exp_getnval( &( CURTASK exps[ level + 1 ] )));         break;      }   /* set variable type to integer */   CURTASK exps[ level - 1 ].type = NUMBER;   CURTASK exps[ level - 1 ].operation = NUMBER;   /* decrement the stack twice */   op_pulldown( 2 );   return TRUE;   }/***************************************************************	FUNCTION:       op_not()	DESCRIPTION:    This function performs a logical NOT on			the integer to the right.***************************************************************/#if ANSI_Cstatic intop_not( int level, int precision )#elsestatic intop_not( level, precision )   int level;   int precision;#endif   {   switch( precision )      {      case STRING:         /* both sides of the operation should be numbers for            logical comparison; if not, report an error */#if PROG_ERRORS         sprintf( bwb_ebuf, "Strings cannot be compared logically." );         bwb_error( bwb_ebuf );#else         bwb_error( err_mismatch );#endif         break;      default:#if INTENSIVE_DEBUG         sprintf( bwb_ebuf, "in op_not(): argument is <%d>, precision <%c>",            (unsigned int) exp_getnval( &( CURTASK exps[ level + 1 ] )), precision );         bwb_debug( bwb_ebuf );#endif         CURTASK exps[ level ].nval = (bnumber)            ~( (int) exp_getnval( &( CURTASK exps[ level + 1 ] )) );#if INTENSIVE_DEBUG         sprintf( bwb_ebuf, "in op_not(): result is <%d>, precision <%c>",            (unsigned int) exp_getnval( &( CURTASK exps[ level ] )), precision );         bwb_debug( bwb_ebuf );#endif         break;      }   /* set variable type to integer */   CURTASK exps[ level ].type = NUMBER;   CURTASK exps[ level ].operation = NUMBER;   /* decrement the stack once */   op_pulldown( 1 );#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in op_not(): CURTASK expsc <%d>, level <%d> result <%d>",      CURTASK expsc, level, CURTASK exps[ CURTASK expsc ].nval );   bwb_debug( bwb_ebuf );#endif   return TRUE;   }/***************************************************************	FUNCTION:       op_xor()	DESCRIPTION:    This function compares two integers and			performs a logical XOR on them.***************************************************************/#if ANSI_Cstatic intop_xor( int level, int precision )#elsestatic intop_xor( level, precision )   int level;   int precision;#endif   {   switch( precision )      {      case STRING:         /* both sides of the operation should be numbers for            logical comparison; if not, report an error */#if PROG_ERRORS         sprintf( bwb_ebuf, "Strings cannot be compared logically." );         bwb_error( bwb_ebuf );#else         bwb_error( err_mismatch );#endif         break;      case NUMBER:         CURTASK exps[ level - 1 ].nval            = (bnumber) ((int) exp_getnval( &( CURTASK exps[ level - 1 ] ))            ^ (int) exp_getnval( &( CURTASK exps[ level + 1 ] )));         break;      }   /* set variable type to integer */   CURTASK exps[ level - 1 ].type = NUMBER;   CURTASK exps[ level - 1 ].operation = NUMBER;   /* decrement the stack twice */   op_pulldown( 2 );   return TRUE;   }/***************************************************************	FUNCTION:       op_negation()	DESCRIPTION:    This function performs a negation on the			element to the right.			Added by JBV 10/95***************************************************************/#if ANSI_Cstatic intop_negation( int level, int precision )#elsestatic intop_negation( level, precision )   int level;   int precision;#endif   {   switch( precision )      {      case STRING:         /* both sides of the operation should be numbers for            logical comparison; if not, report an error */#if PROG_ERRORS         sprintf( bwb_ebuf, "Strings cannot be compared logically." );         bwb_error( bwb_ebuf );#else         bwb_error( err_mismatch );#endif         break;      default:#if INTENSIVE_DEBUG         sprintf( bwb_ebuf, "in op_negation(): argument is <%f>, precision <%c>",            exp_getnval( &( CURTASK exps[ level + 1 ] )), precision );         bwb_debug( bwb_ebuf );#endif         CURTASK exps[ level ].nval = (bnumber)            -( exp_getnval( &( CURTASK exps[ level + 1 ] )) );#if INTENSIVE_DEBUG         sprintf( bwb_ebuf, "in op_negation(): result is <%f>, precision <%c>",            exp_getnval( &( CURTASK exps[ level ] )), precision );         bwb_debug( bwb_ebuf );#endif         break;      }   /* set variable type to requested precision (JBV) */   CURTASK exps[ level ].type = (char) precision;   CURTASK exps[ level ].operation = NUMBER;   /* decrement the stack once */   op_pulldown( 1 );#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in op_negation(): CURTASK expsc <%d>, level <%d> result <%f>",      CURTASK expsc, level, CURTASK exps[ CURTASK expsc ].nval );   bwb_debug( bwb_ebuf );#endif   return TRUE;   }/***************************************************************	FUNCTION:       op_islevelstr()	DESCRIPTION:    This function determines whether the			operation at a specified level involves a			string constant or variable.***************************************************************/#if ANSI_Cstatic intop_islevelstr( int level )#elsestatic intop_islevelstr( level )   int level;#endif   {   /* first see if the level holds a string constant */   if ( CURTASK exps[ level ].operation == CONST_STRING )      {#if INTENSIVE_DEBUG      sprintf( bwb_ebuf, "in op_islevelstr(): string detected at level <%d>.",         level );      bwb_debug( bwb_ebuf );#endif      return TRUE;      }   /* see if the level holds a string variable */   if ( CURTASK exps[ level ].operation == VARIABLE )      {      if ( CURTASK exps[ level ].xvar->type == STRING )         {#if INTENSIVE_DEBUG         sprintf( bwb_ebuf, "in op_islevelstr(): string detected at level <%d>.",            level );         bwb_debug( bwb_ebuf );#endif         return TRUE;         }      }   /* test has failed, return FALSE */#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in op_islevelstr(): string not detected at level <%d>.",      level );   bwb_debug( bwb_ebuf );#endif   return FALSE;   }/***************************************************************	FUNCTION:       op_getprecision()	DESCRIPTION:    This function finds the precision for			an operation by comparing the precision			at this level and that two levels below.***************************************************************/#if ANSI_Cstatic intop_getprecision( int level )#elsestatic intop_getprecision( level )   int level;#endif   {   /* first test for string value */   if (  ( CURTASK exps[ level + 1 ].type == STRING )      || ( CURTASK exps[ level - 1 ].type == STRING ) )      {      return STRING;      }   /* Both are numbers, so we should be able to find a suitable      precision level by starting with the top and moving down;      check first for double precision */   else      {      return NUMBER;      }   }/***************************************************************	FUNCTION:       op_pulldown()	DESCRIPTION:    This function pulls the expression stack			down a specified number of levels, decrementing			the expression stack counter (bycalling dec_esc())			and decrementing the current "level" of operation			processing.***************************************************************/#if ANSI_Cstatic intop_pulldown( int how_far )#elsestatic intop_pulldown( how_far )   int how_far;#endif   {   int level;   register int c;#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in op_pulldown(): pull down e stack <%d> place(s)",      how_far );   bwb_debug( bwb_ebuf );#endif   /* first pull down the actual variables themselves */   level = op_level + ( 2 - how_far );   while ( CURTASK expsc >= ( level + how_far ) )      {      /*------------------------------------------------------*/      /* But before memcpy, deallocate sbuffer for level, and */      /* afterwards, set sbuffer for level + how_far to NULL! */      /* Else konfusion reigns the next time around... (JBV)  */      /*------------------------------------------------------*/      if( CURTASK exps[ level ].sval.sbuffer != NULL ) /* JBV */         FREE( CURTASK exps[ level ].sval.sbuffer, "op_pulldown" );      memcpy( &CURTASK exps[ level ], &CURTASK exps[ level + how_far ],         (size_t) ( sizeof( struct exp_ese )) );      CURTASK exps[ level + how_far ].sval.sbuffer = NULL; /* JBV */      ++level;      }   /* decrement the expression stack counter */   for ( c = 0; c < how_far; ++c )      {      if ( dec_esc() == TRUE )         {         --op_level;         }      else         {         return FALSE;         }      }   return TRUE;   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -