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

📄 bwb_fnc.c

📁 这是一个简易的basic语言解释器, 可供我们学习和改进.
💻 C
📖 第 1 页 / 共 3 页
字号:
   time( &now );   /* Following statement was (bnumber) (JBV) */   * var_findnval( &nvar, nvar.array_pos )      = (float) fmod( (double) now, (double) (60*60*24));   return &nvar;   }/***************************************************************        FUNCTION:       fnc_mid()        DESCRIPTION:    This C function implements the BASIC                        predefined MID$ function	SYNTAX:		MID$( string$, start-position-in-string[, number-of-spaces ] )***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_mid( int argc, struct bwb_variable *argv, int unique_id  )#elsestruct bwb_variable *fnc_mid( argc, argv, unique_id  )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   register int c;   char target_string[ MAXSTRINGSIZE + 1 ];   int target_counter, num_spaces;   char tbuf[ MAXSTRINGSIZE + 1 ];   static int init = FALSE;   /* initialize the variable if necessary */   if ( init == FALSE )      {      init = TRUE;      var_make( &nvar, STRING );      }   /* check arguments */#if PROG_ERRORS   if ( argc < 2 )      {      sprintf( bwb_ebuf, "Not enough arguments to function MID$()" );      bwb_error( bwb_ebuf );      return &nvar;      }   if ( argc > 3 )      {      sprintf( bwb_ebuf, "Two many arguments to function MID$()" );      bwb_error( bwb_ebuf );      return &nvar;      }#else   if ( fnc_checkargs( argc, argv, 2, 3 ) == FALSE )      {      return NULL;      }#endif   /* get arguments */   str_btoc( target_string, var_getsval( &( argv[ 0 ] ) ));   target_counter = (int) var_getnval( &( argv[ 1 ] ) ) - 1;   if ( target_counter > (int) strlen( target_string ))      {      tbuf[ 0 ] = '\0';      str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );      return &nvar;      }   if ( argc == 3 )      {      num_spaces = (int) var_getnval( &( argv[ 2 ] ));      }   else      {      num_spaces = MAXSTRINGSIZE;      }#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in fnc_mid() string <%s> startpos <%d> spaces <%d>",      target_string, target_counter, num_spaces );   bwb_debug( bwb_ebuf );#endif   c = 0;   tbuf[ c ] = '\0';   while ( ( c < num_spaces ) && ( target_string[ target_counter ] != '\0' ))      {      tbuf[ c ] = target_string[ target_counter ];      ++c;      tbuf[ c ] = '\0';      ++target_counter;      }   str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );   return &nvar;   }/***************************************************************        FUNCTION:       fnc_left()        DESCRIPTION:    This C function implements the BASIC                        predefined LEFT$ function	SYNTAX:		LEFT$( string$, number-of-spaces )***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_left( int argc, struct bwb_variable *argv, int unique_id  )#elsestruct bwb_variable *fnc_left( argc, argv, unique_id  )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   register int c;   char target_string[ MAXSTRINGSIZE + 1 ];   int target_counter, num_spaces;   char tbuf[ MAXSTRINGSIZE + 1 ];   static int init = FALSE;   /* initialize the variable if necessary */   if ( init == FALSE )      {      init = TRUE;      var_make( &nvar, STRING );      }   /* check arguments */#if PROG_ERRORS   if ( argc < 2 )      {      sprintf( bwb_ebuf, "Not enough arguments to function LEFT$()" );      bwb_error( bwb_ebuf );      return &nvar;      }   if ( argc > 2 )      {      sprintf( bwb_ebuf, "Two many arguments to function LEFT$()" );      bwb_error( bwb_ebuf );      return &nvar;      }#else   if ( fnc_checkargs( argc, argv, 2, 2 ) == FALSE )      {      return NULL;      }#endif   /* get arguments */   str_btoc( tbuf, var_getsval( &( argv[ 0 ] ) ));   target_counter = 0;   num_spaces = (int) var_getnval( &( argv[ 1 ] ));#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in fnc_left() string <%s> startpos <%d> spaces <%d>",      tbuf, target_counter, num_spaces );   bwb_debug( bwb_ebuf );#endif   c = 0;   target_string[ 0 ] = '\0';   while (( c < num_spaces ) && ( tbuf[ c ] != '\0' ))      {      target_string[ target_counter ] = tbuf[ c ];      ++target_counter;      target_string[ target_counter ] = '\0';      ++c;      }   str_ctob( var_findsval( &nvar, nvar.array_pos ), target_string );   return &nvar;   }/***************************************************************        FUNCTION:       fnc_right()        DESCRIPTION:    This C function implements the BASIC                        predefined RIGHT$ function	SYNTAX:		RIGHT$( string$, number-of-spaces )***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_right( int argc, struct bwb_variable *argv, int unique_id  )#elsestruct bwb_variable *fnc_right( argc, argv, unique_id  )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   register int c;   char target_string[ MAXSTRINGSIZE + 1 ];   int target_counter, num_spaces;   char tbuf[ MAXSTRINGSIZE + 1 ];   static int init = FALSE;   /* initialize the variable if necessary */   if ( init == FALSE )      {      init = TRUE;      var_make( &nvar, STRING );      }   /* check arguments */#if PROG_ERRORS   if ( argc < 2 )      {      sprintf( bwb_ebuf, "Not enough arguments to function RIGHT$()" );      bwb_error( bwb_ebuf );      return &nvar;      }   if ( argc > 2 )      {      sprintf( bwb_ebuf, "Two many arguments to function RIGHT$()" );      bwb_error( bwb_ebuf );      return &nvar;      }#else   if ( fnc_checkargs( argc, argv, 2, 2 ) == FALSE )      {      return NULL;      }#endif   /* get arguments */   str_btoc( target_string, var_getsval( &( argv[ 0 ] ) ));   target_counter = strlen( target_string ) - (int) var_getnval( &( argv[ 1 ] ));   num_spaces = MAXSTRINGSIZE;#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in fnc_right() string <%s> startpos <%d> spaces <%d>",      target_string, target_counter, num_spaces );   bwb_debug( bwb_ebuf );#endif   c = 0;   tbuf[ c ] = '\0';   while ( ( c < num_spaces ) && ( target_string[ target_counter ] != '\0' ))      {      tbuf[ c ] = target_string[ target_counter ];      ++c;      tbuf[ c ] = '\0';      ++target_counter;      }   str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );   return &nvar;   }/***************************************************************        FUNCTION:       fnc_asc()        DESCRIPTION:    This function implements the predefined			BASIC ASC() function, returning the ASCII			number associated with the first character			in the string argument.	SYNTAX:		ASC( string$ )***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_asc( int argc, struct bwb_variable *argv, int unique_id )#elsestruct bwb_variable *fnc_asc( argc, argv, unique_id )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   static char *tbuf;   static int init = FALSE;   /* initialize the variable if necessary */   if ( init == FALSE )      {      init = TRUE;      var_make( &nvar, NUMBER );      /* Revised to CALLOC pass-thru call by JBV */      if ( ( tbuf = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ), "fnc_asc" )) == NULL )         {#if PROG_ERRORS         bwb_error( "in fnc_asc(): failed to get memory for tbuf" );#else         bwb_error( err_getmem );#endif         }      }   /* check parameters */#if PROG_ERRORS   if ( argc < 1 )      {      sprintf( bwb_ebuf, "Not enough parameters (%d) to function ASC().",         argc );      bwb_error( bwb_ebuf );      return NULL;      }   else if ( argc > 1 )      {      sprintf( bwb_ebuf, "Too many parameters (%d) to function ASC().",         argc );      bwb_error( bwb_ebuf );      return NULL;      }#else   if ( fnc_checkargs( argc, argv, 1, 1 ) == FALSE )      {      return NULL;      }#endif   if ( argv[ 0 ].type != STRING )      {#if PROG_ERRORS      sprintf( bwb_ebuf, "Argument to function ASC() must be a string." );      bwb_error( bwb_ebuf );#else      bwb_error( err_mismatch );#endif      return NULL;      }   /* assign ASCII value of first character in the buffer */   str_btoc( tbuf, var_findsval( &( argv[ 0 ] ), argv[ 0 ].array_pos ) );   * var_findnval( &nvar, nvar.array_pos ) = (bnumber) tbuf[ 0 ];#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in fnc_asc(): string is <%s>",      tbuf );   bwb_debug( bwb_ebuf );#endif   return &nvar;   }/***************************************************************        FUNCTION:       fnc_string()        DESCRIPTION:    This C function implements the BASIC			STRING$() function.	SYNTAX:		STRING$( number, ascii-value|string$ )***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_string( int argc, struct bwb_variable *argv, int unique_id )#elsestruct bwb_variable *fnc_string( argc, argv, unique_id )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   int length;   register int i;   char c;   static char *tbuf;   static int init = FALSE;   /* initialize the variable if necessary */   if ( init == FALSE )      {      init = TRUE;      var_make( &nvar, STRING );      /* Revised to CALLOC pass-thru call by JBV */      if ( ( tbuf = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ), "fnc_string" )) == NULL )         {#if PROG_ERRORS         bwb_error( "in fnc_string(): failed to get memory for tbuf" );#else         bwb_error( err_getmem );#endif         }      }   /* check for correct number of parameters */#if PROG_ERRORS   if ( argc < 2 )      {      sprintf( bwb_ebuf, "Not enough parameters (%d) to function STRING$().",         argc );      bwb_error( bwb_ebuf );      return NULL;      }   else if ( argc > 2 )      {      sprintf( bwb_ebuf, "Too many parameters (%d) to function STRING$().",         argc );      bwb_error( bwb_ebuf );      return NULL;      }#else   if ( fnc_checkargs( argc, argv, 2, 2 ) == FALSE )      {      return NULL;      }#endif   strcpy( nvar.name, "(string$)!" );   nvar.type = STRING;   tbuf[ 0 ] = '\0';   length = (int) var_getnval( &( argv[ 0 ] ));   if ( argv[ 1 ].type == STRING )      {      str_btoc( tbuf, var_getsval( &( argv[ 1 ] )));      c = tbuf[ 0 ];      }   else      {      c = (char) var_getnval( &( argv[ 1 ] ) );      }#if INTENSIVE_DEBUG   sprintf( bwb_ebuf, "in fnc_string(): argument <%s> arg type <%c>, length <%d>",      tbuf, argv[ 1 ].type, length );   bwb_debug( bwb_ebuf );   sprintf( bwb_ebuf, "in fnc_string(): type <%c>, c <0x%x>=<%c>",      argv[ 1 ].type, c, c );   bwb_debug( bwb_ebuf );#endif   /* add characters to the string */   for ( i = 0; i < length; ++i )      {      tbuf[ i ] = c;      tbuf[ i + 1 ] = '\0';      }   str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );   return &nvar;   }/***************************************************************        FUNCTION:       fnc_instr()        DESCRIPTION:	This C function implements the BASIC 			INSTR() function, returning the position			in string string-searched$ at which			string-pattern$ occurs.	SYNTAX:		INSTR( [start-position,] string-searched$, string-pattern$ )***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_instr( int argc, struct bwb_variable *argv, int unique_id )#elsestruct bwb_variable *fnc_instr( argc, argv, unique_id )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   static int init = FALSE;   int n_pos, x_pos, y_pos;   int start_pos;   register int n;   char xbuf[ MAXSTRINGSIZE + 1 ];   char ybuf[ MAXSTRINGSIZE + 1 ];   /* initialize the variable if necessary */   if ( init == FALSE )      {      init = TRUE;      var_make( &nvar, NUMBER );      }   /* check for correct number of parameters */#if PROG_ERRORS   if ( argc < 2 )      {      sprintf( bwb_ebuf, "Not enough parameters (%d) to function INSTR().",         argc );      bwb_error( bwb_ebuf );      return NULL;      }   else if ( argc > 3 )      {      sprintf( bwb_ebuf, "Too many parameters (%d) to function INSTR().",         argc );      bwb_error( bwb_ebuf );      return NULL;      }#else   if ( fnc_checkargs( argc, argv, 2, 3 ) == FALSE )      {      return NULL;      }#endif   /* determine argument positions */   if ( argc == 3 )      {      n_pos = 0;      x_pos = 1;      y_pos = 2;      }   else      {      n_pos = -1;      x_pos = 0;      y_pos = 1;      }   /* determine starting position */   if ( n_pos == 0 )      {      start_pos = (int) var_getnval( &( argv[ n_pos ] ) ) - 1;      }   else      {      start_pos = 0;      }   /* get x and y strings */   str_btoc( xbuf, var_getsval( &( argv[ x_pos ] ) ) );   str_btoc( ybuf, var_getsval( &( argv[ y_pos ] ) ) );   /* now search for match */   for ( n = start_pos; n < (int) strlen( xbuf ); ++n )      {      if ( strncmp( &( xbuf[ n ] ), ybuf, strlen( ybuf ) ) == 0 )         {         * var_findnval( &nvar, nvar.array_pos ) = (bnumber) n + 1;         return &nvar;         }      }   /* match not found */   * var_findnval( &nvar, nvar.array_pos ) = (bnumber) 0;   return &nvar;   }/***************************************************************        FUNCTION:       fnc_spc()	DESCRIPTION:    This C function implements the BASIC			SPC() function, returning a string			containing a specified number of			(blank) spaces.	SYNTAX:		SPC( number )***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_spc( int argc, struct bwb_variable *argv, int unique_id )#elsestruct bwb_variable *fnc_spc( argc, argv, unique_id )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   return fnc_space( argc, argv, unique_id );   }/***************************************************************        FUNCTION:       fnc_space()	DESCRIPTION:    This C function implements the BASIC			SPACE() function, returning a string			containing a specified number of			(blank) spaces.	SYNTAX:		SPACE$( number )***************************************************************/#if ANSI_Cstruct bwb_variable *fnc_space( int argc, struct bwb_variable *argv, int unique_id )#elsestruct bwb_variable *fnc_space( argc, argv, unique_id )   int argc;   struct bwb_variable *argv;   int unique_id;#endif   {   static struct bwb_variable nvar;   static char *tbuf;

⌨️ 快捷键说明

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