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

📄 bwb_ops.c

📁 这是一个简易的basic语言解释器, 可供我们学习和改进.
💻 C
📖 第 1 页 / 共 4 页
字号:
               CURTASK exps[ level - 1 ].nval = (bnumber) TRUE;               }            else               {               CURTASK exps[ level - 1 ].nval = (bnumber) FALSE;               }            }         break;      case NUMBER:         if ( exp_getnval( &( CURTASK exps[ level - 1 ] ))            > exp_getnval( &( CURTASK exps[ level + 1 ] )) )            {            CURTASK exps[ level - 1 ].nval = (bnumber) TRUE;            }         else            {            CURTASK exps[ level - 1 ].nval = (bnumber) FALSE;            }         break;      }   /* set variable to integer and operation to NUMBER:      this must be done at the end, since at the beginning it      might cause op_islevelstr() to return a false error */   CURTASK exps[ level - 1 ].type = NUMBER;   CURTASK exps[ level - 1 ].operation = NUMBER;   /* decrement the stack */   op_pulldown( 2 );   return TRUE;   }/***************************************************************	FUNCTION:       op_lteq()	DESCRIPTION:    This function compares two values and			returns an integer value: TRUE if the			left hand value is less than or equal			to the right, and FALSE if it is not.***************************************************************/#if ANSI_Cstatic intop_lteq( int level, int precision )#elsestatic intop_lteq( level, precision )   int level;   int precision;#endif   {   int error_condition;   error_condition = FALSE;   switch( precision )      {      case STRING:         /* both sides of the operation should be numbers for            string addition; if not, report an error */         if (  ( op_islevelstr( level - 1 ) != TRUE )            || ( op_islevelstr( level + 1 ) != TRUE ) )            {#if PROG_ERRORS            sprintf( bwb_ebuf, "Type mismatch in string comparison." );            bwb_error( bwb_ebuf );#else            bwb_error( err_mismatch );#endif            error_condition = TRUE;            }         /* compare the two strings */         if ( error_condition == FALSE )            {            if ( str_cmp( exp_getsval( &( CURTASK exps[ level - 1 ] )),               exp_getsval( &( CURTASK exps[ level + 1 ] )) ) <= 0 )               {               CURTASK exps[ level - 1 ].nval = (bnumber) TRUE;               }            else               {               CURTASK exps[ level - 1 ].nval = (bnumber) FALSE;               }            }         break;      case NUMBER:         if ( exp_getnval( &( CURTASK exps[ level - 1 ] ))            <= exp_getnval( &( CURTASK exps[ level + 1 ] )) )            {            CURTASK exps[ level - 1 ].nval = (bnumber) TRUE;            }         else            {            CURTASK exps[ level - 1 ].nval = (bnumber) FALSE;            }         break;      }   /* set variable to integer and operation to NUMBER:      this must be done at the end, since at the beginning it      might cause op_islevelstr() to return a false error */   CURTASK exps[ level - 1 ].type = NUMBER;   CURTASK exps[ level - 1 ].operation = NUMBER;   /* decrement the stack */   op_pulldown( 2 );   return TRUE;   }/***************************************************************	FUNCTION:       op_gteq()	DESCRIPTION:    This function compares two values and			returns an integer value: TRUE if the			left hand value is greater than or equal			to the right, and FALSE if it is not.***************************************************************/#if ANSI_Cstatic intop_gteq( int level, int precision )#elsestatic intop_gteq( level, precision )   int level;   int precision;#endif   {   int error_condition;   error_condition = FALSE;   switch( precision )      {      case STRING:         /* both sides of the operation should be numbers for            string addition; if not, report an error */         if (  ( op_islevelstr( level - 1 ) != TRUE )            || ( op_islevelstr( level + 1 ) != TRUE ) )            {#if PROG_ERRORS            sprintf( bwb_ebuf, "Type mismatch in string comparison." );            bwb_error( bwb_ebuf );#else            bwb_error( err_mismatch );#endif            error_condition = TRUE;            }         /* compare the two strings */         if ( error_condition == FALSE )            {            if ( str_cmp( exp_getsval( &( CURTASK exps[ level - 1 ] )),               exp_getsval( &( CURTASK exps[ level + 1 ] )) ) >= 0 )               {               CURTASK exps[ level - 1 ].nval = (bnumber) TRUE;               }            else               {               CURTASK exps[ level - 1 ].nval = (bnumber) FALSE;               }            }         break;      case NUMBER:         if ( exp_getnval( &( CURTASK exps[ level - 1 ] ))            >= exp_getnval( &( CURTASK exps[ level + 1 ] )) )            {            CURTASK exps[ level - 1 ].nval = (bnumber) TRUE;            }         else            {            CURTASK exps[ level - 1 ].nval = (bnumber) FALSE;            }         break;      }   /* set variable to integer and operation to NUMBER:      this must be done at the end, since at the beginning it      might cause op_islevelstr() to return a false error */   CURTASK exps[ level - 1 ].type = NUMBER;   CURTASK exps[ level - 1 ].operation = NUMBER;   /* decrement the stack */   op_pulldown( 2 );   return TRUE;   }/***************************************************************	FUNCTION:       op_notequal()	DESCRIPTION:    This function compares two values and			returns an integer value: TRUE if they			are not the same and FALSE if they are.***************************************************************/#if ANSI_Cstatic intop_notequal( int level, int precision )#elsestatic intop_notequal( level, precision )   int level;   int precision;#endif   {   int error_condition;   error_condition = FALSE;   switch( precision )      {      case STRING:         /* both sides of the operation should be numbers for            string addition; if not, report an error */         if (  ( op_islevelstr( level - 1 ) != TRUE )            || ( op_islevelstr( level + 1 ) != TRUE ) )            {#if PROG_ERRORS            sprintf( bwb_ebuf, "Type mismatch in string comparison." );            bwb_error( bwb_ebuf );#else            bwb_error( err_mismatch );#endif            error_condition = TRUE;            }         /* compare the two strings */         if ( error_condition == FALSE )            {            if ( str_cmp( exp_getsval( &( CURTASK exps[ level - 1 ] )),               exp_getsval( &( CURTASK exps[ level + 1 ] )) ) != 0 )               {               CURTASK exps[ level - 1 ].nval = (bnumber) TRUE;               }            else               {               CURTASK exps[ level - 1 ].nval = (bnumber) FALSE;               }            }         break;      case NUMBER:         if ( exp_getnval( &( CURTASK exps[ level - 1 ] ))            != exp_getnval( &( CURTASK exps[ level + 1 ] )) )            {            CURTASK exps[ level - 1 ].nval = (bnumber) TRUE;            }         else            {            CURTASK exps[ level - 1 ].nval = (bnumber) FALSE;            }         break;      }   /* set variable to integer and operation to NUMBER:      this must be done at the end, since at the beginning it      might cause op_islevelstr() to return a false error */   CURTASK exps[ level - 1 ].type = NUMBER;   CURTASK exps[ level - 1 ].operation = NUMBER;   /* decrement the stack */   op_pulldown( 2 );   return TRUE;   }/***************************************************************	FUNCTION:       op_modulus()	DESCRIPTION:    This function divides the number on			the left by the number on the right			and returns the remainder.***************************************************************/#if ANSI_Cstatic intop_modulus( int level, int precision )#elsestatic intop_modulus( level, precision )   int level;   int precision;#endif   {   static double iportion;   switch( precision )      {      case STRING:         /* both sides of the operation should be numbers for            string addition; if not, report an error */#if PROG_ERRORS         sprintf( bwb_ebuf, "Strings cannot be divided." );         bwb_error( bwb_ebuf );#else         bwb_error( err_syntax );#endif         break;      case NUMBER:         if ( exp_getnval( &( CURTASK exps[ level + 1 ] ))            == (bnumber) 0 )            {            CURTASK exps[ level - 1 ].nval = (bnumber) -1;            op_pulldown( 2 );#if PROG_ERRORS            sprintf( bwb_ebuf, "Divide by 0." );            bwb_error( bwb_ebuf );#else            bwb_error( err_dbz );#endif            return FALSE;            }         CURTASK exps[ level ].nval            = exp_getnval( &( CURTASK exps[ level - 1 ] ))            / exp_getnval( &( CURTASK exps[ level + 1 ] ));         modf( (double) CURTASK exps[ level ].nval, &iportion );         CURTASK exps[ level - 1 ].nval            = exp_getnval( &( CURTASK exps[ level - 1 ] ))            - ( exp_getnval( &( CURTASK exps[ level + 1 ] ))            * iportion );         break;      }   /* set variable to requested precision */   CURTASK exps[ level - 1 ].type = (char) precision;   CURTASK exps[ level - 1 ].operation = NUMBER;   /* decrement the stack twice */   op_pulldown( 2 );   return TRUE;   }/***************************************************************	FUNCTION:       op_exponent()	DESCRIPTION:    This function finds the exponential value			of a number (on the left) to the power			indicated on the right-hand side.***************************************************************/#if ANSI_Cstatic intop_exponent( int level, int precision )#elsestatic intop_exponent( level, precision )   int level;   int precision;#endif   {#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in op_exponent(): entered function level <%d>.",      level );   bwb_debug ( bwb_ebuf );#endif   switch( precision )      {      case STRING:         /* both sides of the operation should be numbers for            string addition; if not, report an error */#if PROG_ERRORS         sprintf( bwb_ebuf, "Strings cannot be taken as exponents." );         bwb_error( bwb_ebuf );#else         bwb_error( err_mismatch );#endif         break;      case NUMBER:         CURTASK exps[ level - 1 ].nval           = (bnumber) pow( (double) exp_getnval( &( CURTASK exps[ level - 1 ] )),                  (double) exp_getnval( &( CURTASK exps[ level + 1 ] )) );         break;      }   /* set variable to requested precision */   CURTASK exps[ level - 1 ].type = (char) precision;   CURTASK exps[ level - 1 ].operation = NUMBER;   /* decrement the stack twice */   op_pulldown( 2 );   return TRUE;   }/***************************************************************	FUNCTION:       op_intdiv()	DESCRIPTION:    This function divides the number on			the left by the number on the right,			returning the result as an integer.***************************************************************/#if ANSI_Cstatic intop_intdiv( int level, int precision )#elsestatic intop_intdiv( level, precision )   int level;   int precision;#endif   {   switch( precision )      {      case STRING:         /* both sides of the operation should be numbers for            string addition; if not, report an error */#if PROG_ERRORS         sprintf( bwb_ebuf, "Strings cannot be divided." );         bwb_error( bwb_ebuf );#else         bwb_error( err_mismatch );#endif         break;      default:         if ( exp_getnval( &( CURTASK exps[ level + 1 ] ))            == (bnumber) 0 )            {            CURTASK exps[ level - 1 ].nval = (bnumber) -1;            op_pulldown( 2 );#if PROG_ERRORS            sprintf( bwb_ebuf, "Divide by 0." );            bwb_error( bwb_ebuf );#else            bwb_error( err_dbz );#endif            return FALSE;            }         CURTASK exps[ level - 1 ].nval            = exp_getnval( &( CURTASK exps[ level - 1 ] ))            / exp_getnval( &( CURTASK exps[ level + 1 ] ));         break;      }   /* set variable to requested precision */   CURTASK exps[ level - 1 ].type = NUMBER;   CURTASK exps[ level - 1 ].operation = NUMBER;

⌨️ 快捷键说明

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