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

📄 bwb_prn.c

📁 这是一个简易的basic语言解释器, 可供我们学习和改进.
💻 C
📖 第 1 页 / 共 3 页
字号:
         case '\r':	    loop = FALSE;            break;#endif         case ',':		/* tab over */            /* Tab only if there's no format specification! (JBV) */            if (( fs_pos == -1 ) || ( strlen( element ) == 0 ))               xputc( f, '\t' );            ++l->position;            adv_ws( l->buffer, &( l->position ) );            break;         case ';':		/* concatenate strings */            ++l->position;            adv_ws( l->buffer, &( l->position ) );            break;         default:            loop = FALSE;            break;         }      }				/* end of loop through print elements */   if (( fs_pos > -1 ) && ( strlen( element ) > 0 ))      format = get_prnfmt( format_string, &fs_pos, f ); /* Finish up (JBV) */   /* call prn_cr() to print a CR if it is not overridden by a      concluding ';' mark */   prn_cr( l->buffer, f );   return TRUE;   }                            /* end of function bwb_xprint() */#if COMMON_CMDS/***************************************************************        FUNCTION:       get_prnfmt()	DESCRIPTION:    This function gets the PRINT USING			format string, returning a structure			to the format.***************************************************************/#if ANSI_Cstatic struct prn_fmt *get_prnfmt( char *buffer, int *position, FILE *f )#elsestatic struct prn_fmt *get_prnfmt( buffer, position, f )   char *buffer;   int *position;   FILE *f;#endif   {   static struct prn_fmt retstruct;   int loop;   /* set some defaults */   retstruct.precision = 0;   retstruct.type = FALSE;   retstruct.exponential = FALSE;   retstruct.right_justified = FALSE;   retstruct.commas = FALSE;   retstruct.sign = FALSE;   retstruct.money = FALSE;   retstruct.fill = ' ';   retstruct.minus = FALSE;   retstruct.width = 0;   /* check for negative position */   if ( *position < 0 )      {      return &retstruct;      }   /* advance past whitespace */   /* adv_ws( buffer, position ); */  /* Don't think we want this (JBV) */   /* check first character: a lost can be decided right here */   loop = TRUE;   while( loop == TRUE )      {#if INTENSIVE_DEBUG      sprintf( bwb_ebuf, "in get_prnfmt(): loop, buffer <%s>",         &( buffer[ *position ] ) );      bwb_debug( bwb_ebuf );#endif      switch( buffer[ *position ] )         {         case ' ':		/* end of this format segment */            xxputc( f, buffer[ *position ] ); /* Gotta output it (JBV) */            ++( *position ); /* JBV */            if (retstruct.type != FALSE) loop = FALSE; /* JBV */            break;         case '\0':		/* end of format string */         case '\n':         case '\r':            *position = -1;            return &retstruct;         case '_':		/* print next character as literal */            ++( *position );            xxputc( f, buffer[ *position ] ); /* Not xputc, no tabs (JBV) */            ++( *position );            break;	 case '!':            retstruct.type = STRING;            retstruct.width = 1;            ++( *position ); /* JBV */            return &retstruct;	 case '&': /* JBV */            retstruct.type = STRING;            retstruct.width = -1;             ++( *position );            return &retstruct;	 case '\\':#if INTENSIVE_DEBUG            sprintf( bwb_ebuf, "in get_prnfmt(): found \\" );            bwb_debug( bwb_ebuf );#endif	    retstruct.type = STRING;	    ++retstruct.width;	    ++( *position );	    for ( ; buffer[ *position ] == ' '; ++( *position ) )               {               ++retstruct.width;               }            if ( buffer[ *position ] == '\\' )	       {	       ++retstruct.width;               ++( *position );               }            return &retstruct;         case '$':            ++retstruct.width; /* JBV */            ++( *position );            retstruct.money = TRUE;            if ( buffer[ *position ] == '$' )               {               ++retstruct.width; /* JBV */               ++( *position );               }            break;         case '*':            ++retstruct.width; /* JBV */            ++( *position );            retstruct.fill = '*';            if ( buffer[ *position ] == '*' )               {               ++retstruct.width; /* JBV */               ++( *position );               }            break;         case '+':            ++( *position );            retstruct.sign = TRUE;            break;         case '#':            retstruct.type = NUMBER;		/* for now */            /* ++( *position ); */  /* Removed by JBV */            /* The initial condition shouldn't be retstruct.width = 1 (JBV) */            for ( ; buffer[ *position ] == '#'; ++( *position ) )               {               ++retstruct.width;               }            if ( buffer[ *position ] == ',' )               {               retstruct.commas = TRUE;               ++retstruct.width; /* JBV */               ++( *position ); /* JBV */               }            if ( buffer[ *position ] == '.' )               {	       retstruct.type = NUMBER;	       ++retstruct.width;               ++( *position );               for ( retstruct.precision = 0; buffer[ *position ] == '#'; ++( *position ) )                  {		  ++retstruct.precision;		  ++retstruct.width;                  }               }            if ( buffer[ *position ] == '-' )               {               retstruct.minus = TRUE;               ++( *position );               }            return &retstruct;	 case '^':            retstruct.type = NUMBER;            retstruct.exponential = TRUE;            for ( retstruct.width = 1; buffer[ *position ] == '^'; ++( *position ) )               {               ++retstruct.width;               }            return &retstruct;	 default: /* JBV */            xxputc( f, buffer[ *position ] ); /* Gotta output it (JBV) */            ++( *position );            break;         }      }					/* end of loop */   return &retstruct;   }#endif/***************************************************************        FUNCTION:       prn_cr()	DESCRIPTION:    This function outputs a carriage-return			to a specified file or output device.***************************************************************/#if ANSI_Cstatic intprn_cr( char *buffer, FILE *f )#elsestatic intprn_cr( buffer, f )   char *buffer;   FILE *f;#endif   {   register int c;   int loop;   /* find the end of the buffer */   for ( c = 0; buffer[ c ] != '\0'; ++c )      {      }#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in prn_cr(): initial c is <%d>", c );   bwb_debug( bwb_ebuf );#endif   /* back up through any whitespace */   loop = TRUE;   while ( loop == TRUE )      {      switch( buffer[ c ] )         {         case ' ':                              /* if whitespace */         case '\t':         case 0:#if INTENSIVE_DEBUG            sprintf( bwb_ebuf, "in prn_cr(): backup: c is <%d>, char <%c>[0x%x]",               c, buffer[ c ], buffer[ c ] );            bwb_debug( bwb_ebuf );#endif            --c;                                /* back up */            if ( c < 0 )                        /* check position */               {               loop = FALSE;               }            break;         default:                               /* else break out */#if INTENSIVE_DEBUG            sprintf( bwb_ebuf, "in prn_cr(): breakout: c is <%d>, char <%c>[0x%x]",               c, buffer[ c ], buffer[ c ] );            bwb_debug( bwb_ebuf );#endif            loop = FALSE;            break;         }      }   if ( buffer[ c ] == ';' )      {#if INTENSIVE_DEBUG      sprintf( bwb_ebuf, "in prn_cr(): concluding <;> detected." );      bwb_debug( bwb_ebuf );#endif      return FALSE;      }   else      {      prn_xprintf( f, "\n" );      return TRUE;      }   }/***************************************************************        FUNCTION:       prn_xprintf()	DESCRIPTION:    This function outputs a null-terminated			string to a specified file or output			device.***************************************************************/#if ANSI_Cintprn_xprintf( FILE *f, char *buffer )#elseintprn_xprintf( f, buffer )   FILE *f;   char *buffer;#endif   {   char *p;   /* DO NOT try anything so stupid as to run bwb_debug() from      here, because it will create an endless loop. And don't      ask how I know. */   for ( p = buffer; *p != '\0'; ++p )      {      xputc( f, *p );      }   return TRUE;   }/***************************************************************        FUNCTION:       prn_xxprintf()	DESCRIPTION:    This function outputs a null-terminated			string to a specified file or output			device without expanding tabs.			Added by JBV 10/95***************************************************************/#if ANSI_Cintprn_xxprintf( FILE *f, char *buffer )#elseintprn_xxprintf( f, buffer )   FILE *f;   char *buffer;#endif   {   char *p;   /* DO NOT try anything so stupid as to run bwb_debug() from      here, because it will create an endless loop. And don't      ask how I know. */   for ( p = buffer; *p != '\0'; ++p )      {      xxputc( f, *p );      }   return TRUE;   }/***************************************************************        FUNCTION:       xputc()	DESCRIPTION:    This function outputs a character to a			specified file or output device, expanding			TABbed output approriately.***************************************************************/#if ANSI_Cintxputc( FILE *f, char c )#elseintxputc( f, c )   FILE *f;   char c;#endif   {   static int tab_pending = FALSE;   /* check for pending TAB */   if ( tab_pending == TRUE )      {      if ( (int) c < ( * prn_getcol( f ) ) )         {         xxputc( f, '\n' );         }      while( ( * prn_getcol( f )) < (int) c )         {         xxputc( f, ' ' );         }      tab_pending = FALSE;      return TRUE;      }   /* check c for specific output options */   switch( c )      {      case PRN_TAB:         tab_pending = TRUE;         break;      case '\t':         while( ( (* prn_getcol( f )) % 14 ) != 0 )            {            xxputc( f, ' ' );            }         break;      default:         xxputc( f, c );         break;      }   return TRUE;   }/***************************************************************        FUNCTION:       xxputc()	DESCRIPTION:    This function outputs a character to a			specified file or output device, checking			to be sure the PRINT width is within			the bounds specified for that device.***************************************************************/#if ANSI_Cstatic intxxputc( FILE *f, char c )#elsestatic intxxputc( f, c )   FILE *f;   char c;#endif   {   /* check to see if width has been exceeded */   if ( * prn_getcol( f ) >= prn_getwidth( f ))      {      xxxputc( f, '\n' );                 /* output LF */      * prn_getcol( f ) = 1;		/* and reset */      }   /* adjust the column counter */   if ( c == '\n' )      {      * prn_getcol( f ) = 1;      }   else      {      ++( * prn_getcol( f ));      }   /* now output the character */   return xxxputc( f, c );   }/***************************************************************	FUNCTION:       xxxputc()	DESCRIPTION:    This function sends a character to a			specified file or output device.***************************************************************/#if ANSI_Cstatic intxxxputc( FILE *f, char c )#elsestatic intxxxputc( f, c )   FILE *f;   char c;#endif   {   if (( f == stdout ) || ( f == stderr ))      {      return bwx_putc( c );      }   else      {      return fputc( c, f );      }   }/***************************************************************        FUNCTION:       prn_getcol()	DESCRIPTION:    This function returns a pointer to an			integer containing the current PRINT			column for a specified file or device.***************************************************************/#if ANSI_Cint *prn_getcol( FILE *f )#elseint *prn_getcol( f )   FILE *f;#endif   {   register int n;   static int dummy_pos;   if (( f == stdout ) || ( f == stderr ))      {      return &prn_col;      }#if COMMON_CMDS   for ( n = 0; n < DEF_DEVICES; ++n )      {      if ( dev_table[ n ].cfp == f )         {         return &( dev_table[ n ].col );         }      }#endif   /* search failed */#if PROG_ERRORS   bwb_error( "in prn_getcol(): failed to find file pointer" );#else   bwb_error( err_devnum );#endif   return &dummy_pos;   }/***************************************************************        FUNCTION:       prn_getwidth()	DESCRIPTION:    This function returns the PRINT width for			a specified file or output device.***************************************************************/#if ANSI_Cintprn_getwidth( FILE *f )#elseintprn_getwidth( f )   FILE *f;#endif   {   register int n;   if (( f == stdout ) || ( f == stderr ))      {      return prn_width;      }#if COMMON_CMDS   for ( n = 0; n < DEF_DEVICES; ++n )      {      if ( dev_table[ n ].cfp == f )         {         return dev_table[ n ].width;         }      }#endif   /* search failed */#if PROG_ERRORS   bwb_error( "in prn_getwidth(): failed to find file pointer" );#else   bwb_error( err_devnum );#endif   return 1;   }/***************************************************************        FUNCTION:       prn_precision()	DESCRIPTION:    This function returns the level of precision			required for a specified numerical value.***************************************************************/#if ANSI_Cintprn_precision( struct bwb_variable *v )

⌨️ 快捷键说明

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