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

📄 bwb_int.c

📁 这是一个简易的basic语言解释器, 可供我们学习和改进.
💻 C
📖 第 1 页 / 共 2 页
字号:
   return TRUE;   }/***************************************************************        FUNCTION:       is_cmd()	DESCRIPTION:    This function determines whether the			string in 'buffer' is a BASIC command			statement, returning TRUE or FALSE,			and if TRUE returning the command number			in the command lookup table in the			integer pointed to by 'cmdnum'.***************************************************************/#if ANSI_Cintis_cmd( char *buffer, int *cmdnum )#elseintis_cmd( buffer, cmdnum )   char *buffer;   int *cmdnum;#endif   {   register int n;   /* Convert the command name to upper case */   bwb_strtoupper( buffer );   /* Go through the command table and search for a match. */   for ( n = 0; n < COMMANDS; ++n )      {      if ( strcmp( bwb_cmdtable[ n ].name, buffer ) == 0 )         {         *cmdnum = n;         return TRUE;         }      }   /* No command name was found */   *cmdnum = -1;   return FALSE;   }/***************************************************************        FUNCTION:       is_let()        DESCRIPTION:    This function tries to determine if the                        expression in <buffer> is a LET statement                        without the LET command specified.***************************************************************/#if ANSI_Cintis_let( char *buffer, int *cmdnum )#elseintis_let( buffer, cmdnum )   char *buffer;   int *cmdnum;#endif   {   register int n, i;#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in is_let(): buffer <%s>", buffer );   bwb_debug( bwb_ebuf );#endif   /* Go through the expression and search for an assignment operator. */   for ( n = 0; buffer[ n ] != '\0'; ++n )      {      switch( buffer[ n ] )         {         case '\"':                     /* string constant */            ++n;            while( buffer[ n ] != '\"' )               {               ++n;               if ( buffer[ n ] == '\0' )                  {#if PROG_ERRORS                  sprintf( bwb_ebuf, "Incomplete string constant" );                  bwb_error( bwb_ebuf );#else                  bwb_error( err_syntax );#endif                  *cmdnum = -1;                  return FALSE;                  }               }            ++n;            break;         case '=':#if INTENSIVE_DEBUG            sprintf( bwb_ebuf, "in is_let(): implied LET found." );            bwb_debug( bwb_ebuf );#endif            for ( i = 0; i < COMMANDS; ++i )               {               if ( strncmp( bwb_cmdtable[ i ].name, "LET", (size_t) 3 ) == 0 )                  {                  *cmdnum = i;                  }               }            return TRUE;         }      }   /* No command name was found */   *cmdnum = -1;   return FALSE;   }/***************************************************************        FUNCTION:       bwb_stripcr()	DESCRIPTION:    This function strips the carriage return			or line-feed from the end of a string.***************************************************************/#if ANSI_Cintbwb_stripcr( char *s )#elseintbwb_stripcr( s )   char *s;#endif   {   char *p;   p = s;   while ( *p != 0 )      {      switch( *p )         {         case '\r':         case '\n':            *p = 0;            return TRUE;         }      ++p;      }   *p = 0;   return TRUE;   }/***************************************************************        FUNCTION:       is_numconst()        DESCRIPTION:    This function reads the string in <buffer>                        and returns TRUE if it is a numerical                        constant and FALSE if it is not. At                        this point, only decimal (base 10)                        constants are detected.***************************************************************/#if ANSI_Cintis_numconst( char *buffer )#elseintis_numconst( buffer )   char *buffer;#endif   {   char *p;#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in is_numconst(): received string <%s>.", buffer );   bwb_debug( bwb_ebuf );#endif   /* Return FALSE for empty buffer */   if ( buffer[ 0 ] == '\0' )      {      return FALSE;      }   /* else check digits */   p = buffer;   while( *p != '\0' )      {      switch( *p )         {         case '0':         case '1':         case '2':         case '3':         case '4':         case '5':         case '6':         case '7':         case '8':         case '9':            break;         default:            return FALSE;         }      ++p;      }   /* only numerical characters detected */   return TRUE;   }/***************************************************************        FUNCTION:       bwb_numseq()	DESCRIPTION:    This function reads in a sequence of			numbers (e.g., "10-120"), returning			the first and last numbers in the sequence			in the integers pointed to by 'start' and			'end'.***************************************************************/#if ANSI_Cintbwb_numseq( char *buffer, int *start, int *end )#elseintbwb_numseq( buffer, start, end )   char *buffer;   int *start;   int *end;#endif   {   register int b, n;   int numbers;   static char *tbuf;   static int init = FALSE;   /* get memory for temporary buffer if necessary */   if ( init == FALSE )      {      init = TRUE;      /* Revised to CALLOC pass-thru call by JBV */      if ( ( tbuf = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ), "bwb_numseq")) == NULL )         {#if PROG_ERRORS	 bwb_error( "in bwb_numseq(): failed to find memory for tbuf" );#else	 bwb_error( err_getmem );#endif	 }      }   if ( buffer[ 0 ] == 0 )      {      *start = *end = 0;      return FALSE;      }   numbers = n = b = 0;   tbuf[ 0 ] = 0;   while( TRUE )      {      switch( buffer[ b ] )         {         case 0:                           /* end of string */         case '\n':         case '\r':            if ( n > 0 )               {               if ( numbers == 0 )                  {                  *end = 0;                  *start = atoi( tbuf );                  ++numbers;                  }               else                  {                  *end = atoi( tbuf );                  return TRUE;                  }               }            else               {               if ( numbers == 0 )                  {                  *start = *end = 0;                  }               else if ( numbers == 1 )                  {                  *end = 0;                  }               else if ( ( numbers == 2 ) && ( tbuf[ 0 ] == 0 ))                  {                  *end = 0;                  }               }            return TRUE;#ifdef ALLOWWHITESPACE         case ' ':                         /* whitespace */         case '\t':#endif         case '-':                         /* or skip to next number */            if ( n > 0 )               {               if ( numbers == 0 )                  {                  *start = atoi( tbuf );                  ++numbers;                  }               else                  {                  *end = atoi( tbuf );                  return TRUE;                  }               }            ++b;            n = 0;            break;         case '0':         case '1':         case '2':         case '3':         case '4':         case '5':         case '6':         case '7':         case '8':         case '9':            tbuf[ n ] = buffer[ b ];            ++n;            tbuf[ n ] = 0;            ++b;            break;         default:#if PROG_ERRORS            sprintf( bwb_ebuf,               "ERROR: character <%c> unexpected in numerical sequence",               buffer[ b ] );            ++b;            bwb_error( bwb_ebuf );#else            bwb_error( err_syntax );#endif            break;         }      }   }/***************************************************************        FUNCTION:       bwb_freeline()	DESCRIPTION:    This function frees memory associated			with a program line in memory.***************************************************************/#if ANSI_Cintbwb_freeline( struct bwb_line *l )#elseintbwb_freeline( l )   struct bwb_line *l;#endif   {   /* free arguments if there are any */   /* Revised to FREE pass-thru calls by JBV */   if (l->buffer != NULL)   {       FREE( l->buffer, "bwb_freeline" );       l->buffer = NULL; /* JBV */   }   FREE( l, "bwb_freeline" );   l = NULL; /* JBV */   return TRUE;   }/***************************************************************        FUNCTION:       int_qmdstr()	DESCRIPTION:    This function returns a string delimited			by quotation marks.***************************************************************/#if ANSI_Cintint_qmdstr( char *buffer_a, char *buffer_b )#elseintint_qmdstr( buffer_a, buffer_b )   char *buffer_a;   char *buffer_b;#endif   {   char *a, *b;   a = buffer_a;   ++a;                         /* advance beyond quotation mark */   b = buffer_b;   while( *a != '\"' )      {      *b = *a;      ++a;      ++b;      *b = '\0';      }   return TRUE;   }/***************************************************************	FUNCTION:       is_eol()	DESCRIPTION:    This function determines whether the buffer			is at the end of a line.***************************************************************/#if ANSI_Cextern intis_eol( char *buffer, int *position )#elseintis_eol( buffer, position )   char *buffer;   int *position;#endif   {   adv_ws( buffer, position );#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in is_eol(): character is <0x%x> = <%c>",      buffer[ *position ], buffer[ *position ] );   bwb_debug( bwb_ebuf );#endif   switch( buffer[ *position ] )      {      case '\0':      case '\n':      case '\r':#if MULTISEG_LINES      case ':':#endif         return TRUE;      default:         return FALSE;      }   }

⌨️ 快捷键说明

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