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

📄 bwb_ops.c

📁 这是一个简易的basic语言解释器, 可供我们学习和改进.
💻 C
📖 第 1 页 / 共 4 页
字号:
static intop_subtract( 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 subtracted." );         bwb_error( bwb_ebuf );#else         bwb_error( err_mismatch );#endif         break;      case NUMBER:         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 = (char) precision;   CURTASK exps[ level - 1 ].operation = NUMBER;   /* decrement the stack twice */   op_pulldown( 2 );   return TRUE;   }/***************************************************************	FUNCTION:       op_multiply()	DESCRIPTION:    This function multiplies the number on			the left by the number on the right.***************************************************************/#if ANSI_Cstatic intop_multiply( int level, int precision )#elsestatic intop_multiply( 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 multiplied." );         bwb_error( bwb_ebuf );#else         bwb_error( err_mismatch );#endif         break;      case NUMBER:         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 = (char) precision;   CURTASK exps[ level - 1 ].operation = NUMBER;   /* decrement the stack twice */   op_pulldown( 2 );   return TRUE;   }/***************************************************************	FUNCTION:       op_divide()	DESCRIPTION:    This function divides the number on			the left by the number on the right.***************************************************************/#if ANSI_Cstatic intop_divide( int level, int precision )#elsestatic intop_divide( level, precision )   int level;   int precision;#endif   {   switch( precision )      {      case STRING:         /* both sides of the operation should be numbers for            division; 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;      case NUMBER:         if ( exp_getnval( &( CURTASK exps[ level + 1 ] ))            == (bnumber) 0 )            {            CURTASK exps[ level - 1 ].nval = (bnumber) -1.0;            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 = (char) precision;   CURTASK exps[ level - 1 ].operation = NUMBER;   /* decrement the stack twice */   op_pulldown( 2 );   return TRUE;   }/***************************************************************	FUNCTION:       op_assign()	DESCRIPTION:    This function assigns the value in the			right hand side to the variable in the			left hand side.***************************************************************/#if ANSI_Cstatic intop_assign( int level, int precision )#elsestatic intop_assign( level, precision )   int level;   int precision;#endif   {   /* Make sure the position one level below is a variable */   if ( CURTASK exps[ level - 1 ].operation != VARIABLE )      {      op_pulldown( 2 );#if PROG_ERRORS      sprintf( bwb_ebuf, "in op_assign(): Assignment must be to variable: level -1 <%d> op <%d>",         level - 1, CURTASK exps[ level - 1 ].operation );      bwb_error( bwb_ebuf );#else      bwb_error( err_syntax );#endif      return FALSE;      }#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in op_assign(): entered function level <%d>",      level );   bwb_debug( bwb_ebuf );#endif  /* if the assignment is numerical, then the precision should be set     to that of the variable on the left-hand side of the assignment */   if ( precision != STRING )      {      precision = (int) CURTASK exps[ level - 1 ].type;      }   switch( precision )      {      case STRING:#if INTENSIVE_DEBUG         sprintf( bwb_ebuf, "in op_assign(): try exp_getsval(), level <%d> op <%d> type <%c>:",            level - 1, CURTASK exps[ level - 1 ].operation, CURTASK exps[ level - 1 ].type );         bwb_debug( bwb_ebuf );         exp_getsval( &( CURTASK exps[ level - 1 ] ));         sprintf( bwb_ebuf, "in op_assign(): try exp_getsval(), level <%d> op <%d> type <%c>:",            level + 1, CURTASK exps[ level + 1 ].operation, CURTASK exps[ level + 1 ].type );         bwb_debug( bwb_ebuf );         exp_getsval( &( CURTASK exps[ level + 1 ] ));         sprintf( bwb_ebuf, "in op_assign(): string addition, exp_getsval()s completed" );         bwb_debug( bwb_ebuf );#endif         str_btob( exp_getsval( &( CURTASK exps[ level - 1 ] )),                   exp_getsval( &( CURTASK exps[ level + 1 ] )) );         break;      case NUMBER:         * var_findnval( CURTASK exps[ level - 1 ].xvar,             CURTASK exps[ level - 1 ].array_pos ) =            CURTASK exps[ level - 1 ].nval =             exp_getnval( &( CURTASK exps[ level + 1 ] ) );         break;      default:#if PROG_ERRORS         sprintf( bwb_ebuf, "in op_assign(): Variable before assignment operator has unidentified type." );         bwb_error( bwb_ebuf );#else         bwb_error( err_mismatch );#endif         return FALSE;      }   /* set variable to requested precision */   CURTASK exps[ level - 1 ].type = (char) precision;   /* decrement the stack twice */   op_pulldown( 2 );   return TRUE;   }/***************************************************************	FUNCTION:       op_equals()	DESCRIPTION:    This function compares two values and			returns an integer value: TRUE if they are			the same and FALSE if they are not.***************************************************************/#if ANSI_Cstatic intop_equals( int level, int precision )#elsestatic intop_equals( level, precision )   int level;   int precision;#endif   {   int error_condition;   static bstring b;   bstring *bp;   error_condition = FALSE;   b.rab = FALSE;   switch( precision )      {      case STRING:         /* both sides of the operation should be strings 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, "in op_equals(): 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 )            {            bp = exp_getsval( &( CURTASK exps[ level - 1 ] ));#if OLDWAY	    b.length = bp->length;	    b.sbuffer = bp->sbuffer;#endif	    str_btob( &b, bp );            if ( str_cmp( &b,               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_lessthan()	DESCRIPTION:    This function compares two values and			returns an integer value: TRUE if the			left hand value is less than the right,			and FALSE if it is not.***************************************************************/#if ANSI_Cstatic intop_lessthan( int level, int precision )#elsestatic intop_lessthan( 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_greaterthan()	DESCRIPTION:    This function compares two values and			returns an integer value: TRUE if the			left hand value is greater than the right,			and FALSE if it is not.***************************************************************/#if ANSI_Cstatic intop_greaterthan( int level, int precision )#elsestatic intop_greaterthan( 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 )               {

⌨️ 快捷键说明

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