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

📄 bwb_elx.c

📁 这是一个简易的basic语言解释器, 可供我们学习和改进.
💻 C
📖 第 1 页 / 共 3 页
字号:
                  else                     {                     build_loop = FALSE;                     }                  break;               case '.':                        /* note at least single precision */               case '0':                        /* or ordinary digit */               case '1':               case '2':               case '3':               case '4':               case '5':               case '6':               case '7':               case '8':               case '9':                  CURTASK exps[ CURTASK expsc ].string[ s_pos ] = expression[ CURTASK exps[ CURTASK expsc ].pos_adv ];                  ++CURTASK exps[ CURTASK expsc ].pos_adv;  /* advance to next character */                  ++s_pos;                  CURTASK exps[ CURTASK expsc ].string[ s_pos ] = '\0';                  break;	       case '#':                        /* Microsoft-type precision indicator; ignored but terminates */	       case '!':                        /* Microsoft-type precision indicator; ignored but terminates */		  ++CURTASK exps[ CURTASK expsc ].pos_adv;  /* advance to next character */		  CURTASK exps[ CURTASK expsc ].type = NUMBER;		  exponent = FALSE;		  build_loop = FALSE;		  break;	       case 'E':                        /* exponential, single precision */               case 'e':                  ++CURTASK exps[ CURTASK expsc ].pos_adv;  /* advance to next character */                  CURTASK exps[ CURTASK expsc ].type = NUMBER;		  exponent = TRUE;                  build_loop = FALSE;		  break;               case 'D':                        /* exponential, double precision */               case 'd':                  ++CURTASK exps[ CURTASK expsc ].pos_adv;  /* advance to next character */                  CURTASK exps[ CURTASK expsc ].type = NUMBER;		  exponent = TRUE;                  build_loop = FALSE;                  break;               default:                         /* anything else, terminate */                  build_loop = FALSE;                  break;               }            }         /* assign the value to the mantissa variable */         #if NUMBER_DOUBLE         sscanf( CURTASK exps[ CURTASK expsc ].string, "%lf",            var_findnval( &mantissa, mantissa.array_pos ));#else         sscanf( CURTASK exps[ CURTASK expsc ].string, "%f",            var_findnval( &mantissa, mantissa.array_pos ));#endif#if INTENSIVE_DEBUG         sprintf( bwb_ebuf, "in exp_numconst(): read mantissa, string <%s> val <%lf>",            CURTASK exps[ CURTASK expsc ].string, var_getnval( &mantissa ) );         bwb_debug( bwb_ebuf );#endif         /* test if integer bounds have been exceeded */         if ( CURTASK exps[ CURTASK expsc ].type == NUMBER )            {            i = (int) var_getnval( &mantissa );            d = (bnumber) i;            if ( d != var_getnval( &mantissa ))               {               CURTASK exps[ CURTASK expsc ].type = NUMBER;#if INTENSIVE_DEBUG               sprintf( bwb_ebuf, "in exp_numconst(): integer bounds violated, promote to NUMBER" );               bwb_debug( bwb_ebuf );#endif                              }            }         /* read the exponent if there is one */         if ( exponent == TRUE )            {	    /* allow a plus or minus once at the beginning */	    need_pm = TRUE;	    /* initialize counters */            CURTASK exps[ CURTASK expsc ].string[ 0 ] = '\0';            s_pos = 0;            build_loop = TRUE;            /* loop to build the string */            while ( build_loop == TRUE )               {               switch( expression[ CURTASK exps[ CURTASK expsc ].pos_adv ] )                  {		  case '-':                        /* prefixed plus or minus */                  case '+':		     if ( need_pm == TRUE )        /* only allow once */			{			CURTASK exps[ CURTASK expsc ].string[ s_pos ] = expression[ CURTASK exps[ CURTASK expsc ].pos_adv ];			++CURTASK exps[ CURTASK expsc ].pos_adv;  /* advance to next character */			++s_pos;			CURTASK exps[ CURTASK expsc ].string[ s_pos ] = '\0';			}		     else			{			build_loop = FALSE;			}		     break;		  case '0':                        /* or ordinary digit */                  case '1':                  case '2':                  case '3':                  case '4':                  case '5':                  case '6':                  case '7':                  case '8':                  case '9':                     CURTASK exps[ CURTASK expsc ].string[ s_pos ] = expression[ CURTASK exps[ CURTASK expsc ].pos_adv ];                     ++CURTASK exps[ CURTASK expsc ].pos_adv;  /* advance to next character */                     ++s_pos;		     CURTASK exps[ CURTASK expsc ].string[ s_pos ] = '\0';		     need_pm = FALSE;                     break;                  default:                         /* anything else, terminate */                     build_loop = FALSE;                     break;                  }               }                                /* end of build loop for exponent */            /* assign the value to the user variable */#if NUMBER_DOUBLE            sscanf( CURTASK exps[ CURTASK expsc ].string, "%lf",               &( CURTASK exps[ CURTASK expsc ].nval ) );#else            sscanf( CURTASK exps[ CURTASK expsc ].string, "%f",               &( CURTASK exps[ CURTASK expsc ].nval ) );#endif#if INTENSIVE_DEBUG	    sprintf( bwb_ebuf, "in exp_numconst(): exponent is <%d>",               (int) CURTASK exps[ CURTASK expsc ].nval );            bwb_debug( bwb_ebuf );#endif            }                           /* end of exponent search */         if ( CURTASK exps[ CURTASK expsc ].nval == (bnumber) 0 )            {            CURTASK exps[ CURTASK expsc ].nval = var_getnval( &mantissa );            }         else            {            CURTASK exps[ CURTASK expsc ].nval = var_getnval( &mantissa )               * pow( (bnumber) 10.0, (bnumber) CURTASK exps[ CURTASK expsc ].nval );            }         break;      case 8:                           /* octal constant */         /* initialize counters */         CURTASK exps[ CURTASK expsc ].pos_adv = man_start;         CURTASK exps[ CURTASK expsc ].type = NUMBER;         CURTASK exps[ CURTASK expsc ].string[ 0 ] = '\0';         s_pos = 0;         exponent = OP_NULL;         build_loop = TRUE;         /* loop to build the string */         while ( build_loop == TRUE )            {            switch( expression[ CURTASK exps[ CURTASK expsc ].pos_adv ] )               {               case '0':                        /* or ordinary digit */               case '1':               case '2':               case '3':               case '4':               case '5':               case '6':               case '7':                  CURTASK exps[ CURTASK expsc ].string[ s_pos ] = expression[ CURTASK exps[ CURTASK expsc ].pos_adv ];                  ++CURTASK exps[ CURTASK expsc ].pos_adv;  /* advance to next character */                  ++s_pos;                  CURTASK exps[ CURTASK expsc ].string[ s_pos ] = '\0';                  break;               default:                         /* anything else, terminate */                  build_loop = FALSE;                  break;               }            }         /* now scan the string to determine the number */         sscanf( CURTASK exps[ CURTASK expsc ].string, "%o", &i );         CURTASK exps[ CURTASK expsc ].nval = (bnumber) i;         break;      case 16:                          /* hexadecimal constant */         /* initialize counters */         CURTASK exps[ CURTASK expsc ].pos_adv = man_start;         CURTASK exps[ CURTASK expsc ].type = NUMBER;         CURTASK exps[ CURTASK expsc ].string[ 0 ] = '\0';         s_pos = 0;         exponent = OP_NULL;         build_loop = TRUE;         /* loop to build the string */         while ( build_loop == TRUE )            {            switch( expression[ CURTASK exps[ CURTASK expsc ].pos_adv ] )               {               case '0':                        /* or ordinary digit */               case '1':               case '2':               case '3':               case '4':               case '5':               case '6':               case '7':               case '8':               case '9':               case 'A':               case 'a':               case 'B':               case 'b':               case 'C':               case 'c':               case 'D':               case 'd':               case 'E':               case 'e':               case 'F': /* Don't forget these! (JBV) */               case 'f':                  CURTASK exps[ CURTASK expsc ].string[ s_pos ] = expression[ CURTASK exps[ CURTASK expsc ].pos_adv ];                  ++CURTASK exps[ CURTASK expsc ].pos_adv;  /* advance to next character */                  ++s_pos;                  CURTASK exps[ CURTASK expsc ].string[ s_pos ] = '\0';                  break;               default:                         /* anything else, terminate */                  build_loop = FALSE;                  break;               }            }         /* now scan the string to determine the number */         sscanf( CURTASK exps[ CURTASK expsc ].string, "%x", &i );         CURTASK exps[ CURTASK expsc ].nval = (bnumber) i;         break;      }   /* note that the operation at this level is now a determined NUMBER */   CURTASK exps[ CURTASK expsc ].operation = NUMBER;#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in exp_numconst(): exit level <%d> precision <%c> value <%lf>",      CURTASK expsc, CURTASK exps[ CURTASK expsc ].type, exp_getnval( &( CURTASK exps[ CURTASK expsc ] ) ) );   bwb_debug( bwb_ebuf );#endif#if CHECK_RECURSION   in_use = FALSE;#endif   return TRUE;   }/***************************************************************	FUNCTION:       exp_function()	DESCRIPTION:    This function interprets a function,			calling bwb_exp() (recursively) to resolve any			arguments to the function.***************************************************************/#if ANSI_Cintexp_function( char *expression )#elseintexp_function( expression )   char *expression;#endif   {   struct exp_ese *e;   int s_pos;                           /* position in build buffer */   int loop;   int paren_level;   int n_args;   struct bwb_variable *v;   /* struct bwb_variable argv[ MAX_FARGS ]; */ /* Removed by JBV */   struct bwb_variable *argv; /* Added by JBV */   bstring *b;#if INTENSIVE_DEBUG   char tbuf[ MAXSTRINGSIZE + 1 ];   sprintf( bwb_ebuf, "in exp_function(): entered function, expression <%s>",      expression );   bwb_debug( bwb_ebuf );#endif   /*-----------------------------------------------------------*/   /* Added by JBV                                              */   /* Required because adding a simple "static" modifier in the */   /* argv declaration doesn't work for recursive calls!        */   /*-----------------------------------------------------------*/   if ( ( argv = (struct bwb_variable *) CALLOC( MAX_FARGS,      sizeof( struct bwb_variable ), "exp_function" )) == NULL )      {      bwb_error( err_getmem );      return NULL;      }   /* assign pointers to argument stack */   /* get the function name */   exp_getvfname( expression, CURTASK exps[ CURTASK expsc ].string );#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in exp_function(): name is <%s>.",      CURTASK exps[ CURTASK expsc ].string );   bwb_debug( bwb_ebuf );#endif   /* now find the function itself */   CURTASK exps[ CURTASK expsc ].function = fnc_find( CURTASK exps[ CURTASK expsc ].string );   /* check to see if it is valid */   if ( CURTASK exps[ CURTASK expsc ].function == NULL )      {#if PROG_ERRORS      sprintf( bwb_ebuf, "Failed to find function <%s>.",         CURTASK exps[ CURTASK expsc ].string );      bwb_error( bwb_ebuf );#else      bwb_error( err_uf  );#endif      return OP_ERROR;      }   /* note that this level is a function */   CURTASK exps[ CURTASK expsc ].operation = FUNCTION;   CURTASK exps[ CURTASK expsc ].pos_adv = strlen( CURTASK exps[ CURTASK expsc ].string );   /* check for begin parenthesis */   loop = TRUE;   while( loop == TRUE )      {      switch( expression[ CURTASK exps[ CURTASK expsc ].pos_adv ] )         {         case ' ':                              /* whitespace */         case '\t':            ++CURTASK exps[ CURTASK expsc ].pos_adv;        /* advance */            break;         case '(':                              /* begin paren */#if INTENSIVE_DEBUG            sprintf( bwb_ebuf, "in exp_function(): found begin parenthesis." );            bwb_debug( bwb_ebuf );#endif            ++CURTASK exps[ CURTASK expsc ].pos_adv;        /* advance beyond it */            paren_level = 1;                    /* set paren_level */            loop = FALSE;                       /* and break out */            break;

⌨️ 快捷键说明

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