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

📄 bwb_inp.c

📁 这是一个简易的basic语言解释器, 可供我们学习和改进.
💻 C
📖 第 1 页 / 共 4 页
字号:
   int suppress_qm;   static char tbuf[ MAXSTRINGSIZE + 1 ];   static char pstring[ MAXSTRINGSIZE + 1 ];#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in bwb_input(): enter function" );   bwb_debug( bwb_ebuf );#endif   pstring[ 0 ] = '\0';#if COMMON_CMDS   /* advance beyond whitespace and check for the '#' sign */   adv_ws( l->buffer, &( l->position ) );   if ( l->buffer[ l->position ] == '#' )      {      ++( l->position );      adv_element( l->buffer, &( l->position ), tbuf );      pos = 0;      v = bwb_exp( tbuf, FALSE, &pos );      adv_ws( l->buffer, &( l->position ) );      if ( l->buffer[ l->position ] == ',' )         {         ++( l->position );         }      else         {#if PROG_ERRORS         bwb_error( "in bwb_input(): no comma after#n" );#else         bwb_error( err_syntax );#endif         return bwb_zline( l );         }      req_devnumber = (int) exp_getnval( v );#if INTENSIVE_DEBUG      sprintf( bwb_ebuf, "in bwb_input(): requested device number <%d>",         req_devnumber );      bwb_debug( bwb_ebuf );#endif      /* check the requested device number */      if ( ( req_devnumber < 0 ) || ( req_devnumber >= DEF_DEVICES ))         {#if PROG_ERRORS         bwb_error( "in bwb_input(): Requested device number is out if range." );#else         bwb_error( err_devnum );#endif         return bwb_zline( l );         }      if ( ( dev_table[ req_devnumber ].mode == DEVMODE_CLOSED ) ||         ( dev_table[ req_devnumber ].mode == DEVMODE_AVAILABLE ) )         {#if PROG_ERRORS         bwb_error( "in bwb_input(): Requested device number is not open." );#else         bwb_error( err_devnum );#endif         return bwb_zline( l );         }      if ( dev_table[ req_devnumber ].mode != DEVMODE_INPUT )         {#if PROG_ERRORS         bwb_error( "in bwb_input(): Requested device is not open for INPUT." );#else         bwb_error( err_devnum );#endif         return bwb_zline( l );         }      /* look up the requested device in the device table */      fp = dev_table[ req_devnumber ].cfp;      }   else      {      fp = stdin;      }#else   fp = stdin;#endif				/* COMMON_CMDS */   /* if input is not from stdin, then branch to bwb_xinp() */   if ( fp != stdin )      {      return bwb_xinp( l, fp );      }   /* from this point we presume that input is from stdin */   /* check for a semicolon or a quotation mark, not in      first position: this should indicate a prompt string */   suppress_qm = is_prompt = FALSE;   adv_ws( l->buffer, &( l->position ) );   switch( l->buffer[ l->position ] )      {      case '\"':         is_prompt = TRUE;         break;      case ';':         /* AGENDA: add code to suppress newline if a            semicolon is used here; this may not be possible            using ANSI C alone, since it has not functions for            unechoed console input. */         is_prompt = TRUE;         ++l->position;         break;      case ',':         /* QUERY: why is this code here? the question mark should            be suppressed if a comma <follows> the prompt string. */#if INTENSIVE_DEBUG         bwb_debug( "in bwb_input(): found initial comma" );#endif         suppress_qm = TRUE;         ++l->position;         break;      }   /* get prompt string and print it */   if ( is_prompt == TRUE )      {      /* get string element */      inp_const( l->buffer, tbuf, &( l->position ) );      /* advance past semicolon to beginning of variable */      /*--------------------------------------------------------*/      /* Since inp_const was just called and inp_adv is called  */      /* within that, it will have already noted and passed the */      /* comma by the time it gets here.  Therefore one must    */      /* refer instead to the last returned value for inp_adv!  */      /* (JBV, 10/95)                                           */      /*--------------------------------------------------------*/      /* suppress_qm = inp_adv( l->buffer, &( l->position ) ); */      suppress_qm = last_inp_adv_rval;      /* print the prompt string */      strncpy( pstring, tbuf, MAXSTRINGSIZE );      }                                      /* end condition: prompt string */   /* print out the question mark delimiter unless it has been      suppressed */   if ( suppress_qm != TRUE )      {      strncat( pstring, "? ", MAXSTRINGSIZE );      }#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in bwb_input(): ready to get input line" );   bwb_debug( bwb_ebuf );#endif   /* read a line into the input buffer */   bwx_input( pstring, tbuf );   bwb_stripcr( tbuf );#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in bwb_input(): received line <%s>", tbuf );   bwb_debug( bwb_ebuf );   bwb_debug( "Press RETURN: " );   getchar();#endif   /* reset print column to account for LF at end of fgets() */   * prn_getcol( stdout ) = 1;   return inp_str( l, tbuf, l->buffer, &( l->position ) );   }/***************************************************************        FUNCTION:       bwb_xinp()        DESCRIPTION:    This function does the bulk of processing                        for INPUT#, and so is file independent.***************************************************************/#if ANSI_Cstatic struct bwb_line *bwb_xinp( struct bwb_line *l, FILE *f )#elsestatic struct bwb_line *bwb_xinp( l, f )   struct bwb_line *l;   FILE *f;#endif   {   int loop;   struct bwb_variable *v;   char c;   register int n;   int *pp;   int n_params;   char tbuf[ MAXSTRINGSIZE + 1 ];#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in bwb_xinp(): buffer <%s>",      &( l->buffer[ l->position ] ) );   bwb_debug( bwb_ebuf );#endif   /* loop through elements required */   loop = TRUE;   while ( loop == TRUE )      {            /* read a variable from the list */      bwb_getvarname( l->buffer, tbuf, &( l->position ) );      v = var_find( tbuf );#if INTENSIVE_DEBUG      sprintf( bwb_ebuf, "in bwb_xinp(): found variable name <%s>",         v->name );      bwb_debug( bwb_ebuf );#endif      /* read subscripts */      adv_ws( l->buffer, &( l->position ) );      if ( l->buffer[ l->position ] == '(' )         {#if INTENSIVE_DEBUG         sprintf( bwb_ebuf, "in bwb_xinp(): variable <%s> has dimensions",            v->name );         bwb_debug( bwb_ebuf );#endif         dim_getparams( l->buffer, &( l->position ), &n_params, &pp );         for ( n = 0; n < v->dimensions; ++n )            {            v->array_pos[ n ] = pp[ n ];            }         }      inp_advws( f );      /* perform type-specific input */      switch( v->type )         {         case STRING:            if ( inp_xgetc( f, TRUE ) != '\"' )               {#if PROG_ERRORS               sprintf( bwb_ebuf, "in bwb_xinp(): expected quotation mark" );               bwb_error( bwb_ebuf );#else               bwb_error( err_mismatch );#endif               }            n = 0;            while ( ( c = (char) inp_xgetc( f, TRUE )) != '\"' )               {               tbuf[ n ] = c;               ++n;               tbuf[ n ] = '\0';               }            str_ctob( var_findsval( v, v->array_pos ), tbuf );#if INTENSIVE_DEBUG            sprintf( bwb_ebuf, "in bwb_xinp(): read STRING <%s>",               tbuf );            bwb_debug( bwb_ebuf );#endif            inp_eatcomma( f );            break;         default:            n = 0;            while ( ( c = (char) inp_xgetc( f, FALSE )) != ',' )               {               tbuf[ n ] = c;               ++n;               tbuf[ n ] = '\0';               }#if INTENSIVE_DEBUG            sprintf( bwb_ebuf, "in bwb_xinp(): read NUMBER <%s>",               tbuf );            bwb_debug( bwb_ebuf );#endif            /*------------------------------------------------------------*/            /* atof call replaced by inp_numconst, gets all input formats */            /* (JBV, 10/95)                                               */            /*------------------------------------------------------------*/            /* * var_findnval( v, v->array_pos ) = (bnumber) atof( tbuf ); */            * var_findnval( v, v->array_pos ) = inp_numconst( tbuf );            break;         }				/* end of switch for type-specific input */      /* check for comma */      adv_ws( l->buffer, &( l->position ) );      if ( l->buffer[ l->position ] == ',' )         {         ++( l->position );         }      else         {         loop = FALSE;         }      }   /* return */   return bwb_zline( l );   }/***************************************************************        FUNCTION:       inp_advws()	DESCRIPTION:    This C function advances past whitespace			input from a particular file or device.***************************************************************/#if ANSI_Cstatic intinp_advws( FILE *f )#elsestatic intinp_advws( f )   FILE *f;#endif   {   register int c;   int loop;   loop = TRUE;   while ( loop == TRUE )      {      c = (char) inp_xgetc( f, TRUE );      switch( c )         {         case '\n':         case '\r':         case ' ':         case '\t':            break;         default:            char_saved = TRUE;            cs = c;            loop = FALSE;            break;         }      }      return TRUE;   }/***************************************************************        FUNCTION:       inp_xgetc()	DESCRIPTION:    This C function reads in a character from			a specified file or device.***************************************************************/#if ANSI_Cstatic intinp_xgetc( FILE *f, int is_string )#elsestatic intinp_xgetc( f, is_string )   FILE *f;   int is_string;#endif   {   register int c;   static int prev_eof = FALSE;   if ( char_saved == TRUE )      {      char_saved = FALSE;      return cs;      }   if ( feof( f ) != 0 )      {      if ( prev_eof == TRUE )         {         bwb_error( err_od );         }      else         {	 prev_eof = TRUE;	 return (int) ',';	 }      }   prev_eof = FALSE;   c = fgetc( f );   if ( is_string == TRUE )      {      return c;      }         switch( c )      {      case ' ':      case '\n':      case ',':      case '\r':         return ',';      }   return c;   }/***************************************************************        FUNCTION:       inp_eatcomma()	DESCRIPTION:    This C function advances beyond a comma			input from a specified file or device.***************************************************************/#if ANSI_Cstatic intinp_eatcomma( FILE *f )#elsestatic intinp_eatcomma( f )   FILE *f;#endif   {   char c;   while ( ( c = (char) inp_xgetc( f, TRUE ) ) == ',' )      {      }         char_saved = TRUE;   cs = c;         return TRUE;   }/***************************************************************

⌨️ 快捷键说明

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