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

📄 bwb_exp.c

📁 这是一个简易的basic语言解释器, 可供我们学习和改进.
💻 C
📖 第 1 页 / 共 3 页
字号:
/***************************************************************	FUNCTION:       exp_isfn()	DESCRIPTION:    This function reads the expression to find			if a function name is present at this point.***************************************************************/#if ANSI_Cintexp_isfn( char *expression )#elseintexp_isfn( expression )   char *expression;#endif   {   /* Block out the call to exp_getvfname() if exp_isvn() is called      after exp_isfn() */   exp_getvfname( expression, CURTASK exps[ CURTASK expsc ].string );#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in exp_isfn(): search for function <%s>",      expression );   bwb_debug( bwb_ebuf );#endif   if ( fnc_find( CURTASK exps[ CURTASK expsc ].string ) == NULL )      {#if INTENSIVE_DEBUG      sprintf( bwb_ebuf, "in exp_isfn(): failed to find function <%s>",         expression );      bwb_debug( bwb_ebuf );#endif      return OP_NULL;      }   else      {#if INTENSIVE_DEBUG      sprintf( bwb_ebuf, "in exp_isfn(): found function <%s>",         expression );      bwb_debug( bwb_ebuf );#endif      return FUNCTION;      }   }/***************************************************************	FUNCTION:       exp_isvn()	DESCRIPTION:    This function reads the expression to find			if a variable name at this point.***************************************************************/#if ANSI_Cintexp_isvn( char *expression )#elseintexp_isvn( expression )   char *expression;#endif   {   /* Block out the call to exp_getvfname() if exp_isfn() is called      after exp_isvn() */   /* exp_getvfname( expression, CURTASK exps[ CURTASK expsc ].string ); */   /* rule out null name */   if ( strlen( CURTASK exps[ CURTASK expsc ].string ) == 0 )      {      return OP_NULL;      }#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in exp_isvn(): search for variable <%s>",      CURTASK exps[ CURTASK expsc ].string );   bwb_debug( bwb_ebuf );#endif   if ( var_find( CURTASK exps[ CURTASK expsc ].string ) == NULL )      {#if INTENSIVE_DEBUG      sprintf( bwb_ebuf, "in exp_isvn(): failed to find variable <%s>",         expression );      bwb_debug( bwb_ebuf );#endif      return OP_NULL;      }   else      {#if INTENSIVE_DEBUG      sprintf( bwb_ebuf, "in exp_isvn(): found variable <%s>",         CURTASK exps[ CURTASK expsc ].string );      bwb_debug( bwb_ebuf );#endif      return VARIABLE;      }   }/***************************************************************	FUNCTION:       exp_getvfname()	DESCRIPTION:    This function reads the expression to find			a variable or function name at this point.***************************************************************/#if ANSI_Cintexp_getvfname( char *source, char *destination )#elseintexp_getvfname( source, destination )   char *source;   char *destination;#endif   {   int s_pos, d_pos;                    /* source, destination positions */#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in exp_getvfname(): source buffer <%s>", source );   bwb_debug( bwb_ebuf );#endif   s_pos = d_pos = 0;   destination[ 0 ] = '\0';   while( source[ s_pos ] != '\0' )      {      /* all alphabetical characters are acceptable */      if ( isalpha( source[ s_pos ] ) != 0 )         {         destination[ d_pos ] = source[ s_pos ];         ++d_pos;         ++s_pos;         destination[ d_pos ] = '\0';         }      /* numerical characters are acceptable but not in the first position */      else if (( isdigit( source[ s_pos ] ) != 0 ) && ( d_pos != 0 ))         {         destination[ d_pos ] = source[ s_pos ];         ++d_pos;         ++s_pos;         destination[ d_pos ] = '\0';         }      /* other characters will have to be tried on their own merits */      else         {         switch( source[ s_pos ] )            {            case '.':                           /* tolerated non-alphabetical characters */            case '_':               destination[ d_pos ] = source[ s_pos ];               ++d_pos;               ++s_pos;               destination[ d_pos ] = '\0';               break;	    case STRING:                        /* terminating characters */	    case '#':                           /* Microsoft-type double precision */	    case '!':                           /* Microsoft-type single precision */	       destination[ d_pos ] = source[ s_pos ];               ++d_pos;               ++s_pos;               destination[ d_pos ] = '\0';               return TRUE;            case '(':				/* begin function/sub name */               return TRUE;            default:                            /* anything else is non-tolerated */               return FALSE;            }         }      }#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in exp_getvfname(): found name <%s>", destination );   bwb_debug( bwb_ebuf );#endif   return TRUE;                         /* exit after coming to the end */   }/***************************************************************	FUNCTION:       exp_validarg()	DESCRIPTION:    This function reads the expression to			determine whether it is a valid argument (to be			read recursively by bwb_exp() and passed to a			function.***************************************************************/#if ANSI_Cintexp_validarg( char *expression )#elseintexp_validarg( expression )   char *expression;#endif   {   register int c;#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in exp_validarg(): expression <%s>.",      expression );   bwb_debug( bwb_ebuf );#endif   c = 0;   while ( TRUE )      {      switch( expression[ c ] )         {         case ' ':         case '\t':            ++c;            break;         case '\0':            return FALSE;         default:            return TRUE;         }      }   }/***************************************************************        FUNCTION:   	exp_getnval()	DESCRIPTION:    This function returns the numerical value			contain in the expression-stack element			pointed to by 'e'.***************************************************************/#if ANSI_Cbnumberexp_getnval( struct exp_ese *e )#elsebnumberexp_getnval( e )   struct exp_ese *e;#endif   {   /* check for variable */   if ( e->operation == VARIABLE )      {      switch( e->type )         {         case NUMBER:            return (* var_findnval( e->xvar, e->array_pos ));         default:            bwb_error( err_mismatch );            return (bnumber) 0.0;         }      }   /* must be a numerical value */   if ( e->operation != NUMBER )      {#if PROG_ERRORS      sprintf( bwb_ebuf, "in exp_getnval(): operation <%d> is not a number",         e->operation );      bwb_error( bwb_ebuf );#else      bwb_error( err_syntax );#endif      return (bnumber) 0.0;      }   /* return specific values */   switch( e->type )      {      case NUMBER:         return e->nval;      default:#if PROG_ERRORS         sprintf( bwb_ebuf, "in exp_getnval(): type is <%c>",            e->type );         bwb_error( bwb_ebuf );#else         bwb_error( err_syntax );#endif         return (bnumber) 0.0;      }   }/***************************************************************	FUNCTION:       exp_getsval()	DESCRIPTION:    This function returns a pointer to the			BASIC string structure pointed to by			expression-stack element 'e'.***************************************************************/#if ANSI_Cbstring *exp_getsval( struct exp_ese *e )#elsebstring *exp_getsval( e )   struct exp_ese *e;#endif   {   static bstring b;#if TEST_BSTRING   static int init = FALSE;   if ( init == FALSE )      {      sprintf( b.name, "<exp_getsval() bstring>" );      }#endif   b.rab = FALSE;   /* return based on operation type */   switch( e->operation )      {      case CONST_STRING:      case OP_STRJOIN:         return &( e->sval );      case VARIABLE:        switch( e->type )            {	    case STRING:               return var_findsval( e->xvar, e->array_pos );            case NUMBER:               sprintf( bwb_ebuf, "%lf ", (double) exp_getnval( e ) );               str_ctob( &b, bwb_ebuf );               return &b;            default:#if PROG_ERRORS               sprintf( bwb_ebuf, "in exp_getsval(): type <%c> inappropriate for NUMBER",                  e->type );               bwb_error( bwb_ebuf );#else               bwb_error( err_syntax );#endif               return NULL;            }	 break;      case NUMBER:        switch( e->type )            {	    case NUMBER:               sprintf( bwb_ebuf, "%lf ", (double) exp_getnval( e ) );               str_ctob( &b, bwb_ebuf );               return &b;            default:#if PROG_ERRORS               sprintf( bwb_ebuf, "in exp_getsval(): type <%c> inappropriate for NUMBER",                  e->type );               bwb_error( bwb_ebuf );#else               bwb_error( err_syntax );#endif               return NULL;            }	 break;      default:#if PROG_ERRORS         sprintf( bwb_ebuf, "in exp_getsval(): operation <%d> inappropriate",            e->operation );         bwb_error( bwb_ebuf );#else         bwb_error( err_syntax );#endif         return NULL;      }   /* this point may not be reached */   return NULL;   }/***************************************************************	FUNCTION:       inc_esc()	DESCRIPTION:    This function increments the expression			stack counter.***************************************************************/#if ANSI_Cintinc_esc( void )#elseintinc_esc()#endif   {#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in inc_esc(): prev level <%d>",      CURTASK expsc );   bwb_debug ( bwb_ebuf );#endif   ++CURTASK expsc;   if ( CURTASK expsc >= ESTACKSIZE )      {      --CURTASK expsc;#if PROG_ERRORS      sprintf( bwb_ebuf, "in inc_esc(): Maximum expression stack exceeded <%d>",         CURTASK expsc );      bwb_error( bwb_ebuf );#else      bwb_error( err_overflow );#endif      return OP_NULL;      }#if INTENSIVE_DEBUG   sprintf( CURTASK exps[ CURTASK expsc ].string, "New Expression Stack Level %d", CURTASK expsc );#endif   CURTASK exps[ CURTASK expsc ].type = NUMBER;   CURTASK exps[ CURTASK expsc ].operation = OP_NULL;   CURTASK exps[ CURTASK expsc ].pos_adv = 0;   return TRUE;   }/***************************************************************	FUNCTION:       dec_esc()	DESCRIPTION:    This function decrements the expression			stack counter.***************************************************************/#if ANSI_Cintdec_esc( void )#elseintdec_esc()#endif   {   --CURTASK expsc;   if ( CURTASK expsc < 0 )      {      CURTASK expsc = 0;#if PROG_ERRORS      sprintf( bwb_ebuf, "in dec_esc(): Expression stack counter < 0." );      bwb_error( bwb_ebuf );#else      bwb_error( err_overflow );#endif      return OP_NULL;      }   return TRUE;   }

⌨️ 快捷键说明

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